-
Notifications
You must be signed in to change notification settings - Fork 481
/
1334.py
32 lines (27 loc) · 1.01 KB
/
1334.py
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
class Solution:
def findTheCity(self, n: int, edges: List[List[int]], distanceThreshold: int) -> int:
g = collections.defaultdict(list)
for i, j, w in edges:
g[i].append((j, w))
g[j].append((i, w))
def dijkstra(node):
dist = [float("inf")] * n
dist[node] = 0
hp = [(0, node)]
while hp:
d, cur = heapq.heappop(hp)
if dist[cur] < d:
continue
if d > distanceThreshold:
break
for ne, w in g[cur]:
if d + w < dist[ne]:
dist[ne] = d + w
heapq.heappush(hp, (dist[ne], ne))
return sum(d <= distanceThreshold for d in dist)
res = (0, float("inf"))
for i in range(n):
t = dijkstra(i)
if t <= res[1]:
res = (i, t)
return res[0]