-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1753.py
40 lines (27 loc) · 862 Bytes
/
1753.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
33
34
35
36
37
38
39
40
import sys
import heapq
get_line: iter = lambda: map(int, sys.stdin.readline().rstrip().split())
get_input: int = lambda: int(sys.stdin.readline().strip())
heap = []
V, E = get_line()
st_idx = get_input()
dist = [sys.maxsize] * (V + 1)
adj = [[] for i in range(V + 1)]
def Dijkstra(st_idx):
dist[st_idx] = 0
heapq.heappush(heap, (0, st_idx))
while heap:
ndist, npos = heapq.heappop(heap)
if dist[npos] < ndist:
continue
for nxt_pos, weight in adj[npos]:
nxt_dist = ndist + weight
if nxt_dist < dist[nxt_pos]:
dist[nxt_pos] = nxt_dist
heapq.heappush(heap, (nxt_dist, nxt_pos))
for i in range(E):
u, v, w = get_line()
adj[u].append((v, w))
Dijkstra(st_idx)
for i in range(1, V + 1):
print("INF" if dist[i] == sys.maxsize else dist[i])