-
Notifications
You must be signed in to change notification settings - Fork 0
/
1753-최단경로.py
42 lines (31 loc) · 854 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 math
import heapq
input = sys.stdin.readline
V, E = map(int, input().split())
K = int(input().rstrip())
graph = [[] for _ in range(V+1)]
distance = [math.inf]*(V+1)
visited = [False]*(V+1)
for i in range(E):
u, v, w = map(int, input().split())
graph[u].append([v, w])
def dijkstra(start):
q = []
heapq.heappush(q, (0, start))
distance[start] = 0
while q:
dist, now = heapq.heappop(q)
if visited[now]:
continue
for elem in graph[now]:
if distance[elem[0]] > dist + elem[1]:
distance[elem[0]] = dist + elem[1]
heapq.heappush(q, (dist + elem[1], elem[0]))
visited[now] = True
dijkstra(K)
for i in range(1, len(distance)):
if distance[i] == math.inf:
print("INF")
else:
print(distance[i])