Approach
Stack-based processing
For Codeforces 1473E — Minimum Path, the implementation keeps unresolved items in last-in, first-out order, often to match boundaries, parse structure, or maintain monotonic candidates.
- Define what every stack entry represents.
- Pop entries once the current item resolves or invalidates them.
- Push the current item with only the information later steps need.
Code notes
- 87 lines of Go from the credited upstream file 1473E.go.
- The implementation visibly relies on sequence storage.
- No explicit loop blocks detected.
Complexity
If each item is pushed and popped at most once, the stack work is linear.
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 "container/heap"6 . "fmt"7 "io"8)9 1011type pair73 struct {12 v int13 d int6414}15type hp73 []pair7316 17func (h hp73) Len() int { return len(h) }18func (h hp73) Less(i, j int) bool { return h[i].d < h[j].d }19func (h hp73) Swap(i, j int) { h[i], h[j] = h[j], h[i] }20func (h *hp73) Push(v interface{}) { *h = append(*h, v.(pair73)) }21func (h *hp73) Pop() (v interface{}) { a := *h; *h, v = a[:len(a)-1], a[len(a)-1]; return }22func (h *hp73) push(v pair73) { heap.Push(h, v) }23func (h *hp73) pop() pair73 { return heap.Pop(h).(pair73) }24 25func CF1473E(_r io.Reader, _w io.Writer) {26 in := bufio.NewReader(_r)27 out := bufio.NewWriter(_w)28 defer out.Flush()29 min := func(a, b int64) int64 {30 if a < b {31 return a32 }33 return b34 }35 36 var n, m, v, w, wt int37 Fscan(in, &n, &m)38 type nb struct{ to, wt int }39 g := make([][]nb, n)40 for ; m > 0; m-- {41 Fscan(in, &v, &w, &wt)42 v--43 w--44 g[v] = append(g[v], nb{w, wt})45 g[w] = append(g[w], nb{v, wt})46 }47 48 const inf int64 = 1e1849 dis := make([]int64, n*4)50 for i := range dis {51 dis[i] = inf52 }53 dis[0] = 054 h := hp73{{}}55 for len(h) > 0 {56 pd := h.pop()57 x := pd.v58 if dis[x] < pd.d {59 continue60 }61 62 63 64 for _, e := range g[x>>2] {65 w, wt := e.to, int64(e.wt)66 y := w<<2 | x&367 if newD := dis[x] + wt; newD < dis[y] {68 dis[y] = newD69 h.push(pair73{y, newD})70 }71 if newD := dis[x] + wt*2; x&1 == 0 && newD < dis[y|1] {72 dis[y|1] = newD73 h.push(pair73{y | 1, newD})74 }75 if newD := dis[x]; x>>1&1 == 0 && newD < dis[y|2] {76 dis[y|2] = newD77 h.push(pair73{y | 2, newD})78 }79 }80 }81 for i := 1; i < n; i++ {82 Fprint(out, min(dis[i<<2], dis[i<<2|3]), " ")83 }84}85 8687