-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathj2_02.py
24 lines (22 loc) · 814 Bytes
/
j2_02.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
import heapq
def solve(N, M, edge_list, start, end):
# Initialization of distances.
infinity = 1234567890
dist = [ infinity for i in range(N) ]
dist[start] = 0
priority_queue = [ (0, start) ]
while len(priority_queue) > 0:
d, u = heapq.heappop(priority_queue) # note: returns and removes the element with the smallest d (and smallest u in case of a tie)
for edge in edge_list:
p, q, w = edge
# Find an edge starting at u and save the other endpoint in v.
if p == u:
v = q
elif q == u:
v = p
else:
continue
if dist[v] > dist[u] + w:
dist[v] = dist[u] + w
heapq.heappush(priority_queue, (dist[v], v))
return dist[end]