-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0850-Dijkstra求最短路II.go
132 lines (115 loc) · 1.91 KB
/
0850-Dijkstra求最短路II.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package main
import (
"bufio"
"container/heap"
"fmt"
"io"
"os"
"strconv"
"strings"
)
const (
N = 150010
inf = 0x3f3f3f3f
)
var (
h, e, ne, w, dist [N]int
idx int
st [N]bool
n, m int
scanner *bufio.Scanner
)
type pair struct {
vertex int
dist int
}
func add(x, y, c int) {
w[idx] = c
e[idx] = y
ne[idx] = h[x]
h[x] = idx
idx++
}
func dijkstra() int {
for i := 0; i < len(dist); i++ {
dist[i] = inf
}
dist[1] = 0
pq := PriorityQueue{
&pair{
vertex: 1,
dist: 0,
}}
heap.Init(&pq)
for pq.Len() > 0 {
p := heap.Pop(&pq).(*pair)
if st[p.vertex] {
continue
}
st[p.vertex] = true
for i := h[p.vertex]; i != -1; i = ne[i] {
v := e[i]
if dist[v] > p.dist+w[i] {
dist[v] = p.dist + w[i]
heap.Push(&pq, &pair{
vertex: v,
dist: dist[v],
})
}
}
}
if dist[n] == inf {
return -1
}
return dist[n]
}
func main() {
newScanner(os.Stdin)
for i := 0; i < len(h); i++ {
h[i] = -1
}
fmt.Scan(&n, &m)
for i := 0; i < m; i++ {
a := readLine()
add(a[0], a[1], a[2])
}
fmt.Println(dijkstra())
}
type PriorityQueue []*pair
func (p PriorityQueue) Len() int {
return len(p)
}
func (p PriorityQueue) Less(i, j int) bool {
if p[i].dist == p[j].dist {
return p[i].vertex < p[j].vertex
}
return p[i].dist < p[j].dist
}
func (p PriorityQueue) Swap(i, j int) {
p[i], p[j] = p[j], p[i]
}
func (p *PriorityQueue) Push(x interface{}) {
*p = append(*p, x.(*pair))
}
func (p *PriorityQueue) Pop() interface{} {
old := *p
n := len(old)
x := old[n-1]
*p = old[0 : n-1]
return x
}
func newScanner(reader io.Reader) {
scanner = bufio.NewScanner(reader)
bs := make([]byte, 20000*1024)
scanner.Buffer(bs, len(bs))
}
func readLine() []int {
scanner.Scan()
line := strings.Split(scanner.Text(), " ")
res := make([]int, len(line))
for i, l := range line {
x, _ := strconv.Atoi(l)
res[i] = x
}
return res
}