-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path1916.py
More file actions
36 lines (29 loc) · 804 Bytes
/
Copy path1916.py
File metadata and controls
36 lines (29 loc) · 804 Bytes
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
import sys
import heapq
input = sys.stdin.readline
N = int(input())
M = int(input())
INF = float('inf')
graph = [[] for _ in range(N + 1)]
for _ in range(M):
u, v, w = map(int, input().split())
graph[u-1].append((v-1, w))
start, end = map(int, input().split())
def dijkstra(start, end):
dist_arr = [INF]*N
dist_arr[start-1] = 0
pq = [(0, start-1)]
while pq:
dist, now = heapq.heappop(pq)
if dist_arr[now] < dist:
if now == end:
break
else: continue
for next, weight in graph[now]:
cost = dist + weight
if cost < dist_arr[next]:
dist_arr[next] = cost
heapq.heappush(pq, (cost, next))
return dist_arr
result = dijkstra(start, end)
print(result[end-1])