https://www.acmicpc.net/problem/1753
https://github.com/stellaluminary/Baekjoon
우선순위 큐인 heapq를 이용한 다익스트라 방법으로 최단경로를 구할 수 있다.
import heapq
import sys
input = sys.stdin.readline
INF = 1e9
v,e = map(int, input().split())
start = int(input())
graph = [[] for _ in range(v+1)]
distance = [INF]*(v+1)
for i in range(e):
a,b,c = map(int, input().split())
graph[a].append((b,c))
def dijstra(start):
q = []
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]))
dijstra(start)
for i in range(1, v+1):
if distance[i] == INF:
print('INF')
else:
print(distance[i])