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 pair7 struct{ d, ok int }12type hPair7 struct {13 v int14 pair715}16type hp7 []hPair717 18func (h hp7) Len() int { return len(h) }19func (h hp7) Less(i, j int) bool { a, b := h[i], h[j]; return a.d < b.d || a.d == b.d && a.ok > b.ok }20func (h hp7) Swap(i, j int) { h[i], h[j] = h[j], h[i] }21func (h *hp7) Push(v interface{}) { *h = append(*h, v.(hPair7)) }22func (h *hp7) Pop() (v interface{}) { a := *h; *h, v = a[:len(a)-1], a[len(a)-1]; return }23func (h *hp7) push(v hPair7) { heap.Push(h, v) }24func (h *hp7) pop() hPair7 { return heap.Pop(h).(hPair7) }25 26func CF507E(_r io.Reader, _w io.Writer) {27 in := bufio.NewReader(_r)28 out := bufio.NewWriter(_w)29 defer out.Flush()30 31 var n, m, v, w, ok int32 Fscan(in, &n, &m)33 type nb struct{ to, ok, i int }34 g := make([][]nb, n)35 type edge struct{ v, w, ok int }36 es := make([]edge, m)37 for i := range es {38 Fscan(in, &v, &w, &ok)39 v--40 w--41 es[i] = edge{v, w, ok}42 g[v] = append(g[v], nb{w, ok, i})43 g[w] = append(g[w], nb{v, ok, i})44 }45 46 dis := make([]pair7, n)47 for i := range dis {48 dis[i].d = 1e949 }50 dis[0] = pair7{}51 vis := make([]bool, n)52 type vi struct{ v, i int }53 fa := make([]vi, n)54 for i := range fa {55 fa[i].v = -156 }57 h := hp7{{}}58 for len(h) > 0 {59 vd := h.pop()60 v := vd.v61 if vis[v] {62 continue63 }64 vis[v] = true65 for _, e := range g[v] {66 w := e.to67 if newD, newOK := dis[v].d+1, dis[v].ok+e.ok; newD < dis[w].d || newD == dis[w].d && newOK > dis[w].ok {68 dis[w] = pair7{newD, newOK}69 fa[w] = vi{v, e.i}70 h.push(hPair7{w, dis[w]})71 }72 }73 }74 75 onPath := make([]int, m)76 for x := n - 1; fa[x].v >= 0; x = fa[x].v {77 onPath[fa[x].i] = 178 }79 ans := []edge{}80 for i, e := range es {81 if e.ok != onPath[i] {82 ans = append(ans, edge{e.v + 1, e.w + 1, onPath[i]})83 }84 }85 Fprintln(out, len(ans))86 for _, e := range ans {87 Fprintln(out, e.v, e.w, e.ok)88 }89}90 9192