Use this to learn the idea, then write your own version.
1package main2 3import (4 "bufio"5 "container/heap"6 . "fmt"7 "io"8 "sort"9)10 1112type vd22 struct{ v, d int }13type hp22 []vd2214 15func (h hp22) Len() int { return len(h) }16func (h hp22) Less(i, j int) bool { return h[i].d < h[j].d }17func (h hp22) Swap(i, j int) { h[i], h[j] = h[j], h[i] }18func (h *hp22) Push(v interface{}) { *h = append(*h, v.(vd22)) }19func (h *hp22) Pop() (v interface{}) { a := *h; *h, v = a[:len(a)-1], a[len(a)-1]; return }20func (h *hp22) push(v vd22) { heap.Push(h, v) }21func (h *hp22) pop() vd22 { return heap.Pop(h).(vd22) }22 23func CF1422D(_r io.Reader, out io.Writer) {24 in := bufio.NewReader(_r)25 min := func(a, b int) int {26 if a < b {27 return a28 }29 return b30 }31 abs := func(x int) int {32 if x < 0 {33 return -x34 }35 return x36 }37 38 var n, sx, sy, tx, ty, x, y int39 Fscan(in, &n, &n, &sx, &sy, &tx, &ty)40 dist := make([]int, n)41 end := make([]int, n)42 q := make(hp22, n)43 type pt struct{ x, y, i int }44 ps := make([]pt, n)45 qs := make([]pt, n)46 for i := range dist {47 Fscan(in, &x, &y)48 dist[i] = min(abs(sx-x), abs(sy-y))49 end[i] = abs(x-tx) + abs(y-ty)50 q[i] = vd22{i, dist[i]}51 ps[i] = pt{x, y, i}52 qs[i] = pt{y, x, i}53 }54 55 type nb struct{ to, wt int }56 g := make([][]nb, n)57 for _, ps := range [][]pt{ps, qs} {58 sort.Slice(ps, func(i, j int) bool { a, b := ps[i], ps[j]; return a.x < b.x || a.x == b.x && a.y < b.y })59 for i := 1; i < n; i++ {60 p, q := ps[i-1], ps[i]61 v, w, d := p.i, q.i, min(q.x-p.x, abs(q.y-p.y))62 g[v] = append(g[v], nb{w, d})63 g[w] = append(g[w], nb{v, d})64 }65 }66 67 heap.Init(&q)68 for len(q) > 0 {69 p := q.pop()70 v := p.v71 if dist[v] < p.d {72 continue73 }74 for _, e := range g[v] {75 w := e.to76 if newD := dist[v] + e.wt; newD < dist[w] {77 dist[w] = newD78 q.push(vd22{w, newD})79 }80 }81 }82 ans := abs(sx-tx) + abs(sy-ty)83 for i, d := range dist {84 ans = min(ans, d+end[i])85 }86 Fprint(out, ans)87}88 8990