https://www.acmicpc.net/problem/1753
from collections import defaultdict
import sys, heapq
input = sys.stdin.readline
# 입력값으로 그래프 그리기
v_num, e_num = map(int, input().split(" "))
start = int(input())
graph = defaultdict(list)
for _ in range(e_num):
a, b, c= map(int, input().split(" "))
graph[a].append((c,b))
# 우선순위큐를 사용해 bfs
queue = [(0,start)]
dist = defaultdict(int)
while queue:
weight, node = heapq.heappop(queue)
if node not in dist:
dist[node] = weight
for cw, cv in graph[node]:
alt = weight + cw
heapq.heappush(queue, (alt, cv))
# 시작점에서 자기자신을 포함해 모든 정점까지의 최단거리 구하기
for i in range(1, v_num+1):
if i in dist:
print(dist[i])
else:
print("INF")