https://www.acmicpc.net/problem/1753
import sys
import heapq
from collections import defaultdict
def dijkstra(start):
heap = []
heapq.heappush(heap, (0, start))
dist[start] = 0
while heap:
curr_dist, curr_num = heapq.heappop(heap)
for next_dist, next_num in graph[curr_num]:
if curr_dist + next_dist < dist[next_num]:
dist[next_num] = curr_dist + next_dist
heapq.heappush(heap, [curr_dist + next_dist, next_num])
n, e = map(int, sys.stdin.readline().split())
k = int(sys.stdin.readline())
dist = [sys.maxsize] * (n + 1)
graph = defaultdict(list)
for _ in range(e):
start, end, weight = map(int, sys.stdin.readline().split())
graph[start].append([weight, end])
dijkstra(k)
for i in range(1, n + 1):
if dist[i] == sys.maxsize:
print("INF")
else:
print(dist[i])