-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1753번.py
More file actions
37 lines (32 loc) · 824 Bytes
/
1753번.py
File metadata and controls
37 lines (32 loc) · 824 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
37
#최단 경로 구하기
import sys
input = sys.stdin.readline
from queue import PriorityQueue
V, E = map(int, input().split())
K = int(input())
distance = [sys.maxsize] * (V+1)
visited = [False] * (V+1)
myList = [[] for _ in range(V+1)]
q = PriorityQueue()
for _ in range(E):
u, v, w = map(int, input().split())
myList[u].append((v, w))
q.put((0, K))
distance[K] = 0
while q.qsize() > 0:
current = q.get()
c_v = current[1]
if visited[c_v]:
continue
visited[c_v] = True
for tmp in myList[c_v]:
next = tmp[0]
value = tmp[1]
if distance[next] > distance[c_v] + value:
distance[next] = distance[c_v] + value
q.put((distance[next], next))
for i in range(1, V+1):
if visited[i]:
print(distance[i])
else:
print("INF")