-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
/
Copy pathSolution.go
50 lines (48 loc) · 1.08 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
func closestMeetingNode(edges []int, node1 int, node2 int) int {
n := len(edges)
g := make([][]int, n)
for i, j := range edges {
if j != -1 {
g[i] = append(g[i], j)
}
}
const inf int = 1 << 30
dijkstra := func(i int) []int {
dist := make([]int, n)
for j := range dist {
dist[j] = inf
}
dist[i] = 0
q := hp{}
heap.Push(&q, pair{0, i})
for len(q) > 0 {
i := heap.Pop(&q).(pair).i
for _, j := range g[i] {
if dist[j] > dist[i]+1 {
dist[j] = dist[i] + 1
heap.Push(&q, pair{dist[j], j})
}
}
}
return dist
}
d1 := dijkstra(node1)
d2 := dijkstra(node2)
ans, d := -1, inf
for i, a := range d1 {
b := d2[i]
t := max(a, b)
if t < d {
d = t
ans = i
}
}
return ans
}
type pair struct{ d, i int }
type hp []pair
func (h hp) Len() int { return len(h) }
func (h hp) Less(i, j int) bool { return h[i].d < h[j].d }
func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
func (h *hp) Push(v any) { *h = append(*h, v.(pair)) }
func (h *hp) Pop() any { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v }