-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2.py
31 lines (30 loc) · 765 Bytes
/
2.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
import heapq
import sys
input=sys.stdin.readline
output=sys.stdout.write
V,E=map(int,input().split())
start=int(input().strip())
D=[float("inf")]*(V+1)
graph=[[]for _ in range(V+1)]
for _ in range(E):
u,v,w=map(int,input().split())
graph[u].append((v,w))
def dijkstra(start):
q=[]
heapq.heappush(q,(0,start))
D[start]=0
while q:
dist, node=heapq.heappop(q)
for next in graph[node]:
if dist>D[node]:#이미 도달한 경우
continue
cost=next[1]+D[node]
if cost<D[next[0]]:
D[next[0]]=cost
heapq.heappush(q,(cost,next[0]))
dijkstra(start)
for i in D[1:]:
if i==float("inf"):
output('INF\n')
else:
output(f"{i}\n")