Approach
Breadth-first search
For Codeforces 1320B — Navigation System, the implementation explores reachable states in layers, which is the standard shape for unweighted shortest paths and minimum-step transitions.
- Model each valid configuration as a state and each legal move as an edge.
- Seed the queue with the starting state and mark it immediately.
- Expand each state once, recording distance or reachability for unseen neighbours.
Code notes
- 64 lines of Go from the credited upstream file 1320B.go.
- The implementation visibly relies on sequence storage.
- No explicit loop blocks detected.
Complexity
Verify that each state and transition is processed only a bounded number of times; that determines the traversal cost.
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)8 910func CF1320B(_r io.Reader, out io.Writer) {11 in := bufio.NewReader(_r)12 var n, m, v, w, k, min, extra int13 Fscan(in, &n, &m)14 g := make([][]int, n)15 rg := make([][]int, n)16 for ; m > 0; m-- {17 Fscan(in, &v, &w)18 v--19 w--20 g[v] = append(g[v], w)21 rg[w] = append(rg[w], v)22 }23 Fscan(in, &k)24 path := make([]int, k)25 for i := range path {26 Fscan(in, &path[i])27 path[i]--28 }29 30 31 32 dis := make([]int, n)33 vis := make([]bool, n)34 vis[path[k-1]] = true35 q := []int{path[k-1]}36 for len(q) > 0 {37 v, q = q[0], q[1:]38 for _, w := range rg[v] {39 if !vis[w] {40 vis[w] = true41 dis[w] = dis[v] + 142 q = append(q, w)43 }44 }45 }46 47 for i, cur := range path[:k-1] {48 next := path[i+1]49 if dis[next] >= dis[cur] { 50 min++ 51 continue52 }53 for _, other := range g[cur] {54 if other != next && dis[other] == dis[next] { 55 extra++56 break57 }58 }59 }60 Fprint(out, min, min+extra)61}62 6364