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 pair86 struct {12 v int13 dis int6414}15type hp86 []pair8616 17func (h hp86) Len() int { return len(h) }18func (h hp86) Less(i, j int) bool { return h[i].dis < h[j].dis }19func (h hp86) Swap(i, j int) { h[i], h[j] = h[j], h[i] }20func (h *hp86) Push(v interface{}) { *h = append(*h, v.(pair86)) }21func (h *hp86) Pop() (v interface{}) { a := *h; *h, v = a[:len(a)-1], a[len(a)-1]; return }22func (h *hp86) push(v pair86) { heap.Push(h, v) }23func (h *hp86) pop() pair86 { return heap.Pop(h).(pair86) }24 25func CF786B(_r io.Reader, _w io.Writer) {26 in := bufio.NewReader(_r)27 out := bufio.NewWriter(_w)28 defer out.Flush()29 30 var n, m, st, tp, v, w, wt, l, r int31 Fscan(in, &n, &m, &st)32 d := 4 * n33 type nb struct{ to, wt int }34 g := make([][]nb, 2*d)35 leaf := make([]int, n+1)36 37 type seg []struct{ l, r int }38 t := make(seg, d)39 var build func(o, l, r int)40 build = func(o, l, r int) {41 t[o].l, t[o].r = l, r42 if l == r {43 leaf[l] = o44 g[o] = append(g[o], nb{o + d, 0})45 g[o+d] = append(g[o+d], nb{o, 0})46 return47 }48 lo, ro := o<<1, o<<1|149 g[o] = append(g[o], nb{lo, 0}, nb{ro, 0})50 g[lo+d] = append(g[lo+d], nb{o + d, 0})51 g[ro+d] = append(g[ro+d], nb{o + d, 0})52 m := (l + r) >> 153 build(lo, l, m)54 build(ro, m+1, r)55 }56 build(1, 1, n)57 58 var conn func(int)59 conn = func(o int) {60 if l <= t[o].l && t[o].r <= r {61 if tp == 2 {62 g[v] = append(g[v], nb{o, wt})63 } else {64 g[o+d] = append(g[o+d], nb{v, wt})65 }66 return67 }68 m := (t[o].l + t[o].r) >> 169 if l <= m {70 conn(o << 1)71 }72 if m < r {73 conn(o<<1 | 1)74 }75 }76 for ; m > 0; m-- {77 if Fscan(in, &tp); tp == 1 {78 Fscan(in, &v, &w, &wt)79 g[leaf[v]] = append(g[leaf[v]], nb{leaf[w], wt})80 } else {81 Fscan(in, &v, &l, &r, &wt)82 v = leaf[v]83 conn(1)84 }85 }86 87 const inf int64 = 1e1888 dist := make([]int64, 2*d)89 for i := range dist {90 dist[i] = inf91 }92 st = leaf[st]93 dist[st] = 094 q := hp86{{st, 0}}95 for len(q) > 0 {96 p := q.pop()97 v := p.v98 if dist[v] < p.dis {99 continue100 }101 for _, e := range g[v] {102 w := e.to103 if newD := dist[v] + int64(e.wt); newD < dist[w] {104 dist[w] = newD105 q.push(pair86{w, newD})106 }107 }108 }109 for _, v := range leaf[1:] {110 if dist[v] < inf {111 Fprint(out, dist[v], " ")112 } else {113 Fprint(out, "-1 ")114 }115 }116}117 118119