-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
/
Copy pathSolution.go
66 lines (57 loc) · 1.31 KB
/
Solution.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
const Inf = 0x3f3f3f3f
type pair struct {
first int
second int
}
var _ heap.Interface = (*pairs)(nil)
type pairs []pair
func (a pairs) Len() int { return len(a) }
func (a pairs) Less(i int, j int) bool {
return a[i].first < a[j].first || a[i].first == a[j].first && a[i].second < a[j].second
}
func (a pairs) Swap(i int, j int) { a[i], a[j] = a[j], a[i] }
func (a *pairs) Push(x interface{}) { *a = append(*a, x.(pair)) }
func (a *pairs) Pop() interface{} { l := len(*a); t := (*a)[l-1]; *a = (*a)[:l-1]; return t }
func networkDelayTime(times [][]int, n int, k int) int {
graph := make([]pairs, n)
for _, time := range times {
from, to, time := time[0]-1, time[1]-1, time[2]
graph[from] = append(graph[from], pair{to, time})
}
dis := make([]int, n)
for i := range dis {
dis[i] = Inf
}
dis[k-1] = 0
vis := make([]bool, n)
h := make(pairs, 0)
heap.Push(&h, pair{0, k - 1})
for len(h) > 0 {
from := heap.Pop(&h).(pair).second
if vis[from] {
continue
}
vis[from] = true
for _, e := range graph[from] {
to, d := e.first, dis[from]+e.second
if d < dis[to] {
dis[to] = d
heap.Push(&h, pair{d, to})
}
}
}
ans := math.MinInt32
for _, d := range dis {
ans = max(ans, d)
}
if ans == Inf {
return -1
}
return ans
}
func max(x, y int) int {
if x > y {
return x
}
return y
}