-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path9-2.py
More file actions
45 lines (36 loc) · 1.16 KB
/
Copy path9-2.py
File metadata and controls
45 lines (36 loc) · 1.16 KB
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
41
42
43
44
45
#최단 거리가 가장 짧은 노드를 선택하는 과정을 우선순위 큐를 이용하는 방식으로 대체
import heapq
import sys
input = sys.stdin.readline
INF = int(1e9)
n,m = map(int,input().split())
start = int(input())
graph = [[] for i in range(n+1)]
distance = [INF]*(n+1)
for _ in range(m):
a,b,c = map(int,input().split())
graph[a].append((b,c))
def dijkstra(start):
q = []
# 시작 노드로 가는 최단 거리는 0 => (0,start)
heapq.heappush(q,(0,start))
distance[start] = 0
while q:
# 가장 최단 거리가 짧은 노드에 대한 정보 꺼내기
dist,now = heapq.heappop(q)
# 현재 노드가 이미 처리된 적이 있다면 무시
if distance[now] <dist:
continue
# 현재 노드와 연결된 다른 인접한 노드들 확인
for i in graph[now]:
cost = dist+i[1]
# 비용 확인
if cost < distance[i[0]]:
distance[i[0]] = cost
heapq.heappush(q,(cost,i[0]))
dijkstra(start)
for i in range(1,n+1):
if distance[i] == INF:
print("INFINITY")
else:
print(distance[i])