-
-
Notifications
You must be signed in to change notification settings - Fork 8.9k
/
Copy pathSolution.py
29 lines (29 loc) · 930 Bytes
/
Solution.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
class Solution:
def findAnswer(self, n: int, edges: List[List[int]]) -> List[bool]:
g = defaultdict(list)
for i, (a, b, w) in enumerate(edges):
g[a].append((b, w, i))
g[b].append((a, w, i))
dist = [inf] * n
dist[0] = 0
q = [(0, 0)]
while q:
da, a = heappop(q)
if da > dist[a]:
continue
for b, w, _ in g[a]:
if dist[b] > dist[a] + w:
dist[b] = dist[a] + w
heappush(q, (dist[b], b))
m = len(edges)
ans = [False] * m
if dist[n - 1] == inf:
return ans
q = deque([n - 1])
while q:
a = q.popleft()
for b, w, i in g[a]:
if dist[a] == dist[b] + w:
ans[i] = True
q.append(b)
return ans