-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
0743-network-delay-time.go
71 lines (57 loc) · 1.41 KB
/
0743-network-delay-time.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
type neighbour struct {
destination int
weight int
}
type heapNode struct {
distance int
nodeIndex int
}
func networkDelayTime(times [][]int, n int, k int) int {
edgeMap := make(map[int][]neighbour)
for _, log := range times {
edgeMap[log[0]] = append(edgeMap[log[0]], neighbour{destination: log[1], weight: log[2]})
}
h := &minHeap{heapNode{distance: 0, nodeIndex: k}}
heap.Init(h)
visited := make(map[int]bool)
t := 0
for !h.isEmpty() {
hNode := heap.Pop(h).(heapNode)
if vis := visited[hNode.nodeIndex]; vis {
continue
}
t = max(t, hNode.distance)
visited[hNode.nodeIndex] = true
neighbours := edgeMap[hNode.nodeIndex]
for _, neigh := range neighbours {
if vis := visited[neigh.destination]; !vis {
heap.Push(h, heapNode{
distance: neigh.weight + hNode.distance,
nodeIndex: neigh.destination})
}
}
}
if n == len(visited) {
return t
}
return -1
}
func max(a, b int) int {
if a < b {
return b
}
return a
}
type minHeap []heapNode
func (h minHeap) Len() int { return len(h) }
func (h *minHeap) isEmpty() bool { return len(*h) == 0 }
func (h minHeap) Less(i, j int) bool { return h[i].distance < h[j].distance }
func (h minHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
func (h *minHeap) Push(x interface{}) {
*h = append(*h, x.(heapNode))
}
func (h *minHeap) Pop() interface{} {
l := (*h)[len(*h)-1]
*h = (*h)[:len(*h)-1]
return l
}