- Translate each rule into one explicit state update.
- Maintain the invariant after every processed item.
- Return the accumulated state once all relevant input has been handled.
Code notes
- 72 lines of Go from the credited upstream file 416E.go.
- The implementation visibly relies on sequence storage.
- No explicit loop blocks detected.
Complexity
Count the number and nesting of passes over the input, then include the maintained containers in the memory estimate.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
1package main2 3import (4 "bufio"5 . "fmt"6 "io"7 "slices"8)9 1011func cf416E(in io.Reader, _w io.Writer) {12 out := bufio.NewWriter(_w)13 defer out.Flush()14 var n, m int15 Fscan(in, &n, &m)16 g := make([][]int, n)17 for i := range g {18 g[i] = make([]int, n)19 for j := range g[i] {20 if j != i {21 g[i][j] = 1e922 }23 }24 }25 for range m {26 var v, w, wt int27 Fscan(in, &v, &w, &wt)28 v--29 w--30 g[v][w] = wt31 g[w][v] = wt32 }33 wt := make([][]int, n)34 for i := range wt {35 wt[i] = slices.Clone(g[i])36 }37 for k := range g {38 for i := range g {39 for j := range g {40 g[i][j] = min(g[i][j], g[i][k]+g[k][j])41 }42 }43 }44 45 for i, gi := range g {46 cnt := make([]int, n)47 for j := range n {48 if j == i || gi[j] == 1e9 {49 continue50 }51 for k := range n {52 if k != j && gi[k]+wt[k][j] == gi[j] {53 cnt[j]++54 }55 }56 }57 for j := i + 1; j < n; j++ {58 s := 059 if gi[j] < 1e9 {60 for k := range n {61 if gi[k]+g[k][j] == gi[j] {62 s += cnt[k]63 }64 }65 }66 Fprint(out, s, " ")67 }68 }69}70 7172