For a given weighted graph G(V, E) and a source r, find the source shortest path to each vertex from the source (SSSP: Single Source Shortest Path).

Constraints

* 1 ≤ |V| ≤ 100000
* 0 ≤ di ≤ 10000
* 0 ≤ |E| ≤ 500000
* There are no parallel edges
* There are no self-loops

Input

An edge-weighted graph G (V, E) and the source r.


|V| |E| r
s0 t0 d0
s1 t1 d1
:
s|E|-1 t|E|-1 d|E|-1


|V| is the number of vertices and |E| is the number of edges in G. The graph vertices are named with the numbers 0, 1,..., |V|-1 respectively. r is the source of the graph.

si and ti represent source and target vertices of i-th edge (directed) and di represents the cost of the i-th edge.

Output

Print the costs of SSSP in the following format.


c0
c1
:
c|V|-1


The output consists of |V| lines. Print the cost of the shortest path from the source r to each vertex 0, 1, ... |V|-1 in order. If there is no path from the source to a vertex, print INF.

Examples

Input

4 5 0
0 1 1
0 2 4
1 2 2
2 3 1
1 3 5


Output

0
1
3
4


Input

4 6 1
0 1 1
0 2 4
2 0 1
1 2 2
3 1 1
3 2 5


Output

3
0
2
INF

Difficulty: UNKNOWN_DIFFICULTY

Solutions:
from heapq import heappush, heappop
INF = 10 ** 10
(a, b, c) = map(int, input().split())
d = [[] for i in range(a)]
for i in range(b):
	(ta, tb, tc) = map(int, input().split())
	d[ta].append((tb, tc))

def dijkstra(N, G, s):
	dist = [INF] * N
	que = [(0, s)]
	dist[s] = 0
	while que:
		(c, v) = heappop(que)
		if dist[v] < c:
			continue
		for (t, cost) in G[v]:
			if dist[v] + cost < dist[t]:
				dist[t] = dist[v] + cost
				heappush(que, (dist[t], t))
	for i in range(a):
		print(dist[i] if dist[i] < INF else 'INF')
dijkstra(a, d, c)


def main():
	(v, e, r) = map(int, input().split())
	ans = [float('inf') for _ in range(v)]
	E = [tuple(map(int, input().split())) for _ in range(e)]
	ans[r] = 0
	for i in range(v):
		update = False
		for (s, t, c) in E:
			if ans[t] > ans[s] + c:
				ans[t] = ans[s] + c
				update = True
		if not update:
			break
	for i in ans:
		if i == float('inf'):
			print('INF')
		else:
			print(i)
main()


def dijkstra(vertex_num, edge_list, start):
	import heapq
	d = [float('inf')] * vertex_num
	d[start] = 0
	hq = []
	heapq.heappush(hq, [0, start])
	while len(hq) > 0:
		(cost, frm) = heapq.heappop(hq)
		for (cost, to) in edge_list[frm]:
			if d[to] > d[frm] + cost:
				d[to] = d[frm] + cost
				heapq.heappush(hq, [d[to], to])
	return d
(n, m, s) = map(int, input().split())
adj_list = [[] for _ in range(n)]
for i in range(m):
	(frm, to, cost) = map(int, input().split())
	adj_list[frm].append([cost, to])
d = dijkstra(n, adj_list, s)
for i in range(len(d)):
	print(d[i]) if d[i] != float('inf') else print('INF')


import heapq
(n, m, r) = map(int, input().split())
G = [{} for i in range(n)]
for j in range(m):
	(s, t, d) = map(int, input().split())
	G[s][t] = d
inf = 10 ** 20
D = [inf for i in range(n)]
D[r] = 0
Q = [(0, r)]
heapq.heapify(Q)
while Q:
	(_, x) = heapq.heappop(Q)
	for y in G[x].keys():
		if D[x] + G[x][y] < D[y]:
			D[y] = D[x] + G[x][y]
			heapq.heappush(Q, (D[y], y))
for i in range(n):
	if D[i] == inf:
		print('INF')
	else:
		print(D[i])


import sys
from heapq import heappush, heappop
inf = 1 << 30

def solve():
	(N, M, s) = map(int, sys.stdin.readline().split())
	Adj = [[] for i in range(N)]
	for i in range(M):
		(ui, vi, ci) = map(int, sys.stdin.readline().split())
		Adj[ui].append((vi, ci))
	d = dijkstra(N, Adj, s)
	for di in d:
		print(di if di < inf else 'INF')

def dijkstra(N, Adj, s):
	d = [inf] * N
	d[s] = 0
	pq = [(0, s)]
	fixed = 0
	while pq:
		(di, v) = heappop(pq)
		if di > d[v]:
			continue
		for (u, c) in Adj[v]:
			if d[v] + c < d[u]:
				d[u] = d[v] + c
				heappush(pq, (d[u], u))
		fixed += 1
		if fixed == N - 1:
			break
	return d
solve()


from collections import defaultdict
from heapq import heappush, heappop

def main():
	fl = input().split(' ')
	V = int(fl[0])
	E = int(fl[1])
	R = int(fl[2])
	G = defaultdict(list)
	for i in range(int(E)):
		(s, t, w) = [int(x) for x in input().split(' ')]
		G[s].append((t, w))
	d = {}
	INF = float('inf')
	for i in range(V):
		d[i] = INF
	d[R] = 0
	q = [R]
	while q:
		u = q.pop(0)
		for v in G[u]:
			if d[v[0]] > d[u] + v[1]:
				d[v[0]] = d[u] + v[1]
				q.append(v[0])
	for k in d.keys():
		if d[k] == float('inf'):
			print('INF')
		else:
			print(d[k])
main()


import heapq
(v, e, r) = map(int, input().split())
E = [[] for _ in range(v)]
inf = 10 ** 10
for _ in range(e):
	(s, t, d) = map(int, input().split())
	E[s].append((t, d))
D = [inf for _ in range(v)]
D[r] = 0
Q = []
heapq.heappush(Q, (0, r))
while Q:
	(d, q) = heapq.heappop(Q)
	for (n, c) in E[q]:
		if D[n] > d + c:
			D[n] = d + c
			heapq.heappush(Q, (d + c, n))
for d in D:
	if d < inf:
		print(d)
	else:
		print('INF')


import sys
from pprint import pprint
from heapq import heappop, heappush
(V, E, r) = map(int, sys.stdin.readline().strip().split())
edges = [[] for _ in range(V)]
for _ in range(E):
	(s, t, d) = map(int, sys.stdin.readline().strip().split())
	edges[s].append((t, d))
dist = [float('inf') for _ in range(V)]
prev = [None] * V
q = []
dist[r] = 0
heappush(q, (dist[r], r))
while q:
	(du, u) = heappop(q)
	if dist[u] < du:
		continue
	for (v, weight) in edges[u]:
		alt = du + weight
		if dist[v] > alt:
			dist[v] = alt
			prev[v] = u
			heappush(q, (alt, v))
for d in dist:
	print('INF' if d == float('inf') else d)


(V, E, r) = map(int, input().split())
edge = []
for i in range(E):
	edge.append(list(map(int, input().split())))
inf = float('inf')
d = [inf for i in range(V)]
d[r] = 0
while True:
	update = False
	for i in range(E):
		e = edge[i]
		if d[e[0]] != inf and d[e[1]] > d[e[0]] + e[2]:
			d[e[1]] = d[e[0]] + e[2]
			update = True
	if update == False:
		break
for i in d:
	if i == inf:
		print('INF')
	else:
		print(i)


from collections import defaultdict
import heapq as hq
INF = 10 ** 10
(V, E, r) = map(int, input().split())
e = defaultdict(list)
for i in range(E):
	(s, t, d) = map(int, input().split())
	e[s].append((d, t))
D = [INF] * V
B = [False] * V
h = []
hq.heappush(h, (0, r))
cnt = V
while h and cnt:
	(nd, n) = hq.heappop(h)
	if B[n]:
		continue
	D[n] = nd
	B[n] = True
	cnt -= 0
	for (d, nn) in e[n]:
		if nd + d < D[nn]:
			hq.heappush(h, (nd + d, nn))
for d in D:
	if d == INF:
		print('INF')
	else:
		print(d)


INF = -10000000000

def bellman_ford(s, D, G):
	D[s] = 0
	while True:
		update = False
		for i in range(E):
			e = G[i]
			if D[e[0]] != INF and (D[e[1]] == INF or D[e[1]] > D[e[0]] + e[2]):
				D[e[1]] = D[e[0]] + e[2]
				update = True
		if not update:
			break
(V, E, r) = map(int, input().split())
D = [INF] * V
G = []
for i in range(E):
	G.append(list(map(int, input().split())))
bellman_ford(r, D, G)
for i in range(V):
	print('INF' if D[i] == INF else D[i])


from sys import stdin
from collections import defaultdict
from heapq import heappush, heappop
import math
readline = stdin.readline
(v, e, r) = map(int, readline().split())
g = defaultdict(list)
for i in range(e):
	(s, t, length) = map(int, readline().split())
	g[s].append((length, t))
d = [float('inf')] * v
d[r] = 0
heap = [(d[r], r)]
while heap:
	(du, u) = heappop(heap)
	for (length, v) in g[u]:
		if d[v] > du + length:
			d[v] = du + length
			heappush(heap, (d[v], v))
for di in d:
	print('INF' if math.isinf(di) else di)


from heapq import heappush, heappop
(V, E, s) = map(int, input().split())
Adj = [[] for i in range(V)]
for i in range(E):
	(ui, vi, ci) = map(int, input().split())
	Adj[ui].append([vi, ci])
d = [float('inf')] * V
d[s] = 0
pq = [[0, s]]
fixed = 0
while pq:
	(di, v) = heappop(pq)
	if di > d[v]:
		continue
	for (u, c) in Adj[v]:
		if d[v] + c < d[u]:
			d[u] = d[v] + c
			heappush(pq, [d[u], u])
	fixed += 1
	if fixed == V - 1:
		break
for di in d:
	print(di if di != float('inf') else 'INF')


import sys
from collections import deque
(v, e, r) = map(int, input().split())
edge_info = [0] * v
for i in range(v):
	edge_info[i] = []
for i in range(e):
	(e1, e2, w) = map(int, input().split())
	edge_info[e1].append((e2, w))
v_q = deque()
INF = 9999999999999999999
d = [INF for i in range(v)]
d[r] = 0
v_q.append((0, r))
while len(v_q) != 0:
	(cost, u) = v_q.popleft()
	if cost > d[u]:
		continue
	for j in range(len(edge_info[u])):
		(e2, w) = edge_info[u][j]
		alt = cost + w
		if d[e2] > alt:
			d[e2] = alt
			v_q.append((alt, e2))
for i in range(v):
	if d[i] != INF:
		print(d[i])
	else:
		print('INF')


import heapq
(V, E, root) = map(int, input().split())
visited = [False] * V
nodes = [[] for i in range(V)]
cost = [[] for i in range(V)]
for i in range(E):
	(s, t, d) = map(int, input().split())
	nodes[s].append(t)
	cost[s].append(d)
dist = [float('inf')] * V
dist[root] = 0
h = [(0, root)]
while h:
	(c, v) = heapq.heappop(h)
	if visited[v]:
		continue
	visited[v] = True
	for i in range(len(nodes[v])):
		t = nodes[v][i]
		if dist[t] > c + cost[v][i]:
			dist[t] = c + cost[v][i]
			heapq.heappush(h, (c + cost[v][i], t))
for i in range(V):
	if dist[i] == float('inf'):
		print('INF')
	else:
		print(dist[i])


(V, E, r) = map(int, input().split())
paths = []
for i in range(E):
	(s, t, d) = map(int, input().split())
	paths.append([s, t, d])
INF = 10 ** 20
n = [INF for x in range(V)]
n[r] = 0
while True:
	break_flg = True
	for p in paths:
		if n[p[0]] + p[2] < n[p[1]]:
			n[p[1]] = min(n[p[1]], n[p[0]] + p[2])
			break_flg = False
	if break_flg == True:
		break
for i in n:
	if i == INF:
		print('INF')
	else:
		print(i)


from collections import defaultdict
from collections import deque
from sys import stdin

def sp(G, R, V):
	d = {}
	INF = float('inf')
	for i in range(V):
		d[i] = INF
	d[R] = 0
	q = deque([(0, R)])
	while q:
		(cost, v) = q.popleft()
		for (u, c) in G[v].items():
			if d[u] > d[v] + c:
				d[u] = d[v] + c
				q.append((d[u], u))
	return d
(V, E, R) = [int(x) for x in stdin.readline().split()]
G = defaultdict(dict)
for case in range(E):
	(s, t, w) = [int(x) for x in stdin.readline().split()]
	G[s][t] = w
d = sp(G, R, V)
for k in range(V):
	if d[k] == float('inf'):
		print('INF')
	else:
		print(d[k])


from collections import defaultdict
from heapq import heappush, heappop, heapify
from sys import stdin

def sp(G, R, V):
	d = {}
	INF = float('inf')
	for i in range(V):
		d[i] = INF
	q = [(0, R)]
	d[R] = 0
	heapify(q)
	while q:
		(cost, v) = heappop(q)
		for (next, c) in G[v].items():
			if d[next] > d[v] + c:
				d[next] = d[v] + c
				heappush(q, (d[next], next))
	return d
(V, E, R) = [int(x) for x in stdin.readline().split()]
G = defaultdict(dict)
for case in range(E):
	(s, t, w) = [int(x) for x in stdin.readline().split()]
	G[s][t] = w
d = sp(G, R, V)
for k in range(V):
	if d[k] == float('inf'):
		print('INF')
	else:
		print(d[k])


from collections import defaultdict
from heapq import heappush, heappop, heapify
from sys import stdin

def sp(G, R, V):
	d = {}
	q = [(0, R)]
	heapify(q)
	seen = set()
	while q:
		(cost, v) = heappop(q)
		if v not in seen:
			seen.add(v)
			d[v] = cost
			for (next, c) in G[v].items():
				heappush(q, (cost + c, next))
	return d
(V, E, R) = [int(x) for x in stdin.readline().split()]
G = defaultdict(dict)
for case in range(E):
	(s, t, w) = [int(x) for x in stdin.readline().split()]
	G[s][t] = w
d = sp(G, R, V)
for k in range(V):
	if k not in d:
		print('INF')
	else:
		print(d[k])


import heapq

def solve(V, E, r, adjlist):
	cost = [float('inf')] * V
	used = [False] * V
	cost[r] = 0
	used[r] = True
	h = []
	for v in adjlist[r]:
		heapq.heappush(h, v)
	while len(h) > 0:
		u = heapq.heappop(h)
		if used[u[1]]:
			continue
		used[u[1]] = True
		cost[u[1]] = u[0]
		for (d, t) in adjlist[u[1]]:
			if cost[t] > u[0] + d:
				cost[t] = u[0] + d
				heapq.heappush(h, tuple((cost[t], t)))
	return cost
(V, E, r) = map(int, input().split())
adjlist = [[] for _ in range(V)]
for _ in range(E):
	(s, t, d) = map(int, input().split())
	adjlist[s].append(tuple((d, t)))
ans = solve(V, E, r, adjlist)
for i in range(V):
	if ans[i] == float('inf'):
		print('INF')
	else:
		print(ans[i])


from heapq import heapify, heappop, heappush
import sys
input = sys.stdin.readline
(V, E, r) = map(int, input().split())
G = [[] for _ in [0] * V]
for _ in [0] * E:
	(a, b, c) = map(int, input().split())
	G[a].append((b, c))
INF = 1 << 30
D = [INF] * V
used = [0] * V
q = []
heappush(q, (0, r))
while q:
	(d, v) = heappop(q)
	if used[v]:
		continue
	used[v] = 1
	D[v] = d
	for (u, c) in G[v]:
		if used[u]:
			continue
		if d + c < D[u]:
			heappush(q, (d + c, u))
for i in range(V):
	print(D[i] if D[i] != INF else 'INF')


(V, E, r) = map(int, input().split())
edges = []
for _ in range(E):
	tmp = [int(x) for x in input().split()]
	edges.append(tmp)
d = [float('inf') for _ in range(V)]
d[r] = 0
for _ in range(V - 1):
	count = 0
	for x in edges:
		(s, t, w) = x
		if d[t] > d[s] + w:
			count += 1
			d[t] = d[s] + w
	if count == 0:
		break
for x in d:
	if x == float('inf'):
		print('INF')
	else:
		print(x)


icase = 0
if icase == 0:
	(vv, e, r) = map(int, input().split())
	g = [[] for i in range(vv)]
	for i in range(e):
		(si, ti, di) = map(int, input().split())
		g[si].append((ti, di))
elif icase == 1:
	(vv, e, r) = (4, 5, 0)
	g = [[] for _ in range(vv)]
	g[0] = [(1, 1), (2, 4)]
	g[1] = [(2, 2), (3, 5)]
	g[2] = [(3, 1)]
elif icase == 2:
	(vv, e, r) = (4, 6, 1)
	g = [[] for _ in range(vv)]
	g[0] = [(1, 1), (2, 4)]
	g[1] = [(2, 2)]
	g[2] = [(0, 1)]
	g[3] = [(1, 1), (2, 5)]
from heapq import heappop, heappush
q = []
heappush(q, (0, r))
inf = float('INF')
dist = [inf] * vv
dist[r] = 0
while len(q) > 0:
	(dd, state) = heappop(q)
	for (v, dv) in g[state]:
		if dist[v] > dist[state] + dv:
			dist[v] = dist[state] + dv
			heappush(q, (dist[v], v))
for i in range(vv):
	if dist[i] == inf:
		print('INF')
	else:
		print(dist[i])


import heapq

def dijkstra(g, s):
	n = len(g)
	dist = [10 ** 9] * n
	dist[s] = 0
	hq = []
	heapq.heappush(hq, (dist[s], s))
	while hq:
		(cost, u) = heapq.heappop(hq)
		if dist[u] < cost:
			continue
		for (v, c) in g[u]:
			if dist[v] <= dist[u] + c:
				continue
			dist[v] = dist[u] + c
			heapq.heappush(hq, (dist[v], v))
	return dist
(v, e, r) = map(int, input().split())
g = [[] for _ in range(v)]
for i in range(e):
	(s, t, c) = map(int, input().split())
	g[s].append((t, c))
if e == 0:
	for i in range(v):
		if i == r:
			print(i)
		else:
			print('INF')
else:
	rets = dijkstra(g, r)
	for item in range(v):
		if rets[item] == 10 ** 9:
			print('INF')
		else:
			print(rets[item])


from collections import defaultdict
import heapq
(vertice, edge, source) = map(int, input().split(' '))
link = defaultdict(dict)
weight = defaultdict(dict)
answer = [-1] * vertice
for e in range(edge):
	(i, j, w) = map(int, input().split(' '))
	link[i] = link.get(i, set()) | {j}
	weight[i].update({j: w})
queue = []
heapq.heappush(queue, (0, source))
heapq.heapify(queue)
while queue:
	(now_cost, here) = heapq.heappop(queue)
	if answer[here] > -1:
		continue
	answer[here] = now_cost
	for nw in link[here]:
		if answer[nw] == -1:
			heapq.heappush(queue, (now_cost + weight[here][nw], nw))
for a in answer:
	print(a if a != -1 else 'INF')


import heapq
import sys
input = sys.stdin.readline

def dijkstra_heap(s, edge):
	d = [10 ** 20] * n
	used = [True] * n
	d[s] = 0
	used[s] = False
	edgelist = []
	for (a, b) in edge[s]:
		heapq.heappush(edgelist, a * 10 ** 6 + b)
	while len(edgelist):
		minedge = heapq.heappop(edgelist)
		if not used[minedge % 10 ** 6]:
			continue
		v = minedge % 10 ** 6
		d[v] = minedge // 10 ** 6
		used[v] = False
		for e in edge[v]:
			if used[e[1]]:
				heapq.heappush(edgelist, (e[0] + d[v]) * 10 ** 6 + e[1])
	return d
(n, w, r) = map(int, input().split())
edge = [[] for i in range(n)]
for i in range(w):
	(x, y, z) = map(int, input().split())
	edge[x].append([z, y])
D = dijkstra_heap(r, edge)
for index in range(n):
	if D[index] == 10 ** 20:
		D[index] = 'INF'
	print(D[index])


from heapq import *
(V, E, r) = map(int, input().split())
graph = [[] for _ in range(V)]
for _ in range(E):
	(s, t, d) = map(int, input().split())
	graph[s].append([d, t])
dist = [10 ** 18] * V
dist[r] = 0
pq = []
heappush(pq, (dist[r], r))
while pq:
	(d, v) = heappop(pq)
	if dist[v] < d:
		continue
	for (w, nv) in graph[v]:
		if dist[nv] > dist[v] + w:
			dist[nv] = dist[v] + w
			heappush(pq, (dist[nv], nv))
for di in dist:
	if di == 10 ** 18:
		print('INF')
	else:
		print(di)


from heapq import heappush, heappop
INF = 10 ** 9

def dijkstra(s, n):
	dist = [INF] * n
	hq = [(0, s)]
	dist[s] = 0
	seen = [False] * n
	while hq:
		v = heappop(hq)[1]
		seen[v] = True
		for (to, cost) in adj[v]:
			if seen[to] == False and dist[v] + cost < dist[to]:
				dist[to] = dist[v] + cost
				heappush(hq, (dist[to], to))
	return dist
(v, e, r) = map(int, input().split())
adj = [[] for _ in range(v)]
for i in range(e):
	(s, t, d) = map(int, input().split())
	adj[s].append((t, d))
d = dijkstra(r, v)
for i in d:
	if i != 1000000000:
		print(i)
	else:
		print('INF')


import sys, queue
INF = 10 ** 20

def LI():
	return [int(x) for x in sys.stdin.readline().split()]
(V, E, r) = LI()
dist = [[] for _ in range(V)]
for _ in range(E):
	(a, b, d) = LI()
	dist[a].append((b, d))
short = [INF for _ in range(V)]
short[r] = 0
q = queue.PriorityQueue()
q.put((0, r))
while not q.empty():
	(dt, n) = q.get()
	if dt > short[n]:
		continue
	for (p, d) in dist[n]:
		if short[n] + d < short[p]:
			short[p] = short[n] + d
			q.put((short[p], p))
for i in short:
	print(i if i < INF else 'INF')


import heapq
inf = 10 ** 9

def dijkstra():
	(V, E, r) = map(int, input().split())
	color = ['white' for _ in range(V)]
	D = [inf for _ in range(V)]
	M = [[] for _ in range(V)]
	D[r] = 0
	color[r] = 'gray'
	queue = [[0, r]]
	heapq.heapify(queue)
	for i in range(E):
		(s, t, d) = map(int, input().split())
		M[s].append([d, t])
	while len(queue) != 0:
		u = heapq.heappop(queue)[1]
		if color[u] == 'black':
			continue
		color[u] = 'black'
		V = [[i, j] for (i, j) in M[u] if color[j] != 'black']
		if len(V) > 0:
			for (i, j) in V:
				if D[u] + i < D[j]:
					D[j] = D[u] + i
					color[j] = 'gray'
					heapq.heappush(queue, [D[j], j])
	return D
D = dijkstra()
for i in D:
	if i == inf:
		print('INF')
	else:
		print(i)


from collections import defaultdict
from heapq import heappop, heappush
from math import isinf
(V, E, r) = map(int, input().split())
e = defaultdict(list)
for i in range(E):
	(s, t, length) = map(int, input().split())
	e[s].append((length, t))
distance = [float('inf')] * V
distance[r] = 0
heap = [(distance[r], r)]
while heap:
	(du, u) = heappop(heap)
	for (length, v) in e[u]:
		if distance[v] > distance[u] + length:
			distance[v] = distance[u] + length
			heappush(heap, (distance[v], v))
for d in distance:
	print('INF' if isinf(d) else d)


from heapq import heappush, heappop
import sys

def LI():
	return [int(x) for x in sys.stdin.readline().split()]

def main():
	(V, E, r) = LI()
	INF = 10 ** 12
	if V == 1:
		print(0)
		exit()
	adj = [[] for _ in range(V)]
	for i in range(E):
		(s, t, d) = LI()
		adj[s].append((t, d))
	q = [(0, r)]
	C = [INF] * V
	C[r] = 0
	while len(q) > 0:
		(cost, u) = heappop(q)
		for (v, d) in adj[u]:
			tmp = C[u] + d
			if tmp < C[v]:
				C[v] = tmp
				heappush(q, (C[v], v))
	for c in C:
		if c == INF:
			print('INF')
		else:
			print(c)
main()


from heapq import heapify, heappush, heappop
inf = float('inf')

def dijkstra(graph, start):
	vsize = len(graph)
	dist = [inf] * vsize
	seen = [False] * vsize
	pq = []
	heapify(pq)
	dist[start] = 0
	heappush(pq, (0, start))
	while pq:
		(cost, u) = heappop(pq)
		seen[u] = True
		if dist[u] < cost:
			continue
		for (v, w) in graph[u]:
			temp_cost = dist[u] + w
			if not seen[v] and temp_cost < dist[v]:
				dist[v] = temp_cost
				heappush(pq, (dist[v], v))
	return dist
(V, E, r) = map(int, input().split())
graph = [[] for _ in range(V)]
for _ in range(E):
	(s, t, d) = map(int, input().split())
	graph[s].append((t, d))
for min_cost in dijkstra(graph, r):
	print(min_cost if min_cost != inf else 'INF')


import heapq

def dijkstra(start: int, graph: list) -> list:
	INF = 10 ** 18
	n = len(graph)
	dist = [INF] * n
	dist[start] = 0
	q = [(0, start)]
	while q:
		(d, v) = heapq.heappop(q)
		if dist[v] < d:
			continue
		for (nxt_v, cost) in graph[v]:
			if dist[v] + cost < dist[nxt_v]:
				dist[nxt_v] = dist[v] + cost
				heapq.heappush(q, (dist[nxt_v], nxt_v))
	return dist

def main():
	(n, m, s) = map(int, input().split())
	info = [list(map(int, input().split())) for i in range(m)]
	INF = 10 ** 18
	graph = [[] for i in range(n)]
	for (a, b, cost) in info:
		graph[a].append((b, cost))
	dist = dijkstra(s, graph)
	for i in range(n):
		if dist[i] == INF:
			print('INF')
		else:
			print(dist[i])
main()


(V, E, r) = map(int, input().split())
edges = []
for _ in range(E):
	tmp = [int(x) for x in input().split()]
	edges.append(tmp)
d = [float('inf') for _ in range(V)]
d[r] = 0
for _ in range(V - 1):
	count = 0
	for x in edges:
		(s, t, w) = x
		if d[t] > d[s] + w:
			count += 1
			d[t] = d[s] + w
	if count == 0:
		break
for x in d:
	if x == float('inf'):
		print('INF')
	else:
		print(x)


import heapq
import sys
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 7)
(N, M, r) = map(int, input().split())
edge = [[] for _ in range(N)]
for _ in range(M):
	(s, t, d) = map(int, input().split())
	edge[s].append((t, d))
inf = 10 ** 18
dist = [inf] * N
dist[r] = 0
que = [(0, r)]
while que:
	(c, s) = heapq.heappop(que)
	if dist[s] < c:
		continue
	d = dist[s]
	for (t, cost) in edge[s]:
		if dist[t] > d + cost:
			dist[t] = d + cost
			heapq.heappush(que, (d + cost, t))
for i in range(N):
	if dist[i] >= inf:
		print('INF')
	else:
		print(dist[i])


(n, m, s) = map(int, input().split())
edge = [[] for _ in range(n)]
for _ in range(m):
	(a, b, c) = map(int, input().split())
	edge[a].append((b, c))
from heapq import heappop, heappush
ans = [10 ** 20] * n
ans[s] = 0
h = [[0, s]]
while h:
	(c, v) = heappop(h)
	if ans[v] < c:
		continue
	for (u, t) in edge[v]:
		if c + t < ans[u]:
			ans[u] = c + t
			heappush(h, [c + t, u])
for i in ans:
	if i == 10 ** 20:
		print('INF')
	else:
		print(i)


from collections import defaultdict
from collections import deque
from sys import stdin

def sp(G, R, V):
	d = {}
	INF = float('inf')
	for i in range(V):
		d[i] = INF
	d[R] = 0
	q = deque([R])
	while q:
		u = q.popleft()
		for v in G[u]:
			if d[v[0]] > d[u] + v[1]:
				d[v[0]] = d[u] + v[1]
				q.append(v[0])
	return d
(V, E, R) = tuple((int(x) for x in stdin.readline().split(' ')))
G = defaultdict(list)
for case in range(E):
	(s, t, w) = tuple((int(x) for x in stdin.readline().split(' ')))
	G[s].append((t, w))
d = sp(G, R, V)
for k in range(V):
	if d[k] == float('inf'):
		print('INF')
	else:
		print(d[k])


(v, e, r) = map(int, input().split())
d = [float('INF')] * v
d[r] = 0
edges = []
for i in range(e):
	(s, t, c) = map(int, input().split())
	edges.append([s, t, c])
for i in range(v):
	count = 0
	for (s, v, c) in edges:
		if d[v] > d[s] + c:
			count += 1
			d[v] = d[s] + c
	if count == 0:
		break
for i in d:
	if i == float('inf'):
		print('INF')
	else:
		print(i)


from collections import defaultdict
import heapq
(V, E, r) = map(int, input().split())
edges = defaultdict(list)
for _ in range(E):
	(s, t, d) = map(int, input().split())
	edges[s].append((t, d))
dists = {}
q = [(0, r)]
while q:
	(dist, node) = heapq.heappop(q)
	if node in dists:
		continue
	dists[node] = dist
	for (t, d) in edges[node]:
		if t not in dists:
			heapq.heappush(q, (dist + d, t))
for i in range(V):
	if i in dists:
		print(dists[i])
	else:
		print('INF')


import heapq
(num_V, num_E, r) = map(int, input().split())
E = [[] for _ in range(num_V)]
for _ in range(num_E):
	(ts, tt, td) = map(int, input().split())
	E[ts].append([tt, td])
q = []
heapq.heappush(q, [0, r])
flg = [-1] * num_V
while q:
	(cd, cp) = heapq.heappop(q)
	if flg[cp] == -1:
		flg[cp] = cd
	else:
		continue
	for (np, dist) in E[cp]:
		if flg[np] != -1:
			continue
		heapq.heappush(q, [cd + dist, np])
for d in flg:
	if d == -1:
		print('INF')
	else:
		print(d)


import heapq
import math

def main():
	(nvertices, nedges, s) = map(int, input().split())
	edges = [[] for i in range(nvertices)]
	for i in range(nedges):
		(u, v, w) = map(int, input().split())
		edges[u].append((v, w))
	INF = float('inf')
	hq = [(0, s)]
	weights = [INF] * nvertices
	weights[s] = 0
	while hq:
		(d, u) = heapq.heappop(hq)
		if d > weights[u]:
			continue
		for (v, w) in edges[u]:
			if weights[u] + w < weights[v]:
				weights[v] = weights[u] + w
				heapq.heappush(hq, (weights[v], v))
	for w in weights:
		print('INF' if math.isinf(w) else w)
main()


from heapq import *

def dijkstra(s, n, w, cost):
	ans = [float('inf')] * n
	d = [[float('inf'), i] for i in range(n)]
	used = [False] * n
	ans[s] = 0
	d[s][0] = 0
	heapify(d)
	while d:
		(D, V) = heappop(d)
		if used[V]:
			continue
		used[V] = True
		for (t, distance) in cost[V]:
			ans[t] = min(ans[t], D + distance)
			heappush(d, [ans[t], t])
	return ans
(v, e, r) = map(int, input().split())
route_list = [[] for i in range(v)]
for i in range(e):
	(s, t, d) = map(int, input().split())
	route_list[s].append((t, d))
ans = dijkstra(r, v, e, route_list)
for i in ans:
	if i == float('inf'):
		print('INF')
	else:
		print(i)


(V, E, r) = map(int, input().split())
graph = {i: {} for i in range(V)}
visit = [False for _ in range(V)]
min_d = [(10 ** 18, i) for i in range(V)]
distance = [10 ** 18 for i in range(V)]
import heapq
for i in range(E):
	(n1, n2, c) = map(int, input().split())
	graph[n1][n2] = c

def dijkstra(s):
	distance[s] = 0
	min_d[s] = (0, s)
	heapq.heapify(min_d)
	while len(min_d):
		(m, idx) = heapq.heappop(min_d)
		if visit[idx] == True:
			continue
		distance[idx] = m
		visit[idx] = True
		for j in graph[idx].keys():
			heapq.heappush(min_d, (distance[idx] + graph[idx][j], j))
	return distance
min_d = dijkstra(r)
for m in min_d:
	if m == 10 ** 18:
		print('INF')
	else:
		print(m)


from collections import deque
(V, E, r) = map(int, input().split())
dlis = [[] for _ in range(V)]
mind = [10 ** 10] * V
for _ in range(E):
	std = list(map(int, input().split()))
	dlis[std[0]].append([std[1], std[2]])
q = deque()
q.append([r, 0])
while len(q) > 0:
	v = q.popleft()
	a = v[0]
	b = v[1]
	if mind[a] <= b:
		continue
	mind[a] = b
	for el in dlis[a]:
		aa = el[0]
		bb = b + el[1]
		if mind[aa] > bb:
			q.append([aa, bb])
for i in range(V):
	if mind[i] == 10 ** 10:
		print('INF')
	else:
		print(mind[i])


import sys
import heapq
(V, E, r) = map(int, sys.stdin.readline().strip().split())
G = [[] for _ in range(V)]
for e in range(E):
	(s, t, d) = map(int, sys.stdin.readline().strip().split())
	G[s].append((t, d))
distance = ['INF'] * V
distance[r] = 0
que = []
for (u, d_u) in G[r]:
	heapq.heappush(que, (d_u, u))
while que:
	(d_tot, u) = heapq.heappop(que)
	if distance[u] is not 'INF':
		continue
	distance[u] = d_tot
	for (v, d_v) in G[u]:
		if distance[v] is not 'INF':
			continue
		heapq.heappush(que, (d_tot + d_v, v))
for i in distance:
	print(i)


import sys
from heapq import heappush
from heapq import heappop
from collections import deque
(n_V, n_E, r) = map(int, input().split())
E = [[] for _ in range(n_V)]
for i in range(n_E):
	(v, e, c) = map(int, input().split())
	E[v].append((e, c))
D = [sys.maxsize for _ in range(n_V)]
D[r] = 0
h = []
heappush(h, (D[r], r))
while len(h) != 0:
	(_, index) = heappop(h)
	for (e, c) in E[index]:
		d = D[index] + c
		if D[e] > d:
			D[e] = d
			heappush(h, (d, e))
for d in D:
	if d == sys.maxsize:
		print('INF')
	else:
		print(d)


from heapq import *
import sys
I = sys.stdin.readlines()
(V, E, r) = map(int, I[0].split())
(s, t, d) = (0, 0, 0)
G = [[] for i in range(V)]
for i in range(E):
	(s, t, d) = map(int, I[i + 1].split())
	G[s].append((t, d))
D = [0] * V
INF = 10 ** 9 + 1

def ijk(s):
	global V, D, G, INF
	Q = []
	heapify(Q)
	for i in range(V):
		D[i] = INF
	D[s] = 0
	(v, p, e) = (0, 0, 0)
	heappush(Q, (0, s))
	while len(Q):
		p = heappop(Q)
		v = p[1]
		if D[v] < p[0]:
			continue
		for i in range(len(G[v])):
			e = G[v][i]
			if D[e[0]] > D[v] + e[1]:
				D[e[0]] = D[v] + e[1]
				heappush(Q, (D[e[0]], e[0]))
ijk(r)
for i in range(V):
	if D[i] == INF:
		print('INF')
	else:
		print(D[i])


(V, E, r) = map(int, input().split())
W = [tuple(map(int, input().split())) for _ in range(E)]
dis = [float('inf')] * V
dis[r] = 0
for _ in range(V - 1):
	update = False
	for (s, t, d) in W:
		if dis[t] > dis[s] + d:
			dis[t] = dis[s] + d
			update = True
	if not update:
		break
for i in dis:
	print(i if i != float('inf') else 'INF')


import sys
import heapq

def Dj(G, s):
	n = len(G)
	dist = [float('inf') for i in range(n)]
	vis = [-1 for i in range(n)]
	li = [(0, s)]
	dist[s] = 0
	heapq.heapify(li)
	while len(li):
		(d, u) = heapq.heappop(li)
		if vis[u] == 1:
			continue
		vis[u] = 1
		for v in G[u]:
			if dist[v[1]] > dist[u] + v[0]:
				dist[v[1]] = dist[u] + v[0]
				heapq.heappush(li, (dist[u] + v[0], v[1]))
	return dist

def main():
	T = [int(x) for x in sys.stdin.readline().strip().split()]
	V = T[0]
	E = T[1]
	S = T[2]
	G = [[] for i in range(V)]
	for i in range(E):
		line = [int(x) for x in sys.stdin.readline().strip().split()]
		G[line[0]].append((line[2], line[1]))
	for k in Dj(G, S):
		if k == float('inf'):
			print('INF')
		else:
			print(k)
main()


import sys
import heapq
input = sys.stdin.readline

class Node:

	def __init__(self):
		self.link = []
		self.costs = []
		self.check = False
		self.ans = -1

def main():
	(V, E, r) = map(int, input().split())
	nodes = [Node() for i in range(V)]
	for i in range(E):
		(s, t, d) = map(int, input().split())
		nodes[s].link.append(t)
		nodes[s].costs.append(d)
	q = []
	heapq.heappush(q, (0, r))
	while len(q) > 0:
		(cost, num) = heapq.heappop(q)
		node = nodes[num]
		if node.check:
			continue
		node.check = True
		node.ans = cost
		for (next_num, p_cost) in zip(node.link, node.costs):
			if not nodes[next_num].check:
				heapq.heappush(q, (cost + p_cost, next_num))
	for i in range(V):
		print(nodes[i].ans if nodes[i].ans != -1 else 'INF')
main()


from heapq import heappush, heappop
(v, e, r) = map(int, input().split())
edge = [[] for _ in range(v)]
for _ in range(e):
	(s, t, d) = map(int, input().split())
	edge[s].append((t, d))
cost = [float('inf')] * v
cost[r] = 0
q = [(r, 0)]
while q:
	(now, distance) = heappop(q)
	for (nxt, newcost) in edge[now]:
		if distance + newcost < cost[nxt]:
			cost[nxt] = distance + newcost
			heappush(q, (nxt, distance + newcost))
for i in range(v):
	if cost[i] == float('inf'):
		print('INF')
	else:
		print(cost[i])


(v, e, r) = map(int, input().split())
vertex = [{'cost': float('inf'), 'dest': []} for _ in range(v)]
vertex[r]['cost'] = 0
for _ in range(e):
	(v_id, dest, cost) = map(int, input().split())
	vertex[v_id]['dest'].append((dest, cost))
import heapq
q = []
heapq.heapify(q)
heapq.heappush(q, (0, r))
while len(q) != 0:
	(distance, v) = heapq.heappop(q)
	if vertex[v]['cost'] < distance:
		continue
	for dest in vertex[v]['dest']:
		if vertex[dest[0]]['cost'] > vertex[v]['cost'] + dest[1]:
			c = vertex[v]['cost'] + dest[1]
			vertex[dest[0]]['cost'] = c
			heapq.heappush(q, (c, dest[0]))
for v in vertex:
	print(v['cost'] if v['cost'] != float('inf') else 'INF')


import heapq
(v, e, r) = map(int, input().split())
g = [[] for _ in range(v)]
for i in range(e):
	(s, t, d) = map(int, input().split())
	g[s].append((d, t))
dist = [float('inf')] * v
visited = [False] * v
dist[r] = 0
visited[r] = True
candidate = []
for i in g[r]:
	heapq.heappush(candidate, i)
while len(candidate):
	minedge = heapq.heappop(candidate)
	if visited[minedge[1]]:
		continue
	c = minedge[1]
	dist[c] = minedge[0]
	visited[c] = True
	for i in g[c]:
		if not visited[i[1]]:
			heapq.heappush(candidate, (dist[c] + i[0], i[1]))
for i in dist:
	if i == float('inf'):
		print('INF')
	else:
		print(i)


from heapq import heappop, heappush
(V, E, r) = map(int, input().split())
edge = [[] for _ in range(V)]
for _ in range(E):
	(s, t, d) = map(int, input().split())
	edge[s].append((d, t))
dp = [-1] * V
q = []
heappush(q, (0, r))
while len(q) > 0:
	(d, x) = heappop(q)
	if dp[x] > -1:
		continue
	dp[x] = d
	for (nd, nt) in edge[x]:
		heappush(q, (d + nd, nt))
for i in dp:
	print(i if i > -1 else 'INF')


import heapq
(V, E, r) = map(int, input().split())
Graph = [[] for _ in range(V)]
for _ in range(E):
	(s, t, d) = map(int, input().split())
	Graph[s].append((t, d))
pq = []
dist = [10 ** 10] * V
dist[r] = 0
heapq.heappush(pq, (0, r))
while pq:
	(w, n) = heapq.heappop(pq)
	for (node, weight) in Graph[n]:
		newlen = w + weight
		if newlen < dist[node]:
			dist[node] = newlen
			heapq.heappush(pq, (dist[node], node))
for i in dist:
	if i == 10 ** 10:
		print('INF')
	else:
		print(i)


(N, M, S) = map(int, input().split())
G = [[] for i in range(N)]
for i in range(M):
	(s, t, d) = map(int, input().split())
	G[s].append([t, d])

def dijkstra(graph, start, inf=float('inf')):
	import heapq
	n = len(graph)
	distances = [inf] * n
	distances[start] = 0
	visited = [False] * n
	hq = [(0, start)]
	while hq:
		(dist, fr) = heapq.heappop(hq)
		visited[fr] = True
		for (to, cost) in graph[fr]:
			new_dist = distances[fr] + cost
			if visited[to] or distances[to] <= new_dist:
				continue
			distances[to] = new_dist
			heapq.heappush(hq, (new_dist, to))
	return distances
ans = dijkstra(G, S)
for a in ans:
	print(a if a != float('inf') else 'INF')


import heapq
(V, E, r) = map(int, input().split())
adj = [[] for _ in range(V)]
for _ in range(E):
	(u, v, w) = map(int, input().split())
	adj[u].append((v, w))

def dijkstra():
	dist = [float('inf')] * V
	dist[r] = 0
	visited = [False] * V
	pq = [(0, r)]
	while pq:
		(w, n) = heapq.heappop(pq)
		visited[n] = True
		for (nei, wei) in adj[n]:
			if visited[nei]:
				continue
			if w + wei < dist[nei]:
				dist[nei] = w + wei
				heapq.heappush(pq, (w + wei, nei))
	return dist
for d in dijkstra():
	if d == float('inf'):
		print('INF')
	else:
		print(d)


import heapq
(n, m, r) = map(int, input().split())
edge = [[] for i in range(n)]
for i in range(m):
	(u, v, a) = map(int, input().split())
	edge[u].append([a, v])
inf = 1145141919810364364
start = 10 ** 15
d = [inf] * n
used = [False] * n
d[r] = 0
used[r] = True
lis = []
for (k, l) in edge[r]:
	heapq.heappush(lis, k * 10 ** 6 + l)
while lis:
	minedge = heapq.heappop(lis)
	if used[minedge % 10 ** 6]:
		continue
	v = minedge % 10 ** 6
	d[v] = minedge // 10 ** 6
	used[v] = True
	for q in edge[v]:
		if not used[q[1]]:
			heapq.heappush(lis, (q[0] + d[v]) * 10 ** 6 + q[1])
for i in d:
	if i >= inf:
		print('INF')
	else:
		print(i)


V_MAX = 100000
E_MAX = 500000
INF = float('inf')
(v, e, r) = (int(x) for x in input().split())
e_from = []
e_to = []
e_cost = []
dist = []
for i in range(e):
	(f, t, c) = (int(x) for x in input().split())
	e_from.append(f)
	e_to.append(t)
	e_cost.append(c)
for i in range(v):
	dist.append(INF)
dist[r] = 0
while True:
	flag = 1
	for i in range(e):
		if dist[e_from[i]] != INF and dist[e_to[i]] > dist[e_from[i]] + e_cost[i]:
			dist[e_to[i]] = dist[e_from[i]] + e_cost[i]
			flag = 0
	if flag:
		break
for i in range(v):
	if dist[i] == INF:
		print('INF')
	else:
		print(dist[i])


from heapq import heappush, heappop
INF = float('inf')

def dijkstra(s, n, adj):
	dist = [INF] * n
	hq = [(0, s)]
	dist[s] = 0
	seen = [False] * n
	while hq:
		v = heappop(hq)[1]
		seen[v] = True
		for (to, cost) in adj[v]:
			if seen[to] == False and dist[v] + cost < dist[to]:
				dist[to] = dist[v] + cost
				heappush(hq, (dist[to], to))
	return dist
(v, e, r) = map(int, input().split())
adj = [[] for _ in range(v)]
for i in range(e):
	(s, t, d) = map(int, input().split())
	adj[s].append((t, d))
ans = dijkstra(r, v, adj)
for i in ans:
	if i == float('inf'):
		print('INF')
	else:
		print(i)


from sys import stdin
from collections import defaultdict, deque
import math
readline = stdin.readline
(v, e, r) = map(int, readline().split())
g = defaultdict(list)
for i in range(e):
	(s, t, length) = map(int, readline().split())
	g[s].append((length, t))
d = [float('inf')] * v
d[r] = 0
que = deque([(d[r], r)])
while que:
	(du, u) = que.popleft()
	for (length, v) in g[u]:
		if d[v] > du + length:
			d[v] = du + length
			que.append((d[v], v))
for di in d:
	print('INF' if math.isinf(di) else di)


from collections import defaultdict

def main():
	fl = input().split(' ')
	V = int(fl[0])
	E = int(fl[1])
	R = int(fl[2])
	G = defaultdict(list)
	for i in range(int(E)):
		(s, t, w) = [int(x) for x in input().split(' ')]
		G[s].append((t, w))
	d = {}
	INF = float('inf')
	for i in range(V):
		d[i] = INF
	d[R] = 0
	q = [R]
	while q:
		u = q.pop(0)
		for v in G[u]:
			if d[v[0]] > d[u] + v[1]:
				d[v[0]] = d[u] + v[1]
				q.append(v[0])
	for k in d.keys():
		if d[k] == float('inf'):
			print('INF')
		else:
			print(d[k])
main()


import sys
from heapq import heappush, heappop
readline = sys.stdin.readline
from collections import defaultdict
(vertices, edges, start) = map(int, readline().split())
connect = defaultdict(list)
cost = defaultdict(dict)
answer = ['INF' for n in range(vertices)]
for _ in range(edges):
	(s, t, w) = map(int, readline().split())
	connect[s].append(t)
	cost[s].update({t: w})
now = start
weight = 0
p_queue = []
heappush(p_queue, (0, now))
visited = set()
while p_queue:
	(weight, now) = heappop(p_queue)
	if now in visited:
		continue
	visited |= {now}
	answer[now] = weight
	for c in connect[now]:
		if c not in visited:
			heappush(p_queue, (weight + cost[now][c], c))
for a in answer:
	print(a)


import heapq

def main():
	(V, E, r) = map(int, input().split())
	G = [[] for _ in range(V)]
	P = [10000000000.0] * V
	for _ in range(E):
		(s, t, d) = map(int, input().split())
		G[s].append((d, t))
	q = []
	heapq.heapify(q)
	heapq.heappush(q, (0, r))
	fixed = [0] * V
	while q:
		v = heapq.heappop(q)
		if P[v[1]] > v[0]:
			P[v[1]] = v[0]
		fixed[v[1]] += 1
		for g in G[v[1]]:
			if not fixed[g[1]]:
				dist = g[0] + P[v[1]]
				P[g[1]] = dist
				heapq.heappush(q, (dist, g[1]))
	for p in P:
		if p == 10000000000.0:
			print('INF')
		else:
			print(p)
main()


import heapq

def dijkstra(start_v, G):
	n = len(G)
	dist = [0 if i == start_v else float('inf') for i in range(n)]
	que = []
	heapq.heappush(que, (0, start_v))
	while que:
		(cur_d, cur_v) = heapq.heappop(que)
		if dist[cur_v] < cur_d:
			continue
		for adj in G[cur_v]:
			(nxt_v, cost) = adj
			if dist[cur_v] + cost < dist[nxt_v]:
				dist[nxt_v] = dist[cur_v] + cost
				heapq.heappush(que, (dist[nxt_v], nxt_v))
	return dist
(v, e, r) = map(int, input().split())
g = [[] for _ in range(v)]
for _ in range(e):
	(s, t, d) = map(int, input().split())
	g[s].append((t, d))
dist = dijkstra(r, g)
for i in range(v):
	if dist[i] != float('inf'):
		print(dist[i])
	else:
		print('INF')


from heapq import heappush, heappop
(V, E, r) = map(int, input().split())
inf = 10 ** 10
edges = [[] for _ in [None] * V]
vertices = [inf] * V
vertices[r] = 0
for _ in [None] * E:
	(s, t, d) = map(int, input().split())
	edges[s].append((t, d))
q = [(0, r)]
rem = V - 1
while q:
	(cost, v) = heappop(q)
	if vertices[v] < cost:
		continue
	for (dest, _cost) in edges[v]:
		newcost = cost + _cost
		if vertices[dest] > newcost:
			vertices[dest] = newcost
			heappush(q, (newcost, dest))
	rem -= 1
	if rem == 0:
		break
print('\n'.join([str(n) if n != inf else 'INF' for n in vertices]))


import heapq

def dijkstra(start, edge, n):
	dist = [float('inf')] * n
	dist[start] = 0
	pq = [(dist[start], start)]
	while len(pq):
		(d, frm) = heapq.heappop(pq)
		if dist[frm] < d:
			continue
		for (to, cost) in edge[frm]:
			if dist[frm] + cost < dist[to]:
				dist[to] = dist[frm] + cost
				heapq.heappush(pq, (dist[to], to))
	return dist
(n, m, r) = map(int, input().split())
edge = [[] for _ in range(n)]
for i in range(m):
	(x, y, d) = map(int, input().split())
	edge[x].append((y, d))
dist = dijkstra(r, edge, n)
for d in dist:
	if d == float('inf'):
		print('INF')
	else:
		print(d)


import queue
(v, e, s) = map(int, input().split())
edge = []
inf = 1000000100
for i in range(v):
	edge.append([])
for i in range(e):
	(vs, vt, d) = map(int, input().split())
	edge[vs].append((vt, d))
dis = []
for i in range(v):
	dis.append(inf)
dis[s] = 0
q = queue.PriorityQueue()
q.put((0, s))
finished = 0
while finished < v and (not q.empty()):
	now = q.get()
	nowv = now[1]
	if now[0] > dis[nowv]:
		continue
	for (vt, d) in edge[nowv]:
		if dis[vt] > dis[nowv] + d:
			dis[vt] = dis[nowv] + d
			q.put((dis[vt], vt))
	finished += 1
for i in range(v):
	if dis[i] >= inf:
		print('INF')
	else:
		print(dis[i])


import heapq
(nv, ne, r) = map(int, input().split(' '))
vertices = [[] for i in range(nv)]
for i in range(ne):
	(s, t, w) = map(int, input().split(' '))
	vertices[s].append((t, w))
q = []
heapq.heappush(q, (0, r))
dist = [float('inf')] * nv
dist[r] = 0
while len(q):
	u = heapq.heappop(q)[1]
	for (t, w) in vertices[u]:
		alt = dist[u] + w
		if alt < dist[t]:
			dist[t] = alt
			heapq.heappush(q, (alt, t))
[print(d) if d < float('inf') else print('INF') for d in dist]


from heapq import heappush, heappop
INF = float('inf')

def dijkstra(v, st, g):
	dist = [INF] * v
	dist[st] = 0
	hq = [(st, 0)]
	while hq:
		(u, d) = heappop(hq)
		if dist[u] < d:
			continue
		for (nu, w) in g[u]:
			nw = d + w
			if dist[nu] > nw:
				dist[nu] = nw
				heappush(hq, (nu, nw))
	return dist

def main():
	(v, e, st) = map(int, input().split())
	g = [[] for _ in range(v)]
	for _ in range(e):
		(s, t, d) = map(int, input().split())
		g[s].append((t, d))
	for ans in dijkstra(v, st, g):
		if ans == INF:
			print('INF')
		else:
			print(ans)
main()


import sys
input = sys.stdin.readline
from heapq import *

def dijkstra(sta):
	dist = [10 ** 18] * V
	dist[sta] = 0
	pq = []
	heappush(pq, (dist[sta], sta))
	while pq:
		(d, v) = heappop(pq)
		if dist[v] < d:
			continue
		for (nv, w) in G[v]:
			if dist[nv] > dist[v] + w:
				dist[nv] = dist[v] + w
				heappush(pq, (dist[nv], nv))
	return dist
(V, E, r) = map(int, input().split())
G = [[] for _ in range(V)]
for _ in range(E):
	(s, t, d) = map(int, input().split())
	G[s].append((t, d))
dist = dijkstra(r)
for di in dist:
	if di == 10 ** 18:
		print('INF')
	else:
		print(di)


import heapq
(v, e, r) = map(int, input().split())
g_l = [[] for _ in range(v)]
for i in range(e):
	(_s, _t, _d) = map(int, input().split())
	g_l[_s].append([_t, _d])

def djkstra(s):
	ans_l = [float('INF')] * v
	_l = [[0, s]]
	heapq.heapify(_l)
	while len(_l) > 0:
		(_c, _v) = heapq.heappop(_l)
		if ans_l[_v] < _c:
			continue
		ans_l[_v] = _c
		for (_t, _d) in g_l[_v]:
			if ans_l[_t] > _c + _d:
				heapq.heappush(_l, [_c + _d, _t])
	return ans_l
ans_l = djkstra(r)
for i in range(v):
	print(str(ans_l[i]).upper())


from collections import defaultdict

def main():
	fl = input().split(' ')
	V = int(fl[0])
	E = int(fl[1])
	R = int(fl[2])
	G = defaultdict(dict)
	for i in range(int(E)):
		(s, t, w) = [int(x) for x in input().split(' ')]
		G[s][t] = w
	d = {}
	INF = float('inf')
	for i in range(V):
		d[i] = INF
	d[R] = 0
	q = [R]
	while q:
		u = q.pop(0)
		for v in G[u].keys():
			if d[v] > d[u] + G[u][v]:
				d[v] = d[u] + G[u][v]
				q.append(v)
	for k in range(V):
		if d[k] == float('inf'):
			print('INF')
		else:
			print(d[k])
main()


import heapq
import sys
input = sys.stdin.readline

def dijkstra(start: int, graph: list) -> list:
	INF = 10 ** 18
	n = len(graph)
	dist = [INF] * n
	dist[start] = 0
	q = [(0, start)]
	while q:
		(d, v) = heapq.heappop(q)
		if dist[v] < d:
			continue
		for (nxt_v, cost) in graph[v]:
			if dist[v] + cost < dist[nxt_v]:
				dist[nxt_v] = dist[v] + cost
				heapq.heappush(q, (dist[nxt_v], nxt_v))
	return dist
(n, m, s) = map(int, input().split())
info = [list(map(int, input().split())) for i in range(m)]
INF = 10 ** 18
graph = [[] for i in range(n)]
for (a, b, cost) in info:
	graph[a].append((b, cost))
dist = dijkstra(s, graph)
for i in range(n):
	if dist[i] == INF:
		print('INF')
	else:
		print(dist[i])


import heapq as hp
(V, E, s) = map(int, input().split())
graph = [[] for _ in range(V)]
INF = 0
for _ in range(E):
	(a, b, d) = map(int, input().split())
	graph[a].append((d, b))
	INF = max(INF, d)
INF = INF * (E + 1) + 1
D = [INF for _ in range(V)]
D[s] = 0
q = []
hp.heappush(q, (0, s))
while q:
	(nd, np) = hp.heappop(q)
	if D[np] < nd:
		continue
	for (d, p) in graph[np]:
		if D[p] > D[np] + d:
			D[p] = D[np] + d
			hp.heappush(q, (D[p], p))
for d in D:
	if d == INF:
		print('INF')
	else:
		print(d)


from collections import defaultdict
from collections import deque
from sys import stdin

def sp(G, R, V):
	d = {}
	INF = float('inf')
	for i in range(V):
		d[i] = INF
	d[R] = 0
	q = deque([R])
	while q:
		u = q.popleft()
		for v in G[u].keys():
			if d[v] > d[u] + G[u][v]:
				d[v] = d[u] + G[u][v]
				q.append(v)
	return d
(V, E, R) = [int(x) for x in stdin.readline().split()]
G = defaultdict(dict)
for case in range(E):
	(s, t, w) = [int(x) for x in stdin.readline().split()]
	G[s][t] = w
d = sp(G, R, V)
for k in range(V):
	if d[k] == float('inf'):
		print('INF')
	else:
		print(d[k])


(vn, en, r) = map(int, input().split())
cost = [[] for _ in range(vn)]
for _ in range(en):
	(s, t, d) = map(int, input().split())
	cost[s].append([t, d])
D0 = [float('inf') for i in range(vn)]
import heapq
D0[r] = 0
D = [(0, r)]
heapq.heapify(D)
count = 0
while True:
	v = -1
	if not D:
		break
	(new_c, new_v) = heapq.heappop(D)
	if D0[new_v] < new_c:
		continue
	D0[new_v] = new_c
	for (t, d) in cost[new_v]:
		if D0[t] > new_c + d:
			heapq.heappush(D, (new_c + d, t))
			D0[t] = new_c + d
	count += 1
for i in range(vn):
	if not D0[i] == float('inf'):
		print(D0[i])
		continue
	print('INF')


from heapq import heappush, heappop
INF = 10 ** 18
(n, m, r) = map(int, input().split())
e = [[] for _ in range(n)]
d = [INF] * n
for i in range(m):
	(a, b, c) = map(int, input().split())
	e[a].append((b, c))
q = []
heappush(q, (0, r))
d[r] = 0
while q:
	(dist, node) = heappop(q)
	for (to, cost) in e[node]:
		if d[to] > cost + dist:
			d[to] = cost + dist
			heappush(q, (d[to], to))
for i in d:
	if i != INF:
		print(i)
	else:
		print('INF')


(V, E, r) = map(int, input().split())
edges = [[] for _ in range(V)]
for _ in range(E):
	(s, t, d) = map(int, input().split())
	edges[s].append([d, t])

def dijkstra(s):
	import heapq
	dist = [float('inf')] * V
	fixed = [False] * V
	dist[s] = 0
	fixed[s] = True
	priority_que = []
	for e in edges[s]:
		heapq.heappush(priority_que, e)
	while len(priority_que):
		edge = heapq.heappop(priority_que)
		if fixed[edge[1]]:
			continue
		v = edge[1]
		dist[v] = edge[0]
		fixed[v] = True
		for ne in edges[v]:
			heapq.heappush(priority_que, [ne[0] + dist[v], ne[1]])
	return dist
dist = dijkstra(r)
for dd in dist:
	if dd == float('inf'):
		print('INF')
	else:
		print(dd)


from heapq import heappush, heappop
INF = 10 ** 12
(V, E, r) = map(int, input().split())
edges_lst = [[] for _ in range(V)]
dist_lst = [INF for _ in range(V)]
for _ in range(E):
	(s, t, dist) = map(int, input().split())
	edges_lst[s].append((t, dist))
que = []
heappush(que, (0, r))
dist_lst[r] = 0
while que:
	(dist, node) = heappop(que)
	for (to, cost) in edges_lst[node]:
		if dist_lst[to] > cost + dist:
			dist_lst[to] = cost + dist
			heappush(que, (cost + dist, to))
for dist in dist_lst:
	if dist == INF:
		print('INF')
	else:
		print(dist)


import heapq
import math

def main():
	(nvertices, nedges, s) = map(int, input().split())
	Adj = [[] for i in range(nvertices)]
	for i in range(nedges):
		(u, v, w) = map(int, input().split())
		Adj[u].append((v, w))
	Q = [(0, s)]
	d = [float('inf')] * nvertices
	d[s] = 0
	while Q:
		(w, u) = heapq.heappop(Q)
		if d[u] < w:
			continue
		for (v, cost) in Adj[u]:
			if d[u] + cost < d[v]:
				d[v] = d[u] + cost
				heapq.heappush(Q, (d[v], v))
	for cost in d:
		if math.isinf(cost):
			print('INF')
		else:
			print(cost)
main()


import heapq
(v, e, r) = map(int, input().split())
edges = [[] for _ in range(v)]
for i in range(e):
	(s, t, d) = map(int, input().split())
	edges[s].append([t, d])

def dijkstra(edges, start, num_v):
	dist = [float('INF')] * num_v
	dist[start] = 0
	q = []
	heapq.heappush(q, [0, start])
	while q:
		(_, u) = heapq.heappop(q)
		for i in edges[u]:
			if dist[i[0]] > dist[u] + i[1]:
				dist[i[0]] = dist[u] + i[1]
				heapq.heappush(q, [dist[u] + i[1], i[0]])
	return dist
res = dijkstra(edges, r, v)
for i in range(v):
	if res[i] == float('inf'):
		print('INF')
	else:
		print(res[i])


import heapq
graph = {}
inf = 1
(n, m, s) = map(int, input().split())
for i in range(m):
	(src, nxt, dst) = map(int, input().split())
	if src not in graph:
		graph[src] = {}
	graph[src][nxt] = dst
	inf += dst
cost = [inf] * n
cost[s] = 0
heap = []
heapq.heappush(heap, (0, s))
while len(heap) > 0:
	pair = heapq.heappop(heap)
	(c, v) = (pair[0], pair[1])
	if cost[v] < c:
		continue
	if v not in graph:
		continue
	for (nxt, dst) in graph[v].items():
		if cost[nxt] > cost[v] + dst:
			cost[nxt] = cost[v] + dst
			heapq.heappush(heap, (cost[nxt], nxt))
for c in cost:
	print(c) if c < inf else print('INF')


(v, e, r) = map(int, input().split())
D = [float('inf')] * v
L = []
D[r] = 0
for i in range(e):
	(s, t, d) = map(int, input().split())
	L.append([s, t, d])
for i in range(v - 1):
	update = False
	for j in range(e):
		(a, b, c) = (L[j][0], L[j][1], L[j][2])
		if D[b] > D[a] + c:
			D[b] = D[a] + c
			update = True
	if not update:
		break
for i in range(v):
	if D[i] == float('inf'):
		print('INF')
	else:
		print(D[i])


from heapq import heappush, heappop
import sys
from typing import List, Tuple
(V, E, r) = map(lambda x: int(x), input().split())
edges: List[List[Tuple[int, int]]] = [[] for _ in range(V)]
dists = [sys.maxsize for _ in range(V)]
dists[r] = 0
for _ in range(E):
	(s, t, dist) = map(lambda x: int(x), input().split())
	edges[s].append((t, dist))
que: List[Tuple[int, int]] = []
heappush(que, (0, r))
while que:
	(dist, vertex) = heappop(que)
	for (to, cost) in edges[vertex]:
		if cost + dist < dists[to]:
			dists[to] = cost + dist
			heappush(que, (cost + dist, to))
for dist in dists:
	if sys.maxsize == dist:
		print('INF')
	else:
		print(dist)


import heapq
import math

def main():
	(nvertices, nedges, s) = map(int, input().split())
	edges = [[] for i in range(nvertices)]
	for i in range(nedges):
		(u, v, w) = map(int, input().split())
		edges[u].append((v, w))
	INF = float('inf')
	hq = []
	for i in range(nvertices):
		heapq.heappush(hq, (0 if i == s else INF, i))
	heapq.heapify(hq)
	weights = [INF] * nvertices
	weights[s] = 0
	while hq:
		(d, u) = heapq.heappop(hq)
		if d > weights[u]:
			continue
		for (v, w) in edges[u]:
			if weights[u] + w < weights[v]:
				weights[v] = weights[u] + w
				heapq.heappush(hq, (weights[v], v))
	for w in weights:
		print('INF' if math.isinf(w) else w)
main()


import sys
from collections import defaultdict
from queue import Queue

def main():
	input = sys.stdin.buffer.readline
	(n, m, r) = map(int, input().split())
	graph = defaultdict(list)
	for _ in range(m):
		(s, t, d) = map(int, input().split())
		graph[s].append((t, d))
	que = Queue()
	que.put(r)
	dist = [-1] * n
	dist[r] = 0
	while not que.empty():
		e = que.get()
		dist_now = dist[e]
		for (nxt, cost) in graph[e]:
			dist_nxt = dist[nxt]
			if dist_nxt == -1 or dist_nxt > dist_now + cost:
				dist[nxt] = dist_now + cost
				que.put(nxt)
	for d in dist:
		print(d if d != -1 else 'INF')
main()


import heapq
(V, E, root) = map(int, input().split())
visited = [False] * V
nodes = [[] for i in range(V)]
cost = [[] for i in range(V)]
for i in range(E):
	(s, t, d) = map(int, input().split())
	nodes[s].append(t)
	cost[s].append(d)
dist = [float('inf')] * V
dist[root] = 0
h = [(0, root)]
while h:
	(c, v) = heapq.heappop(h)
	if visited[v]:
		continue
	visited[v] = True
	for i in range(len(nodes[v])):
		t = nodes[v][i]
		nc = c + cost[v][i]
		if dist[t] > nc:
			dist[t] = nc
			heapq.heappush(h, (nc, t))
for i in range(V):
	if dist[i] == float('inf'):
		print('INF')
	else:
		print(dist[i])


(n, m, r) = map(int, input().split())
import heapq

def dijkstra_heap(s, edge):
	d = [float('inf')] * n
	used = [False] * n
	d[s] = 0
	used[s] = True
	edgelist = []
	for (w, v) in edge[s]:
		heapq.heappush(edgelist, w * 10 ** 6 + v)
	while len(edgelist):
		minedge = heapq.heappop(edgelist)
		v = minedge % 10 ** 6
		if used[v]:
			continue
		d[v] = minedge // 10 ** 6
		used[v] = True
		for (w, u) in edge[v]:
			if not used[u]:
				heapq.heappush(edgelist, (d[v] + w) * 10 ** 6 + u)
	return d
edge = [[] for _ in range(n)]
for i in range(m):
	(u, v, w) = map(int, input().split())
	edge[u].append((w, v))
d = dijkstra_heap(r, edge)
for ans in d:
	if ans == float('inf'):
		print('INF')
	else:
		print(ans)


import heapq

def dijkstra(start: int):
	global dist
	global edges
	dist[start] = 0
	q = [(0, start)]
	while q:
		(d_cur, cur) = heapq.heappop(q)
		if dist[cur] < d_cur:
			continue
		for (nex, cost) in edges[cur]:
			if dist[nex] > dist[cur] + cost:
				dist[nex] = dist[cur] + cost
				heapq.heappush(q, (dist[nex], nex))
(V, E, start) = map(int, input().split())
edges = [[] for _ in range(V)]
for _ in range(E):
	(s, t, d) = map(int, input().split())
	edges[s].append((t, d))
dist = [float('inf') for _ in range(V)]
dijkstra(start)
for di in dist:
	if di == float('inf'):
		print('INF')
	else:
		print(di)


from heapq import heappush, heappop
import sys
INF = sys.maxsize
(v_num, e_num, src) = map(int, input().split())
graph = [[] for _ in range(v_num)]
for _ in range(e_num):
	(s, t, dist) = map(int, input().split())
	graph[s].append((t, dist))
heap = []
dst = [INF for _ in range(v_num)]
heappush(heap, (0, src))
dst[src] = 0
while heap:
	(dist, node) = heappop(heap)
	for (to, cost) in graph[node]:
		if dst[to] > cost + dist:
			dst[to] = cost + dist
			heappush(heap, (cost + dist, to))
for dist in dst:
	if dist == INF:
		print('INF')
	else:
		print(dist)


from collections import defaultdict
from heapq import heapify, heappop, heappush
INF = 10 ** 11 + 1
(nV, nE, r) = map(int, input().split())
G = defaultdict(list)
for _ in range(nE):
	(a, b, c) = map(int, input().split())
	G[a].append((b, c))
d = [INF for _ in range(nV)]
d[r] = 0
queue = [(0, r)]
while queue:
	(c, v) = heappop(queue)
	for (u, cost) in G[v]:
		if d[v] + cost < d[u]:
			d[u] = d[v] + cost
			heappush(queue, (d[u], u))
for dd in d:
	if dd == INF:
		print('INF')
	else:
		print(dd)


import heapq
(V, E, r) = map(int, input().split())
G = [[] for i in range(V)]
for i in range(E):
	(s, t, d) = map(int, input().split())
	G[s].append([d, t])
INF = 1000000007
mn = [INF] * V
todo = [[0, r]]
fin = [0] * V
heapq.heapify(todo)
while todo != []:
	v = heapq.heappop(todo)
	if not fin[v[1]]:
		mn[v[1]] = v[0]
		fin[v[1]] = 1
		for i in G[v[1]]:
			if mn[v[1]] + i[0] < mn[i[1]]:
				heapq.heappush(todo, [mn[v[1]] + i[0], i[1]])
for i in range(V):
	print(mn[i] if mn[i] != INF else 'INF')


from collections import defaultdict
from collections import deque
from sys import stdin

def sp(G, R, V):
	d = {}
	INF = float('inf')
	for i in range(V):
		d[i] = INF
	d[R] = 0
	q = deque([R])
	while q:
		u = q.popleft()
		for v in G[u].keys():
			if d[v] > d[u] + G[u][v]:
				d[v] = d[u] + G[u][v]
				q.append(v)
	return d
(V, E, R) = [int(x) for x in stdin.readline().split(' ')]
G = defaultdict(dict)
for case in range(E):
	(s, t, w) = [int(x) for x in stdin.readline().split(' ')]
	G[s][t] = w
d = sp(G, R, V)
for k in range(V):
	if d[k] == float('inf'):
		print('INF')
	else:
		print(d[k])


import heapq
(V, E, r) = list(map(int, input().split()))
STD = [list(map(int, input().split())) for _ in range(E)]
G = {i: [] for i in range(V)}
for std in STD:
	G[std[0]].append([std[1], std[2]])
dist = [float('inf')] * V
que = [(0, r)]
dist[r] = 0
while que:
	(c, v) = heapq.heappop(que)
	if dist[v] < c:
		continue
	for (t, cost) in G[v]:
		if dist[v] + cost < dist[t]:
			dist[t] = dist[v] + cost
			heapq.heappush(que, (dist[t], t))
for cost in dist:
	if cost == float('inf'):
		print('INF')
	else:
		print(cost)


from heapq import heappush, heapify, heappop
INF = 10 ** 10
(n, m, r) = map(int, input().split())
es = [[] for _ in range(n)]
d = [INF] * n
for i in range(m):
	(a, b, c) = map(int, input().split())
	es[a].append((b, c))

def dijkstra(s):
	v = -1
	que = [(0, s)]
	heapify(que)
	d[s] = 0
	while que:
		p = heappop(que)
		v = p[1]
		if d[v] < p[0]:
			continue
		for e in es[v]:
			if d[e[0]] > d[v] + e[1]:
				d[e[0]] = d[v] + e[1]
				heappush(que, (d[e[0]], e[0]))
dijkstra(r)
for i in d:
	if i == INF:
		print('INF')
	else:
		print(i)


import queue

def Dijkstra(vertex, edges, start):
	weight = [0 if i == start else float('inf') for i in range(vertex)]
	Q = queue.PriorityQueue()
	Q.put((weight[start], start))
	while not Q.empty():
		cur = Q.get()
		if weight[cur[1]] < cur[0]:
			continue
		for j in range(len(edges[cur[1]])):
			if weight[edges[cur[1]][j][1]] > weight[cur[1]] + edges[cur[1]][j][2]:
				weight[edges[cur[1]][j][1]] = weight[cur[1]] + edges[cur[1]][j][2]
				Q.put((weight[edges[cur[1]][j][1]], edges[cur[1]][j][1]))
	return weight
(V, E, r) = map(int, input().split())
edges = [[] for i in range(V)]
for i in range(E):
	data = list(map(int, input().split()))
	edges[data[0]].append(data)
for j in range(V):
	edges[j].sort()
w = Dijkstra(V, edges, r)
for i in range(len(w)):
	if w[i] == float('inf'):
		print('INF')
	else:
		print(w[i])


import heapq
import math
INF = math.inf
(V, E, r) = map(int, input().split())
edges = [[] for i in range(V)]
dist = [INF] * V
visited = [0] * V
node_heap = []
for i in range(E):
	lst = list(map(int, input().strip().split()))
	edges[lst[0]].append([lst[0], lst[1], lst[2]])
dist[r] = 0
cur_node = r
visited[r] = 1
for cur_edges in edges[cur_node]:
	heapq.heappush(node_heap, [dist[r] + cur_edges[2], cur_edges[1]])
while node_heap:
	shortest_route = heapq.heappop(node_heap)
	if visited[shortest_route[1]]:
		continue
	cur_node = shortest_route[1]
	dist[cur_node] = shortest_route[0]
	for cur_edges in edges[cur_node]:
		if visited[cur_edges[1]] == 0:
			heapq.heappush(node_heap, [dist[cur_node] + cur_edges[2], cur_edges[1]])
	visited[cur_node] = 1
for i in range(V):
	if dist[i] == INF:
		print('{}'.format('INF'))
	else:
		print('{}'.format(dist[i]))


from collections import defaultdict
from heapq import heappush, heappop
(v, e, r) = map(int, input().split())
E = defaultdict(list)
for i in range(e):
	(s, t, d) = map(int, input().split())
	E[s].append((t, d))
D = [float('inf')] * v
D[r] = 0
q = []
heappush(q, (r, 0))
while q:
	(now, dist) = heappop(q)
	if D[now] < dist:
		continue
	for (nxt, w) in E[now]:
		t = dist + w
		if t < D[nxt]:
			D[nxt] = t
			heappush(q, (nxt, t))
for i in range(v):
	if D[i] == float('inf'):
		print('INF')
	else:
		print(D[i])


import sys
readline = sys.stdin.buffer.readline

def even(n):
	return 1 if n % 2 == 0 else 0
(n, m, r) = map(int, readline().split())
g = [[] for i in range(n)]
for i in range(m):
	(x, y, w) = map(int, readline().split())
	g[x].append((w, y))
INF = 10 ** 18
import heapq
push = heapq.heappush
pop = heapq.heappop

def dijkstra(s, g):
	n = len(g)
	dist = [INF] * n
	dist[s] = 0
	used = [0] * n
	used[s] = 1
	hp = []
	for go in g[s]:
		push(hp, go)
	while hp:
		(w, fr) = pop(hp)
		if not used[fr]:
			dist[fr] = w
			used[fr] = 1
			for (w, go) in g[fr]:
				if not used[go]:
					push(hp, (w + dist[fr], go))
	return dist
for i in dijkstra(r, g):
	print(i if i < INF else 'INF')


import sys

def bellmanford(G, s):
	n = len(G)
	dist = [float('inf') for i in range(n)]
	dist[s] = 0
	for k in range(n):
		flag = False
		for u in range(n):
			for v in range(len(G[u])):
				if dist[G[u][v][0]] > dist[u] + G[u][v][1]:
					dist[G[u][v][0]] = dist[u] + G[u][v][1]
					flag = True
		if not flag:
			break
	for k in dist:
		if k == float('inf'):
			print('INF')
		else:
			print(k)

def main():
	T = [int(x) for x in sys.stdin.readline().strip().split()]
	V = T[0]
	E = T[1]
	s = T[2]
	G = [[] for i in range(V)]
	for i in range(E):
		line = [int(x) for x in sys.stdin.readline().strip().split()]
		G[line[0]].append((line[1], line[2]))
	bellmanford(G, s)
main()


from collections import defaultdict
from heapq import heappush, heappop, heapify
from sys import stdin

def sp(G, R, V):
	d = {}
	q = [(0, R)]
	heapify(q)
	while q:
		(cost, v) = heappop(q)
		if v not in d:
			d[v] = cost
			for (next, c) in G[v].items():
				heappush(q, (cost + c, next))
	return d
(V, E, R) = [int(x) for x in stdin.readline().split()]
G = defaultdict(dict)
for case in range(E):
	(s, t, w) = [int(x) for x in stdin.readline().split()]
	G[s][t] = w
d = sp(G, R, V)
for k in range(V):
	if k not in d:
		print('INF')
	else:
		print(d[k])


import heapq
inf = 10000000000
(n, m, r) = map(int, input().split())
e = [[] for _ in range(n)]
for _ in range(m):
	(a, b, c) = map(int, input().split())
	e[a].append([b, c])
d = [inf] * n
d[r] = 0
pq = [[0, r]]
heapq.heapify(pq)
while len(pq):
	pre = heapq.heappop(pq)
	(a, b) = (pre[0], pre[1])
	if d[b] < a:
		continue
	for i in range(len(e[b])):
		next_index = e[b][i][0]
		next_cost = d[b] + e[b][i][1]
		if next_cost < d[next_index]:
			d[next_index] = next_cost
			heapq.heappush(pq, [next_cost, next_index])
for i in range(n):
	if d[i] == inf:
		print('INF')
	else:
		print(d[i])


import heapq as pq
inf = float('inf')
(n, e, r) = map(int, input().split())
M = [[] for i in range(n)]
for i in range(e):
	(s, t, d) = map(int, input().split())
	M[s].append([t, d])

def dijkstra(s):
	color = [0] * n
	d = [inf] * n
	d[s] = 0
	H = []
	pq.heappush(H, [0, s])
	while len(H) != 0:
		(u2, u1) = pq.heappop(H)
		color[u1] = 1
		if d[u1] < u2:
			continue
		for (v1, v2) in M[u1]:
			if color[v1] == 1:
				continue
			if d[u1] + v2 < d[v1]:
				d[v1] = d[u1] + v2
				pq.heappush(H, [d[v1], v1])
	return d
d = dijkstra(r)
for i in d:
	if i == inf:
		print('INF')
	else:
		print(i)


from heapq import heappop as pop
from heapq import heappush as push
INF = 10 ** 18

class edge:

	def __init__(self, to, cost):
		self.to = to
		self.cost = cost
(V, E, r) = map(int, input().split())
G = [[] for i in range(V)]
d = [0 for i in range(V)]

def dijkstra(s, V):
	que = []
	global d
	global G
	d = [INF for i in range(V)]
	d[s] = 0
	push(que, (0, s))
	while len(que):
		p = pop(que)
		v = p[1]
		if d[v] < p[0]:
			continue
		for i in range(len(G[v])):
			e = G[v][i]
			if d[e.to] > d[v] + e.cost:
				d[e.to] = d[v] + e.cost
				push(que, (d[e.to], e.to))
for i in range(E):
	(s, t, c) = map(int, input().split())
	G[s].append(edge(t, c))
dijkstra(r, V)
for i in range(V):
	if d[i] != INF:
		print(d[i])
	else:
		print('INF')


import sys
sys.setrecursionlimit(10 ** 7)
INF = 1 << 60
from heapq import heappush, heappop
(V, E, r) = map(int, input().split())
G = [[] for _ in range(V)]
for _ in range(E):
	(f, t, c) = map(int, input().split())
	G[f].append((t, c))
dist = [INF] * V
dist[r] = 0
q = [(0, r)]
while q:
	(cost, v) = heappop(q)
	for (to, c) in G[v]:
		sum_C = cost + c
		if sum_C < dist[to]:
			dist[to] = sum_C
			heappush(q, (sum_C, to))
for c in dist:
	print(c if c < INF else 'INF')


from heapq import heappush, heappop
INF = 10000000000

def solve(r, v, dic):
	dp = []
	q = []
	dp = [INF] * v
	dp[r] = 0
	heappush(q, (0, r))
	while q:
		(cost, vtx) = heappop(q)
		if dp[vtx] < cost:
			continue
		if vtx not in dic:
			continue
		for (vtx1, cost1) in dic[vtx]:
			if cost + cost1 < dp[vtx1]:
				dp[vtx1] = cost + cost1
				heappush(q, (cost + cost1, vtx1))
	return dp
dic = {}
dp = []
line = input()
(v, e, r) = list(map(int, line.split()))
for _ in range(0, e):
	line = input()
	(s, t, d) = list(map(int, line.split()))
	if s not in dic:
		dic[s] = [[t, d]]
	else:
		dic[s] += [[t, d]]
dp = solve(r, v, dic)
for t in range(0, v):
	print(dp[t] if dp[t] != INF else 'INF')


(v, e, r) = map(int, input().split())
g = [[] for i in range(v)]
for i in range(e):
	(s, t, d) = map(int, input().split())
	g[s].append([t, d])
cost = ['INF' for i in range(v)]
cost[r] = 0
finish = []
finish.append(r)
import heapq
h = []
u = r
for count in range(v - 1):
	for adj in g[u]:
		if cost[adj[0]] != 'INF':
			continue
		new_cost = cost[u] + adj[1]
		heapq.heappush(h, (new_cost, adj[0]))
	while 1:
		if len(h) == 0:
			break
		minc = heapq.heappop(h)
		if cost[minc[1]] == 'INF':
			cost[minc[1]] = minc[0]
			u = minc[1]
			break
for i in range(v):
	print(cost[i])


import heapq
import sys
input = sys.stdin.readline

def dijkstra_heap(s, edge):
	d = [10 ** 20] * n
	used = [True] * n
	d[s] = 0
	used[s] = False
	edgelist = []
	for (a, b) in edge[s]:
		heapq.heappush(edgelist, a * 10 ** 6 + b)
	while len(edgelist):
		minedge = heapq.heappop(edgelist)
		if not used[minedge % 10 ** 6]:
			continue
		v = minedge % 10 ** 6
		d[v] = minedge // 10 ** 6
		used[v] = False
		for e in edge[v]:
			if used[e[1]]:
				heapq.heappush(edgelist, (e[0] + d[v]) * 10 ** 6 + e[1])
	return d
(n, w, s) = map(int, input().split())
edge = [[] for i in range(n)]
for i in range(w):
	(x, y, z) = map(int, input().split())
	edge[x].append([z, y])
for i in dijkstra_heap(s, edge):
	if i == 10 ** 20:
		print('INF')
	else:
		print(i)


from collections import defaultdict
from heapq import heappush, heappop

def main():
	fl = input().split(' ')
	V = int(fl[0])
	E = int(fl[1])
	R = int(fl[2])
	G = defaultdict(list)
	for i in range(int(E)):
		(s, t, w) = [int(x) for x in input().split(' ')]
		G[s].append((t, w))
	d = {}
	INF = float('inf')
	for i in range(V):
		d[i] = INF
	d[R] = 0
	q = [R]
	while q:
		u = heappop(q)
		for v in G[u]:
			if d[v[0]] > d[u] + v[1]:
				d[v[0]] = d[u] + v[1]
				heappush(q, v[0])
	for k in d.keys():
		if d[k] == float('inf'):
			print('INF')
		else:
			print(d[k])
main()


from collections import defaultdict, deque
(V, E, r) = map(int, input().split(' '))
edges = defaultdict(set)
for _ in range(E):
	(s, t, d) = map(int, input().split(' '))
	edges[s].add((t, d))
costs = [-1] * V
costs[r] = 0
queue = deque()
queue.append(r)
while len(queue) > 0:
	s = queue.popleft()
	for (t, d) in edges[s]:
		if costs[t] < 0 or costs[s] + d < costs[t]:
			costs[t] = costs[s] + d
			queue.append(t)
for i in range(V):
	if costs[i] < 0:
		print('INF')
	else:
		print(costs[i])


from heapq import heappush, heappop

def dijkstra(g, start):
	dist = [1e+18] * len(g)
	dist[start] = 0
	q = [(0, start)]
	pending = len(g) - 1
	while q and pending:
		(c, v) = heappop(q)
		if dist[v] < c:
			continue
		pending -= 1
		for (to, cost) in g[v]:
			if dist[v] + cost < dist[to]:
				dist[to] = dist[v] + cost
				heappush(q, (dist[to], to))
	return dist
(v, e, r) = [int(i) for i in input().split()]
g = [[] for _ in range(v)]
for _ in range(e):
	(a, b, c) = [int(i) for i in input().split()]
	g[a].append((b, c))
for i in dijkstra(g, r):
	if i == 1e+18:
		print('INF')
	else:
		print(i)


(Vn, En, r) = map(int, input().split())
Ve = [[] for _ in range(Vn)]
for _ in range(En):
	(s, t, c) = map(int, input().split())
	Ve[s].append((t, c))
INF = 10 ** 4 * 10 ** 5
dp = [INF] * Vn
dp[r] = 0
from heapq import heappush, heappop
q = [(0, r)]
while q:
	(du, u) = heappop(q)
	for (v, dv) in Ve[u]:
		if dp[v] > du + dv:
			dp[v] = du + dv
			heappush(q, (du + dv, v))
for i in range(Vn):
	if not dp[i] == INF:
		print(dp[i])
	else:
		print('INF')


import queue

def dijkstra(n, graph, start):
	dist = [0 if i == start else float('inf') for i in range(n)]
	que = queue.PriorityQueue()
	que.put((0, start))
	while not que.empty():
		p = que.get()
		(d, now) = (p[0], p[1])
		if dist[now] < d:
			continue
		for e in graph[now]:
			if dist[e[0]] <= d + e[1]:
				continue
			dist[e[0]] = dist[now] + e[1]
			que.put((dist[e[0]], e[0]))
	return dist

def main():
	(n, m, s) = map(int, input().split())
	graph = [[] for _ in range(n)]
	for _ in range(m):
		data = list(map(int, input().split()))
		graph[data[0]].append([data[1], data[2]])
	dist = dijkstra(n, graph, s)
	for d in dist:
		if d == float('inf'):
			print('INF')
		else:
			print(d)
	return
main()


import heapq
maxd = float('inf')
(nv, ne, r) = [int(n) for n in input().split(' ')]
vertices = {k: {} for k in range(nv)}
for i in range(ne):
	(s, t, w) = [int(n) for n in input().split(' ')]
	vertices[s][t] = w
q = []
heapq.heappush(q, (0, r))
dist = {v: maxd for v in vertices}
dist[r] = 0
while len(q):
	u = heapq.heappop(q)[1]
	for v in vertices[u]:
		alt = dist[u] + vertices[u][v]
		if alt < dist[v]:
			dist[v] = alt
			heapq.heappush(q, (alt, v))
[print(d) if d < maxd else print('INF') for (v, d) in dist.items()]


from heapq import heappush, heappop
INF = 10 ** 18
(V, E, r) = map(int, input().split())
cost = [[] for i in range(V)]
for i in range(E):
	(x, y, z) = map(int, input().split())
	cost[x].append([y, z])
dist_lst = [INF for _ in range(V)]
que = []
heappush(que, (0, r))
dist_lst[r] = 0
while que:
	(dist, node) = heappop(que)
	for (to, cos) in cost[node]:
		if dist_lst[to] > cos + dist:
			dist_lst[to] = cos + dist
			heappush(que, (cos + dist, to))
for dist in dist_lst:
	if dist == INF:
		print('INF')
	else:
		print(dist)


import heapq
(V, E, r) = map(int, input().split())
G = [[] for _ in range(V)]
for _ in range(E):
	(s, t, d) = map(int, input().split())
	G[s].append((t, d))
dp = [float('inf')] * V
dp[r] = 0
Q = []
heapq.heappush(Q, (0, r))
while len(Q) > 0:
	(d, v) = heapq.heappop(Q)
	if dp[v] < d:
		continue
	for (nv, nd) in G[v]:
		if dp[nv] > dp[v] + nd:
			dp[nv] = dp[v] + nd
			heapq.heappush(Q, (dp[nv], nv))
print(*[d if d != float('inf') else 'INF' for d in dp], sep='\n')


import heapq
[vertex, edge, r] = list(map(int, input('').split()))
infty = 999999999
final_c = [infty for i in range(vertex)]
dict_c = [[] for i in range(vertex)]
for i in range(edge):
	[e1, e2, c] = list(map(int, input('').split()))
	dict_c[e1].append((e2, c))
final_c[r] = 0
root_c = [(final_c[r], r)]

def Dijkstra(root, n):
	global infty, root_c, final_c, dict_c, root_c
	while root_c:
		(cost, v) = heapq.heappop(root_c)
		if cost > final_c[v]:
			continue
		for i in range(len(dict_c[v])):
			(e2, c) = dict_c[v][i]
			if final_c[e2] > final_c[v] + c:
				final_c[e2] = final_c[v] + c
				heapq.heappush(root_c, (final_c[e2], e2))
Dijkstra(r, vertex)
for i in range(vertex):
	if final_c[i] == infty:
		print('INF')
	else:
		print(final_c[i])


import heapq

def dijkstra_heap(s):
	d = [float('inf')] * n
	used = [True] * n
	d[s] = 0
	used[s] = False
	edgelist = []
	for e in edge[s]:
		heapq.heappush(edgelist, e)
	while len(edgelist):
		minedge = heapq.heappop(edgelist)
		if not used[minedge[1]]:
			continue
		v = minedge[1]
		d[v] = minedge[0]
		used[v] = False
		for e in edge[v]:
			if used[e[1]]:
				heapq.heappush(edgelist, [e[0] + d[v], e[1]])
	return d
(n, e, r) = map(int, input().split())
std = [[int(x) for x in input().split()] for _ in range(e)]
edge = [[] for _ in range(n)]
for (s, t, d) in std:
	edge[s].append([d, t])
ans = dijkstra_heap(r)
for x in ans:
	if x == float('inf'):
		print('INF')
	else:
		print(x)


from heapq import heappop, heappush
import math
(V, E, r) = map(int, input().split())
edge = [[] for _ in range(V)]
d = [math.inf] * V
que = []
for _ in range(E):
	(s, t, c) = map(int, input().split())
	edge[s].append([t, c])
d[r] = 0
heap = [(d[r], r)]
while heap:
	(du, u) = heappop(heap)
	for (t, c) in edge[u]:
		if d[t] > du + c:
			d[t] = du + c
			heappush(heap, (d[t], t))
for di in d:
	print('INF' if math.isinf(di) else di)


import heapq
(V, E, r) = list(map(int, input().split()))
INF = 10 ** 15

def dijkstra(edges, s):
	d = [INF for x in range(V)]
	d[s] = 0
	que = []
	heapq.heappush(que, (0, s))
	while len(que) > 0:
		(u_d, u) = heapq.heappop(que)
		if d[u] < u_d:
			continue
		for (v_d, v) in edges[u]:
			if d[v] > d[u] + v_d:
				d[v] = d[u] + v_d
				heapq.heappush(que, (d[v], v))
	return d
cost = [[] for i in range(V)]
for i in range(E):
	(x, y, z) = map(int, input().split())
	cost[x].append((z, y))
d = dijkstra(cost, r)
for c in d:
	if c == INF:
		print('INF')
	else:
		print(c)


from heapq import heappush, heappop
(V, E, r) = map(int, input().split())
inf = 10 ** 10
edges = [[] for _ in [None] * V]
vertices = [inf] * V
vertices[r] = 0
for _ in [None] * E:
	(s, t, d) = map(int, input().split())
	edges[s].append((t, d))
q = [(0, r)]
rem = V - 1
while q and rem > 0:
	(cost, v) = heappop(q)
	if vertices[v] < cost:
		continue
	rem -= 1
	for (dest, _cost) in edges[v]:
		newcost = cost + _cost
		if vertices[dest] > newcost:
			vertices[dest] = newcost
			heappush(q, (newcost, dest))
print('\n'.join([str(n) if n != inf else 'INF' for n in vertices]))


import heapq
INF = 10 ** 9
(v, e, r) = map(int, input().split())
adjw = [[] for _ in range(v)]
for i in range(e):
	(s, t, d) = map(int, input().split())
	adjw[s].append((t, d))

def dijkstra(x):
	d = [INF] * x
	d[r] = 0
	hq = [(0, r)]
	while hq:
		(c, v) = heapq.heappop(hq)
		if d[v] < c:
			continue
		for (t, cost) in adjw[v]:
			if d[v] + cost < d[t]:
				d[t] = d[v] + cost
				heapq.heappush(hq, (d[t], t))
	return d
d = dijkstra(v)
for i in range(v):
	print(d[i]) if d[i] < INF else print('INF')


import heapq

def main():
	[V, E, r] = [int(num) for num in input().split()]
	edges = {}
	for i in range(V):
		edges[i] = []
	for i in range(E):
		num_list = [int(num) for num in input().split()]
		edges[num_list[0]].append((num_list[1], num_list[2]))
	dist = [10 ** 10 for _ in range(V)]
	dist[r] = 0
	PQ = []
	heapq.heappush(PQ, (0, r))
	while len(PQ) > 0:
		(_, node_num) = heapq.heappop(PQ)
		for edge in edges[node_num]:
			new_len = dist[node_num] + edge[1]
			if new_len < dist[edge[0]]:
				heapq.heappush(PQ, (new_len, edge[0]))
				dist[edge[0]] = new_len
	for i in range(V):
		if dist[i] == 10 ** 10:
			print('INF')
		else:
			print(dist[i])
main()


from heapq import heapify, heappop, heappush

def main():
	(vertex, edge, root) = map(int, input().split())
	max_val = 10 ** 10
	graph = [[] for _ in range(vertex)]
	for _ in range(edge):
		(s, t, d) = map(int, input().split())
		graph[s].append((t, d))
	dist = [max_val for _ in range(vertex)]
	dist[root] = 0
	q = []
	heapify(q)
	heappush(q, (0, root))
	while q:
		(now_d, now_v) = heappop(q)
		for (next_v, length) in graph[now_v]:
			now_dist = dist[now_v] + length
			if dist[next_v] > now_dist:
				dist[next_v] = now_dist
				heappush(q, (dist[next_v], next_v))
	for d in dist:
		print('INF' if d == max_val else d)
main()


from heapq import heappush, heappop
(V, E, r) = map(int, input().split())
edges = [[] for _ in [None] * V]
vertices = [10 ** 10] * V
vertices[r] = 0
for _ in [None] * E:
	(s, t, d) = map(int, input().split())
	edges[s].append((t, d))
q = [(0, r)]
while q:
	(cost, v) = heappop(q)
	for (dest, _cost) in edges[v]:
		newcost = cost + _cost
		if vertices[dest] > newcost:
			vertices[dest] = newcost
			heappush(q, (newcost, dest))
print('\n'.join([str(n) if n != 10 ** 10 else 'INF' for n in vertices]))


import heapq

def dijkstra(s, edge):
	dist = [10 ** 20] * N
	used = [False] * N
	dist[s] = 0
	used[s] = True
	edgelist = []
	for (a, b) in edge[s]:
		heapq.heappush(edgelist, a * 10 ** 6 + b)
	while len(edgelist):
		minedge = heapq.heappop(edgelist)
		if used[minedge % 10 ** 6]:
			continue
		v = minedge % 10 ** 6
		dist[v] = minedge // 10 ** 6
		used[v] = True
		for e in edge[v]:
			if not used[e[1]]:
				heapq.heappush(edgelist, (e[0] + dist[v]) * 10 ** 6 + e[1])
	return dist
(N, M, r) = map(int, input().split())
graph = [list(map(int, input().split())) for _ in range(M)]
edge = [[] for _ in range(N)]
for i in graph:
	edge[i[0]].append([i[2], i[1]])
dist = dijkstra(r, edge)
for i in range(N):
	if dist[i] == 10 ** 20:
		print('INF')
	else:
		print(dist[i])


import heapq
(V, E, root) = map(int, input().split())
visited = [False] * V
nodes = [[] for i in range(V)]
cost = [[] for i in range(V)]
for i in range(E):
	(s, t, d) = map(int, input().split())
	nodes[s].append(t)
	cost[s].append(d)
dist = [float('inf')] * V
dist[root] = 0
h = [(0, root)]
while h:
	(c, v) = heapq.heappop(h)
	if visited[v]:
		continue
	visited[v] = True
	for i in range(len(nodes[v])):
		t = nodes[v][i]
		if dist[t] > c + cost[v][i]:
			dist[t] = c + cost[v][i]
		if not visited[t]:
			visited[v] = True
			heapq.heappush(h, (c + cost[v][i], t))
for i in range(V):
	if dist[i] == float('inf'):
		print('INF')
	else:
		print(dist[i])


from collections import defaultdict
from collections import deque

def main():
	fl = input().split(' ')
	V = int(fl[0])
	E = int(fl[1])
	R = int(fl[2])
	G = defaultdict(list)
	for i in range(int(E)):
		(s, t, w) = [int(x) for x in input().split(' ')]
		G[s].append((t, w))
	d = {}
	INF = float('inf')
	for i in range(V):
		d[i] = INF
	d[R] = 0
	q = deque([R])
	while q:
		u = q.popleft()
		for v in G[u]:
			if d[v[0]] > d[u] + v[1]:
				d[v[0]] = d[u] + v[1]
				q.append(v[0])
	for k in range(V):
		if d[k] == float('inf'):
			print('INF')
		else:
			print(d[k])
main()


from heapq import heappush, heappop
(v, e, r) = map(int, input().split())
edges = [[] for i in range(v)]
d = [float('INF')] * v
for i in range(e):
	(s, t, c) = map(int, input().split())
	edges[s].append([t, c])
que = []
heappush(que, (0, r))
d[r] = 0
while que:
	(c, u) = heappop(que)
	for (v, s) in edges[u]:
		if d[v] > s + c:
			d[v] = s + c
			heappush(que, (d[v], v))
for i in d:
	if i == float('inf'):
		print('INF')
	else:
		print(i)


(v, e, r) = map(int, input().split())
E = [tuple(map(int, input().split())) for one in range(e)]
D = [10 ** 9 for two in range(v)]
D[r] = 0
for i in range(v):
	update = 0
	for (s, t, d) in E:
		if D[t] > D[s] + d:
			D[t] = D[s] + d
			update += 1
	if update == 0:
		break
for j in range(v):
	print(D[j] if D[j] != 10 ** 9 else 'INF')


from heapq import heappush, heappop
(N, M, s) = map(int, input().split())
Adj = [[] for i in range(N)]
for i in range(M):
	(ui, vi, ci) = map(int, input().split())
	Adj[ui].append((vi, ci))
d = [float('inf')] * N
d[s] = 0
pq = [(0, s)]
fixed = 0
while pq:
	(di, v) = heappop(pq)
	if di > d[v]:
		continue
	for (u, c) in Adj[v]:
		if d[v] + c < d[u]:
			d[u] = d[v] + c
			heappush(pq, (d[u], u))
	fixed += 1
	if fixed == N - 1:
		break
for di in d:
	print(di if di != float('inf') else 'INF')


from heapq import heappush, heappop
(v, e, r) = map(int, input().split())
G = [[] for _ in range(v)]
for _ in range(e):
	(s, t, d) = map(int, input().split())
	G[s].append((d, t))
dist = [float('inf') for _ in range(v)]
prev = [(0, r)]
while len(prev):
	now = heappop(prev)
	if dist[now[1]] < now[0]:
		continue
	else:
		dist[now[1]] = now[0]
	for nxt in G[now[1]]:
		if dist[nxt[1]] > dist[now[1]] + nxt[0]:
			dist[nxt[1]] = dist[now[1]] + nxt[0]
			heappush(prev, (nxt[0] + dist[now[1]], nxt[1]))
for i in dist:
	if i == float('inf'):
		print('INF')
	else:
		print(i)


from sys import stdin
import math
import heapq
(V, E, r) = map(lambda x: int(x), stdin.readline().split(' '))
adjacency_list = [[] for x in range(V)]
for i in range(E):
	(s, t, d) = map(lambda x: int(x), stdin.readline().split(' '))
	adjacency_list[s].append([t, d])
distance = [(math.inf, id) for id in range(V)]
output_distance = [math.inf for id in range(V)]
checked_set = set()
distance[r] = (0, r)
distance.sort()
while len(checked_set) < V:
	target = heapq.heappop(distance)
	target_id = target[1]
	if target_id in checked_set:
		continue
	target_dis = target[0]
	output_distance[target_id] = target_dis
	for i in adjacency_list[target_id]:
		if i[1] + target_dis < output_distance[i[0]]:
			heapq.heappush(distance, (i[1] + target_dis, i[0]))
	checked_set.add(target_id)
for dis in output_distance:
	if dis == math.inf:
		print('INF')
	else:
		print(dis)


from heapq import heappush, heappop
INF = 10 ** 12
(V, E, r) = map(int, input().split())
graph = [[] for _ in range(V)]
dist_all = [INF] * V
dist_all[r] = 0
for i in range(E):
	(start, end, dist1) = map(int, input().split())
	graph[start].append([end, dist1])
q = []
heappush(q, (0, r))
while q:
	(dist, node) = heappop(q)
	for (to, cost) in graph[node]:
		if dist_all[to] > dist + cost:
			dist_all[to] = dist + cost
			heappush(q, (dist + cost, to))
for i in range(V):
	if dist_all[i] == INF:
		print('INF')
	else:
		print(dist_all[i])


(n, m, s) = map(int, input().split())
edge = [[] for _ in range(n)]
for _ in range(m):
	(a, b, c) = map(int, input().split())
	edge[a].append((b, c))
from heapq import heappop, heappush
ans = [10 ** 20] * n
ans[s] = 0
h = [[0, s]]
while h:
	(c, v) = heappop(h)
	if ans[v] < c:
		continue
	for (u, t) in edge[v]:
		if c + t < ans[u]:
			ans[u] = c + t
			heappush(h, [c + t, u])
for i in ans:
	if i == 10 ** 20:
		print('INF')
	else:
		print(i)


import heapq
(N, M, S) = map(int, input().split())
edges = [[] for _ in range(N)]
inf = 10 ** 18
for _ in range(M):
	(u, v, d) = map(int, input().split())
	edges[u].append((v, d))

def dijkstra(s):
	dist = [inf] * N
	dist[s] = 0
	vs = []
	heapq.heappush(vs, (0, s))
	while len(vs):
		(res, v) = heapq.heappop(vs)
		if res > dist[v]:
			continue
		for (nxt, cost) in edges[v]:
			if res + cost < dist[nxt]:
				dist[nxt] = res + cost
				heapq.heappush(vs, (dist[nxt], nxt))
	return dist
dist = dijkstra(S)
for i in range(N):
	if dist[i] == inf:
		print('INF')
	else:
		print(dist[i])


import heapq
INF = float('inf')

def dijkstra(g, s):
	n = len(g)
	dist = [INF] * n
	dist[s] = 0
	hq = []
	heapq.heappush(hq, (dist[s], s))
	while hq:
		(cost, u) = heapq.heappop(hq)
		if dist[u] < cost:
			continue
		for (v, c) in g[u]:
			if dist[v] <= dist[u] + c:
				continue
			dist[v] = dist[u] + c
			heapq.heappush(hq, (dist[v], v))
	return dist
(V, E, r) = map(int, input().split())
g = [[] for _ in range(V)]
for _ in range(E):
	(s, t, d) = map(int, input().split())
	g[s].append((t, d))
ds = dijkstra(g, r)
for d in ds:
	print('INF' if d == INF else d)


import heapq
INF = 10 ** 9
(V, E, r) = map(int, input().split())
edge_list = [[] for _ in range(V)]
dist_list = [INF] * V
for _ in range(E):
	(s, t, d) = map(int, input().split())
	edge_list[s].append((t, d))
q = []
heapq.heapify(q)
heapq.heappush(q, (0, r))
dist_list[r] = 0
while len(q) != 0:
	(dist, node) = heapq.heappop(q)
	for (to, cost) in edge_list[node]:
		if dist + cost < dist_list[to]:
			dist_list[to] = dist + cost
			heapq.heappush(q, (dist + cost, to))
for dist in dist_list:
	if dist == INF:
		print('INF')
	else:
		print(dist)


from heapq import heappush, heappop
INF = 10 ** 20
(v_num, e_num, r) = map(int, input().split())
edges_lst = [[] for _ in range(v_num)]
dist_lst = [INF for _ in range(v_num)]
for _ in range(e_num):
	(s, t, dist) = map(int, input().split())
	edges_lst[s].append((t, dist))
que = []
heappush(que, (0, r))
dist_lst[r] = 0
while que:
	(dist, node) = heappop(que)
	for (to, cost) in edges_lst[node]:
		if dist_lst[to] > cost + dist:
			dist_lst[to] = cost + dist
			heappush(que, (cost + dist, to))
for dist in dist_lst:
	if dist == INF:
		print('INF')
	else:
		print(dist)


import sys
from heapq import heappush, heappop
readline = sys.stdin.readline
(V, E, r) = map(int, readline().split())
inf = 10 ** 10
edges = [[] for _ in [None] * V]
vertices = [inf] * V
vertices[r] = 0
for _ in [None] * E:
	(s, t, d) = map(int, readline().split())
	edges[s].append((t, d))
q = [(0, r)]
rem = V - 1
while q and rem:
	(cost, v) = heappop(q)
	if vertices[v] < cost:
		continue
	rem -= 1
	for (dest, _cost) in edges[v]:
		newcost = cost + _cost
		if vertices[dest] > newcost:
			vertices[dest] = newcost
			heappush(q, (newcost, dest))
print('\n'.join([str(n) if n != inf else 'INF' for n in vertices]))


import sys
input = sys.stdin.readline
from heapq import heappush, heappop, heapify

def dijkstra(V, E, edge, r=0):
	dist = [float('inf')] * V
	dist[r] = 0
	visited = [False] * V
	visited[r] = True
	q = edge[r]
	heapify(q)
	count = V - 1
	while count and q:
		(c, v) = heappop(q)
		if visited[v]:
			continue
		count -= 1
		visited[v] = True
		dist[v] = c
		for (cc, u) in edge[v]:
			if not visited[u]:
				heappush(q, (c + cc, u))
	return dist

def main():
	(V, E, r) = list(map(int, input().split()))
	edge = [[] for i in range(V)]
	for i in range(E):
		(s, t, d) = map(int, input().split())
		edge[s].append((d, t))
	dist = dijkstra(V, E, edge, r)
	for d in dist:
		if d == float('inf'):
			print('INF')
		else:
			print(d)
main()


import sys
sys.setrecursionlimit(10000000)
MOD = 10 ** 9 + 7
INF = 10 ** 15
from heapq import heapify, heappop, heappush

def dijkstra(G, s=0):
	n = len(G)
	dist = [INF] * n
	dist[s] = 0
	q = [(0, s)]
	while q:
		(d, v) = heappop(q)
		if dist[v] < d:
			continue
		for (e, cost) in G[v]:
			if dist[e] > dist[v] + cost:
				dist[e] = dist[v] + cost
				heappush(q, (dist[e], e))
	return dist

def main():
	(V, E, r) = map(int, input().split())
	G = [[] for _ in range(V)]
	for _ in range(E):
		(s, t, d) = map(int, input().split())
		G[s].append((t, d))
	dist = dijkstra(G, s=r)
	print('\n'.join(map(lambda x: 'INF' if x == INF else str(x), dist)))
main()


from heapq import heappush, heappop
_input = input
(V, E, r) = map(int, _input().split())
inf = 10 ** 10
edges = [[] for _ in [None] * V]
vertices = [inf] * V
vertices[r] = 0
for _ in [None] * E:
	(s, t, d) = map(int, _input().split())
	edges[s].append((t, d))
q = [(0, r)]
rem = V - 1
while q and rem:
	(cost, v) = heappop(q)
	if vertices[v] < cost:
		continue
	rem -= 1
	for (dest, _cost) in edges[v]:
		newcost = cost + _cost
		if vertices[dest] > newcost:
			vertices[dest] = newcost
			heappush(q, (newcost, dest))
print('\n'.join([str(n) if n != inf else 'INF' for n in vertices]))


(v, e, r) = map(int, input().split())
g = [[] for i in range(v)]
for i in range(e):
	(s, t, d) = map(int, input().split())
	g[s].append([t, d])
cost = ['INF' for i in range(v)]
cost[r] = 0
import heapq
h = []
u = r
for count in range(v - 1):
	for adj in g[u]:
		if cost[adj[0]] != 'INF':
			continue
		new_cost = cost[u] + adj[1]
		heapq.heappush(h, (new_cost, adj[0]))
	while 1:
		if len(h) == 0:
			break
		minc = heapq.heappop(h)
		if cost[minc[1]] == 'INF':
			cost[minc[1]] = minc[0]
			u = minc[1]
			break
for i in range(v):
	print(cost[i])


(V, E, r) = map(int, input().split())
cost = [[] for _ in range(V)]
INF = float('inf')
d = [INF] * V
prev = [-1] * V
used = [True] * V
for _ in range(E):
	(s, t, dd) = map(int, input().split())
	cost[s].append([dd, t])
d[r] = 0
used[r] = False
import heapq
Q = []
for e in cost[r]:
	heapq.heappush(Q, e)
while len(Q) != 0:
	(dis, v) = heapq.heappop(Q)
	if used[v] == False:
		continue
	d[v] = dis
	used[v] = False
	for e in cost[v]:
		if used[e[1]] == True:
			heapq.heappush(Q, [e[0] + d[v], e[1]])
for k in d:
	if k != INF:
		print(k)
	else:
		print('INF')


import math
graph = []
(V, E, r) = map(int, input().split())
lens = [float('inf') for i in range(V)]
lens[r] = 0
for i in range(E):
	(start, desti, distance) = map(int, input().split())
	graph.append([start, desti, distance])
while True:
	flag = True
	for q in graph:
		if lens[q[1]] > lens[q[0]] + q[2]:
			lens[q[1]] = min(lens[q[1]], lens[q[0]] + q[2])
			flag = False
	if flag:
		break
for item in lens:
	if math.isinf(item):
		print('INF')
	else:
		print(item)


from collections import defaultdict
from heapq import heappush, heappop
inf = float('inf')

def inf_matrix(n):
	return [[inf for i in range(n)] for j in range(n)]

def dijkstra(r, w, V):
	result = [inf] * V
	q = []
	heappush(q, (0, r))
	result[r] = 0
	while len(q) > 0:
		(min_w, min_next) = heappop(q)
		for (weight, nxt) in w[min_next]:
			if result[nxt] > min_w + weight:
				result[nxt] = min_w + weight
				heappush(q, (min_w + weight, nxt))
	return result

def main():
	(V, E, r) = map(int, input().split())
	w = defaultdict(list)
	for e in range(E):
		(s, t, d) = map(int, input().split())
		w[s].append((d, t))
	result = dijkstra(r, w, V)
	for i in range(V):
		if result[i] == inf:
			print('INF')
		else:
			print(result[i])
main()


import sys
import heapq as h
inf = sys.maxsize

class heapq:

	def __init__(self):
		self.q = []

	def push(self, v):
		h.heappush(self.q, v)

	def pop(self):
		return h.heappop(self.q)

	def is_empty(self):
		return len(self.q) == 0
(V, e, r) = (int(i) for i in input().split())
G = [[] for i in range(V)]
for j in range(e):
	(f, t, cost) = (int(i) for i in input().split())
	G[f].append((t, cost))
dist = [inf] * 100000
visited = [False] * 100000
a = heapq()
a.push((0, r))

def end(visited):
	for i in visited:
		if i == False:
			return False
	return True
while not a.is_empty():
	(cur_dist, u) = a.pop()
	if not visited[u]:
		dist[u] = cur_dist
		visited[u] = [True]
		for (v, cost) in G[u]:
			if not visited[v] and dist[u] + cost < dist[v]:
				a.push((dist[u] + cost, v))
for i in range(V):
	if dist[i] == inf:
		print('INF')
	else:
		print(dist[i])


import sys
from heapq import heappush, heappop
inf = 1 << 30

def solve():
	(N, M, s) = map(int, sys.stdin.readline().split())
	Adj = [[] for i in range(N)]
	for i in range(M):
		(ui, vi, ci) = map(int, sys.stdin.readline().split())
		Adj[ui].append((vi, ci))
	d = dijkstra(N, Adj, s)
	for di in d:
		print(di if di < inf else 'INF')

def dijkstra(N, Adj, s):
	d = [inf] * N
	p = [-1] * N
	fixed = [False] * N
	d[s] = 0
	p[s] = s
	pq = [(0, s)]
	for i in range(N - 1):
		while pq:
			(di, v) = heappop(pq)
			if not fixed[v] and di == d[v]:
				break
		fixed[v] = True
		for (u, c) in Adj[v]:
			if d[v] + c < d[u]:
				d[u] = d[v] + c
				p[u] = v
				heappush(pq, (d[u], u))
	return d
solve()


import heapq
import os
import sys

def main():
	(V, E, R) = read_ints()
	G = [dict() for _ in range(V)]
	for _ in range(E):
		(s, t, d) = read_ints()
		G[s][t] = d
	print(*solve(V, E, R, G), sep='\n')

def solve(V, E, R, G):
	dists = [-1] * V
	q = []
	heapq.heappush(q, (0, R))
	while q:
		(d, i) = heapq.heappop(q)
		if dists[i] != -1:
			continue
		dists[i] = d
		for (j, e) in G[i].items():
			if dists[j] == -1:
				heapq.heappush(q, (d + e, j))
	return ['INF' if d == -1 else d for d in dists]
DEBUG = 'DEBUG' in os.environ

def inp():
	return sys.stdin.readline().rstrip()

def read_int():
	return int(inp())

def read_ints():
	return [int(e) for e in inp().split()]

def dprint(*value, sep=' ', end='\n'):
	if DEBUG:
		print(*value, sep=sep, end=end)
main()


(V, E, Start) = map(int, input().split())
INF = 10 ** 20
Edge = []
D = [INF] * V
for _ in range(E):
	Edge.append(list(map(int, input().split())))
D[Start] = 0
while True:
	update = False
	for i in range(E):
		fr = Edge[i][0]
		to = Edge[i][1]
		cost = Edge[i][2]
		if D[fr] != INF and D[to] > D[fr] + cost:
			D[to] = D[fr] + cost
			update = True
	if update == False:
		break
for d in D:
	if d == INF:
		print('INF')
	else:
		print(d)


import os
import sys
import heapq

def main():
	(V, E, r) = map(int, input().split())
	distance = [float('inf')] * V
	distance[r] = 0
	path = [[] for _ in range(V)]
	for _ in range(E):
		(s, t, d) = map(int, input().split())
		path[s].append([d, t])
	Q = [[0, r]]
	while Q:
		(_, u) = heapq.heappop(Q)
		for (dis, v) in path[u]:
			if distance[u] + dis < distance[v]:
				distance[v] = distance[u] + dis
				heapq.heappush(Q, [dis, v])
	distance = [dis if dis != float('inf') else 'INF' for dis in distance]
	for dis in distance:
		print(dis)
main()


from heapq import heappush, heappop
(V, E, r) = map(int, input().split())
edges = [[] for _ in range(V)]
for _ in range(E):
	(s, t, d) = map(int, input().split())
	edges[s].append((t, d))
que = []
mindists = [float('inf')] * V
mindists[r] = 0
heappush(que, (0, r))
while que:
	(ccost, cnode) = heappop(que)
	if mindists[cnode] < ccost:
		continue
	for (nnode, ncost) in edges[cnode]:
		tcost = ccost + ncost
		if mindists[nnode] > tcost:
			mindists[nnode] = tcost
			heappush(que, (tcost, nnode))
print('\n'.join((str(e) if e != float('inf') else 'INF' for e in mindists)))


import math
import heapq
(V, E, r) = map(int, input().split())
edge = [[] for _ in range(V)]
d = [math.inf for _ in range(V)]
for i in range(E):
	(si, ti, di) = map(int, input().split())
	edge[si].append([ti, di])
q = []
heapq.heapify(q)

def dijkstra(start):
	d[start] = 0
	heapq.heappush(q, [0, start])
	while len(q) != 0:
		di = heapq.heappop(q)
		distance = di[0]
		v = di[1]
		if d[v] < distance:
			continue
		ei = edge[v]
		for i in range(len(ei)):
			e = ei[i]
			if d[e[0]] > d[v] + e[1]:
				d[e[0]] = d[v] + e[1]
				heapq.heappush(q, [d[e[0]], e[0]])
dijkstra(r)
for i in range(len(d)):
	print(d[i] if d[i] != math.inf else 'INF')


import sys
from heapq import heappush, heappop
read = sys.stdin.read
readline = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 8)
INF = float('inf')
MOD = 10 ** 9 + 7

def main():
	(V, E, r) = map(int, readline().split())
	G = [[] for _ in range(V)]
	for _ in range(E):
		(u, v, d) = map(int, readline().split())
		G[u].append([v, d])
	dist = [INF] * V
	que = [(0, r)]
	dist[r] = 0
	while que:
		(c, v) = heappop(que)
		for (t, cost) in G[v]:
			if dist[v] + cost < dist[t]:
				dist[t] = dist[v] + cost
				heappush(que, (dist[t], t))
	for i in dist:
		print('INF' if i == INF else i)
main()


import queue
(v, e, s) = map(int, input().split())
edge = []
inf = 1000000100
for i in range(v):
	edge.append([])
for i in range(e):
	(vs, vt, d) = map(int, input().split())
	edge[vs].append((vt, d))
dis = []
for i in range(v):
	dis.append(inf)
dis[s] = 0
q = queue.PriorityQueue()
q.put((0, s))
while not q.empty():
	now = q.get()
	nowv = now[1]
	for (vt, d) in edge[nowv]:
		if dis[vt] > dis[nowv] + d:
			dis[vt] = dis[nowv] + d
			q.put((dis[vt], vt))
for i in range(v):
	if dis[i] >= inf:
		print('INF')
	else:
		print(dis[i])


import heapq

def main():
	(nvertices, nedges, s) = map(int, input().split())
	adj = [[] for i in range(nvertices)]
	for i in range(nedges):
		(u, v, w) = map(int, input().split())
		adj[u].append((v, w))
	INF = float('inf')
	costs = [INF] * nvertices
	costs[s] = 0
	que = [(0, s)]
	for i in range(1, nvertices):
		que.append((INF, 1))
	cnt = 0
	while que:
		(cost, u) = heapq.heappop(que)
		if cost > costs[u]:
			continue
		costs[u] = cost
		for (v, w) in adj[u]:
			if costs[v] > costs[u] + w:
				costs[v] = costs[u] + w
				heapq.heappush(que, (costs[v], v))
		cnt += 1
		if cnt == nvertices:
			break
	for i in range(nvertices):
		print(costs[i] if costs[i] < INF else 'INF')
main()


import heapq
(N, M, r) = list(map(int, input().split()))
D = {}
for i in range(N):
	D[i] = []
for _ in range(M):
	(s, t, d) = list(map(int, input().split()))
	D[s].append((t, d))
A = []
heapq.heappush(A, (0, r))
INF = float('inf')
results = [INF for _ in range(N)]
results[r] = 0
while A:
	(cost, a) = heapq.heappop(A)
	for (t, d) in D[a]:
		next_cost = cost + d
		if next_cost < results[t]:
			results[t] = next_cost
			heapq.heappush(A, (next_cost, t))
for result in results:
	if result == INF:
		print('INF')
	else:
		print(result)


from heapq import heappush, heappop, heapify
INF = 10 ** 30

def dijkstra(G, s, n):
	que = [(0, s)]
	dist = [INF] * n
	dist[s] = 0
	while que:
		(mincost, u) = heappop(que)
		if mincost > dist[u]:
			continue
		for (c, v) in G[u]:
			if dist[u] + c < dist[v]:
				dist[v] = dist[u] + c
				heappush(que, (dist[v], v))
	return dist
(V, E, r) = map(int, input().split())
G = [[] for _ in range(V)]
for i in range(E):
	(s, t, d) = map(int, input().split())
	G[s].append((d, t))
dist = dijkstra(G, r, V)
for i in range(V):
	print(dist[i] if dist[i] != INF else 'INF')


import heapq
INF = 10 ** 9 + 1

def solve():
	(n_V, n_E, r) = map(int, input().split())
	graph = {v: [] for v in range(n_V)}
	for _ in range(n_E):
		(u, v, w) = map(int, input().split())
		graph[u].append((v, w))
	min_distances = [INF] * n_V
	min_distances[r] = 0
	prev = [None for _ in range(n_V)]
	queue = [(min_distances[v], v) for v in range(n_V)]
	heapq.heapify(queue)
	while queue:
		(_, dom) = heapq.heappop(queue)
		for (cod, edge_weight) in graph[dom]:
			if min_distances[cod] > min_distances[dom] + edge_weight:
				min_distances[cod] = min_distances[dom] + edge_weight
				prev[cod] = dom
				heapq.heappush(queue, (min_distances[cod], cod))
	for d in min_distances:
		print(d if d < INF else 'INF')
solve()


import heapq
(V, E, r) = map(int, input().split())
g = [[] for _ in range(V)]
for i in range(E):
	(s, t, d) = map(int, input().split())
	g[s].append((t, d))
dis = [float('inf') for _ in range(V)]
dis[r] = 0
pq = [(0, r)]
while pq:
	(d, node) = heapq.heappop(pq)
	if d > dis[node]:
		continue
	for (nxt, cost) in g[node]:
		if d + cost < dis[nxt]:
			dis[nxt] = d + cost
			heapq.heappush(pq, (dis[nxt], nxt))
for d in dis:
	if d == float('inf'):
		print('INF')
	else:
		print(d)


from heapq import heappop, heappush

class E:

	def __init__(self, to, cost):
		self.to = to
		self.cost = cost
(N, M, r) = [int(x) for x in input().split()]
G = [[] for x in range(N)]
D = [float('inf')] * N
V = [False] * N
for i in range(M):
	(s, t, d) = [int(x) for x in input().split()]
	G[s].append(E(t, d))
pq = [(0, r)]
D[r] = 0
while pq:
	u = heappop(pq)
	if V[u[1]]:
		continue
	for v in G[u[1]]:
		if v.cost + u[0] < D[v.to]:
			D[v.to] = v.cost + u[0]
			heappush(pq, (D[v.to], v.to))
	V[u[1]] = True
for d in D:
	print(d if d != float('inf') else 'INF')


import queue
(v, e, s) = map(int, input().split())
edge = []
inf = 1000000100
for i in range(v):
	edge.append([])
for i in range(e):
	(vs, vt, d) = map(int, input().split())
	edge[vs].append((vt, d))
dis = []
for i in range(v):
	dis.append(inf)
dis[s] = 0
q = queue.PriorityQueue()
q.put((0, s))
while not q.empty():
	now = q.get()
	nowv = now[1]
	for (vt, d) in edge[nowv]:
		if dis[vt] > dis[nowv] + d:
			dis[vt] = dis[nowv] + d
			q.put((dis[vt], vt))
for i in range(v):
	if dis[i] >= inf:
		print('INF')
	else:
		print(dis[i])


import heapq
(N, M, s) = map(int, input().split())
Graph = [[] for _ in range(N + 1)]
dig = ['INF' for _ in range(N + 1)]
see = [False for _ in range(N + 1)]
for _ in range(M):
	(a, b, w) = map(int, input().split())
	Graph[a].append([w, b])
pq = []
dig[s] = 0
see[s] = True
for nv in Graph[s]:
	heapq.heappush(pq, nv)
while len(pq):
	v = heapq.heappop(pq)
	if see[v[1]]:
		continue
	see[v[1]] = True
	dig[v[1]] = v[0]
	for nv in Graph[v[1]]:
		if not see[nv[1]]:
			heapq.heappush(pq, [dig[v[1]] + nv[0], nv[1]])
for i in range(N):
	print(dig[i])


from heapq import heappop, heappush
(V, E, r) = map(int, input().split())
G = [[] for _ in range(V)]
for _ in range(E):
	(s, t, d) = map(int, input().split())
	G[s].append((t, d))
PQ = []
INF = float('inf')
dist = [INF] * V
white = 0
gray = 1
black = 2
color = [white] * V
heappush(PQ, (0, r))
dist[r] = 0
while PQ:
	(md, s) = heappop(PQ)
	color[s] = black
	if dist[s] < md:
		continue
	for (t, d) in G[s]:
		if color[t] == black:
			continue
		if dist[t] > dist[s] + d:
			dist[t] = dist[s] + d
			color[t] = gray
			heappush(PQ, (dist[t], t))
print('\n'.join(map(lambda x: 'INF' if x == INF else str(x), dist)))


import heapq

def main():
	(nvertices, nedges, s) = map(int, input().split())
	adj = [[] for i in range(nvertices)]
	for i in range(nedges):
		(u, v, w) = map(int, input().split())
		adj[u].append((v, w))
	INF = float('inf')
	costs = [INF] * nvertices
	que = [(0, s)]
	for i in range(1, nvertices):
		que.append((INF, 1))
	while que:
		(cost, u) = heapq.heappop(que)
		if cost > costs[u]:
			continue
		costs[u] = cost
		for (v, w) in adj[u]:
			if costs[v] > costs[u] + w:
				costs[v] = costs[u] + w
				heapq.heappush(que, (costs[v], v))
	for i in range(nvertices):
		print(costs[i] if costs[i] < INF else 'INF')
main()


(v, e, s) = map(int, input().split())
edge = []
inf = 1000000100
for i in range(v):
	edge.append([])
for i in range(e):
	(vs, vt, d) = map(int, input().split())
	edge[vs].append((vt, d))
dis = []
for i in range(v):
	dis.append(inf)
dis[s] = 0
for j in range(v):
	update = False
	for i in range(v):
		for (t, d) in edge[i]:
			if dis[i] < inf and d < inf and (dis[t] > dis[i] + d):
				dis[t] = dis[i] + d
				update = True
	if update == False:
		break
for i in range(v):
	if dis[i] >= inf:
		print('INF')
	else:
		print(dis[i])


import heapq
(n_vertex, n_edge, start) = [int(i) for i in input().split()]
min_costs = [float('inf')] * n_vertex
min_costs[start] = 0
adj_list = {i: set() for i in range(n_vertex)}
for _ in range(n_edge):
	(f, t, c) = [int(i) for i in input().split()]
	adj_list[f].add((t, c))
heap = [(0, start)]
while heap:
	(cost, pos) = heapq.heappop(heap)
	for (next_pos, weight) in adj_list[pos]:
		if cost + weight >= min_costs[next_pos]:
			continue
		min_costs[next_pos] = cost + weight
		heapq.heappush(heap, (min_costs[next_pos], next_pos))
for cost in min_costs:
	print(cost if cost < float('inf') else 'INF')


from collections import defaultdict

def main():
	fl = input().split(' ')
	V = int(fl[0])
	E = int(fl[1])
	R = int(fl[2])
	G = defaultdict(list)
	for i in range(int(E)):
		(s, t, w) = [int(x) for x in input().split(' ')]
		G[s].append((t, w))
	d = {}
	INF = float('inf')
	for i in range(V):
		d[i] = INF
	d[R] = 0
	q = [R]
	while q:
		u = q.pop(0)
		for v in G[u]:
			if d[v[0]] > d[u] + v[1]:
				d[v[0]] = d[u] + v[1]
				q.append(v[0])
	for k in d.keys():
		if d[k] == float('inf'):
			print('INF')
		else:
			print(d[k])
main()


(V, E, r) = map(int, input().split())
g = []
for i in range(E):
	(s, t, d) = map(int, input().split())
	g.append([s, t, d])
inf = 1001001001
dd = [inf] * V
dd[r] = 0
for i in range(V):
	update = 0
	for (s, t, d) in g:
		if dd[t] > dd[s] + d:
			dd[t] = dd[s] + d
			update += 1
	if update == 0:
		break
for i in dd:
	if i == inf:
		print('INF')
	else:
		print(i)


import heapq
l = input().split()
V = int(l[0])
E = int(l[1])
r = int(l[2])
graph = [[] for _ in range(V)]
for i in range(E):
	l = input().split()
	graph[int(l[0])].append((int(l[2]), int(l[1])))
heap = [(0, r)]
dist = [float('inf') for _ in range(V)]
while heap:
	pop = heapq.heappop(heap)
	if dist[pop[1]] < pop[0]:
		continue
	dist[pop[1]] = pop[0]
	for (w, i) in graph[pop[1]]:
		if i != pop[1] and w >= 0 and (dist[i] == float('inf')):
			heapq.heappush(heap, (pop[0] + w, i))
for d in dist:
	if d == float('inf'):
		print('INF')
	else:
		print(d)


import heapq

def dijkstra(g, s):
	queue = [[0, s]]
	v = len(g)
	d = [float('inf')] * v
	d[s] = 0
	while queue:
		(i, j) = heapq.heappop(queue)
		if d[j] > i:
			continue
		for e in g[j]:
			if d[e[0]] > d[j] + e[1]:
				d[e[0]] = d[j] + e[1]
				heapq.heappush(queue, [d[e[0]], e[0]])
	return d
(v, e, r) = map(int, input().split())
g = [[] for i in range(v)]
for i in range(e):
	(s, t, d) = map(int, input().split())
	g[s].append((t, d))
d = dijkstra(g, r)
for i in d:
	if i < float('inf'):
		print(i)
	else:
		print('INF')


from collections import defaultdict
from collections import deque
from sys import stdin

def sp(G, R, V):
	d = {}
	INF = float('inf')
	for i in range(V):
		d[i] = INF
	d[R] = 0
	q = deque([R])
	while q:
		u = q.popleft()
		for v in G[u]:
			if d[v[0]] > d[u] + v[1]:
				d[v[0]] = d[u] + v[1]
				q.append(v[0])
	return d
(V, E, R) = [int(x) for x in stdin.readline().split(' ')]
G = defaultdict(list)
for case in range(E):
	(s, t, w) = [int(x) for x in stdin.readline().split(' ')]
	G[s].append((t, w))
d = sp(G, R, V)
for k in range(V):
	if d[k] == float('inf'):
		print('INF')
	else:
		print(d[k])


import sys
from heapq import heappush
from heapq import heappop
(n_V, n_E, r) = map(int, input().split())
E = [[] for _ in range(n_V)]
for i in range(n_E):
	(v, e, c) = map(int, input().split())
	E[v].append((e, c))
D = [sys.maxsize for _ in range(n_V)]
D[r] = 0
h = []
heappush(h, (D[r], r))
while len(h) != 0:
	(_, index) = heappop(h)
	for (e, c) in E[index]:
		d = D[index] + c
		if D[e] > d:
			D[e] = d
			heappush(h, (d, e))
for d in D:
	if d == sys.maxsize:
		print('INF')
	else:
		print(d)


inf = 1001001001
(nv, ne, r) = map(int, input().split())
d = [inf] * nv
d[r] = 0
e = []
for i in range(ne):
	e.append(list(map(int, input().split())))
for i in range(nv - 1):
	flg = False
	for j in e:
		if d[j[1]] > d[j[0]] + j[2]:
			flg = True
		d[j[1]] = min(d[j[1]], d[j[0]] + j[2])
	if not flg:
		break
for i in range(nv):
	print('INF' if d[i] == inf else d[i])


import heapq
INF = float('inf')

def dijkstra(g, s):
	n = len(g)
	dist = [INF] * n
	dist[s] = 0
	hq = []
	heapq.heappush(hq, (dist[s], s))
	while hq:
		(cost, u) = heapq.heappop(hq)
		if dist[u] < cost:
			continue
		for (v, c) in g[u]:
			if dist[v] <= dist[u] + c:
				continue
			dist[v] = dist[u] + c
			heapq.heappush(hq, (dist[v], v))
	return dist
(V, E, r) = map(int, input().split())
g = [[] for _ in range(V)]
for i in range(E):
	(s, t, d) = map(int, input().split())
	g[s].append((t, d))
res = dijkstra(g, r)
for i in res:
	print('INF' if i == INF else i)


from heapq import heappush, heappop
INF = 1e+18

def dijkstra(st, g):
	n = len(g)
	dis = [INF] * n
	dis[st] = 0
	que = []
	heappush(que, (0, st))
	while que:
		(d, u) = heappop(que)
		for (v, cost) in g[u]:
			if dis[v] > d + cost:
				dis[v] = d + cost
				heappush(que, (dis[v], v))
	return dis

def main():
	(n, m, st) = map(int, input().split())
	g = [[] for _ in range(n)]
	for _ in range(m):
		(u, v, cost) = map(int, input().split())
		g[u].append((v, cost))
	res = dijkstra(st, g)
	for ans in res:
		if ans == INF:
			print('INF')
		else:
			print(ans)
main()


def E_dijkstra(V, AL, S):
	from heapq import heappush, heappop
	INF = float('inf')
	ans = [INF] * V
	ans[S] = 0
	used = [0] * V
	used[S] = 1
	que = []
	for i in range(V):
		heappush(que, (ans[i], i))
	while que:
		(x, u) = heappop(que)
		used[u] = 1
		for (v, w) in AL[u]:
			if not used[v] and ans[v] > x + w:
				ans[v] = x + w
				heappush(que, (ans[v], v))
	return ans
import sys
input = sys.stdin.readline
(V, E, R) = map(int, input().split())
AL = [[] for i in range(V)]
for _ in range(E):
	(S, T, D) = map(int, input().split())
	AL[S].append((T, D))
for ans in E_dijkstra(V, AL, R):
	print('INF' if ans == float('inf') else ans)


(v_num, e_num, r) = map(int, input().split())
INF = (10000 + 1) * v_num
dist = [INF] * v_num
G = [[] for _ in range(v_num)]
for _ in range(e_num):
	(s, t, d) = tuple(map(int, input().split()))
	G[s].append((t, d))
from heapq import heappush, heappop
q = []
heappush(q, (0, r))
dist[r] = 0
while q:
	(d, node) = heappop(q)
	for (to, cost) in G[node]:
		if dist[to] > d + cost:
			dist[to] = d + cost
			heappush(q, (d + cost, to))
for d in dist:
	if d == INF:
		print('INF')
	else:
		print(d)


import heapq
(V, E, start) = map(int, input().split())
G = [[] for _ in range(V)]
for i in range(E):
	(si, ti, di) = map(int, input().split())
	G[si].append((ti, di))
d = [float('INF')] * V
used = [False] * V
prev = [-1] * V

def Dijkstra(s):
	d[s] = 0
	q = []
	heapq.heappush(q, (0, s))
	while len(q) > 0:
		(dist, v) = heapq.heappop(q)
		if d[v] < dist:
			continue
		for (j, cost) in G[v]:
			if d[j] > d[v] + cost:
				d[j] = d[v] + cost
				heapq.heappush(q, (d[j], j))
				prev[j] = v
Dijkstra(start)
for i in range(V):
	if d[i] == float('inf'):
		print('INF')
	else:
		print(d[i])


import heapq
import string
INF = 1000000000.0

def dijkstra(graph, start=0, goal=None):
	min_cost = [INF for _ in range(len(graph))]
	min_cost[start] = 0
	que = [(0, start)]
	result = INF
	while len(que) > 0:
		(cur_cost, cur_node) = heapq.heappop(que)
		if goal is not None and cur_node == goal:
			result = cur_cost
			break
		for (to, cost) in graph[cur_node]:
			new_cost = cur_cost + cost
			if new_cost >= min_cost[to]:
				continue
			min_cost[to] = new_cost
			heapq.heappush(que, (new_cost, to))
	return min_cost

def main():
	(n, m, r) = map(int, input().split())
	graph = [[] for _ in range(n)]
	for _ in range(m):
		(u, v, cost) = map(int, input().split())
		graph[u].append((v, cost))
	min_cost = dijkstra(graph, r)
	for c in min_cost:
		print(c if c < INF else 'INF')
main()


import heapq
VEr = input().split()
(V, E, r) = (int(VEr[0]), int(VEr[1]), int(VEr[2]))
List = [[] for i_ in range(V)]
heap = []
result = [float('inf')] * V
for i in range(E):
	(s, t, d) = map(int, input().split())
	List[s].append([t, d])
result[r] = 0
heapq.heappush(heap, [0, r])
while len(heap) != 0:
	temp = heapq.heappop(heap)
	cost = temp[0]
	basyo = temp[1]
	if cost > result[basyo]:
		continue
	else:
		for i in List[basyo]:
			if result[i[0]] > result[basyo] + i[1]:
				result[i[0]] = result[basyo] + i[1]
				heapq.heappush(heap, [result[i[0]], i[0]])
for i in result:
	if i == float('inf'):
		print('INF')
	else:
		print(i)


import heapq

def dijkstra(s):
	d = [float('inf')] * n
	d[s] = 0
	used = [False] * n
	used[s] = True
	que = []
	for e in g[s]:
		heapq.heappush(que, e)
	while que:
		minedge = heapq.heappop(que)
		if used[minedge[1]]:
			continue
		v = minedge[1]
		d[v] = minedge[0]
		used[v] = True
		for e in g[v]:
			if not used[e[1]]:
				heapq.heappush(que, [e[0] + d[v], e[1]])
	return d
(n, w, r) = [int(x) for x in input().split()]
g = [[] for _ in range(n)]
for _ in range(w):
	(x, y, z) = [int(x) for x in input().split()]
	g[x].append([z, y])
for i in dijkstra(r):
	if i == float('inf'):
		print('INF')
	else:
		print(i)


import heapq
(nv, ne, r) = map(int, input().split())
edges = [set() for _ in range(nv)]
costs = [-1] * nv
while ne:
	(s, t, d) = map(int, input().split())
	edges[s].add((d, t))
	ne -= 1
queue = list(edges[r])
heapq.heapify(queue)
costs[r] = 0
while queue:
	(total_cost, idx) = heapq.heappop(queue)
	if costs[idx] > -1:
		continue
	costs[idx] = total_cost
	for (cost, idx2) in edges[idx]:
		if costs[idx2] == -1:
			heapq.heappush(queue, (total_cost + cost, idx2))
for cost in costs:
	print(cost if cost > -1 else 'INF')


import heapq
(N, M, r) = map(int, input().split())
inf = 10 ** 18

def dijkstra(start):
	q = [(0, start)]
	heapq.heapify(q)
	cost = [inf] * N
	cost[start] = 0
	while q:
		(c, v) = heapq.heappop(q)
		if c > cost[v]:
			continue
		for (d, u) in G[v]:
			tmp = d + cost[v]
			if tmp < cost[u]:
				cost[u] = tmp
				heapq.heappush(q, (tmp, u))
	return cost
G = [[] for _ in range(N)]
for _ in range(M):
	(a, b, t) = map(int, input().split())
	G[a].append((t, b))
res = dijkstra(r)
for i in res:
	if i == inf:
		print('INF')
		continue
	print(i)


from collections import defaultdict

def main():
	fl = input().split(' ')
	V = int(fl[0])
	E = int(fl[1])
	R = int(fl[2])
	G = defaultdict(list)
	for i in range(int(E)):
		(s, t, w) = [int(x) for x in input().split(' ')]
		G[s].append((t, w))
	d = {}
	INF = float('inf')
	for i in range(V):
		d[i] = INF
	d[R] = 0
	q = [R]
	while q:
		u = q.pop(0)
		for v in G[u]:
			if d[v[0]] > d[u] + v[1]:
				d[v[0]] = d[u] + v[1]
				q.append(v[0])
	for k in range(V):
		if d[k] == float('inf'):
			print('INF')
		else:
			print(d[k])
main()


import heapq

def dijkstra_heap(s, edge):
	d = [10 ** 20] * n
	used = [True] * n
	d[s] = 0
	used[s] = False
	edgelist = []
	for (a, b) in edge[s]:
		heapq.heappush(edgelist, a * 10 ** 6 + b)
	while len(edgelist):
		minedge = heapq.heappop(edgelist)
		if not used[minedge % 10 ** 6]:
			continue
		v = minedge % 10 ** 6
		d[v] = minedge // 10 ** 6
		used[v] = False
		for e in edge[v]:
			if used[e[1]]:
				heapq.heappush(edgelist, (e[0] + d[v]) * 10 ** 6 + e[1])
	for (i, j) in enumerate(used):
		if j:
			d[i] = -1
	return d
(n, w, r) = map(int, input().split())
edge = [[] for i in range(n)]
for i in range(w):
	(x, y, z) = map(int, input().split())
	edge[x].append([z, y])
for i in dijkstra_heap(r, edge):
	if i == -1:
		print('INF')
	else:
		print(i)


import heapq

def main():
	(nvertices, nedges, s) = map(int, input().split())
	adj = [[] for i in range(nvertices)]
	for i in range(nedges):
		(u, v, w) = map(int, input().split())
		adj[u].append((v, w))
	INF = float('inf')
	costs = [INF] * nvertices
	costs[s] = 0
	que = [(0, s)]
	for i in range(1, nvertices):
		que.append((INF, 1))
	while que:
		(cost, u) = heapq.heappop(que)
		if cost > costs[u]:
			continue
		costs[u] = cost
		for (v, w) in adj[u]:
			if costs[v] > costs[u] + w:
				costs[v] = costs[u] + w
				heapq.heappush(que, (costs[v], v))
	for i in range(nvertices):
		print(costs[i] if costs[i] < INF else 'INF')
main()


import heapq
(v, e, r) = map(int, input().split())
graph = [[] for _ in range(v)]
for _ in range(e):
	(s, t, d) = map(int, input().split())
	graph[s].append((t, d))
d = [float('inf')] * v
d[r] = 0
hq = [(0, r)]
heapq.heapify(hq)
while hq:
	(cost, now) = heapq.heappop(hq)
	if d[now] < cost:
		continue
	for nei in graph[now]:
		to = nei[0]
		c = nei[1]
		if d[to] > d[now] + c:
			d[to] = d[now] + c
			heapq.heappush(hq, (d[to], to))
for i in range(v):
	if d[i] == float('inf'):
		print('INF')
	else:
		print(d[i])


from pprint import pprint
import heapq
import sys
sys.setrecursionlimit(10 ** 6)
INF = float('inf')
(N, M, source) = map(int, input().split())
adj_list = [[] for v in range(N)]
for _ in range(M):
	(u, v, w) = map(int, input().split())
	adj_list[u].append([v, w])

def dijkstra(n_nodes, adj_list, source):
	dist = [0 if v == source else INF for v in range(n_nodes)]
	priority_queue = [[dist[source], source]]
	while priority_queue:
		(current_distance, current) = heapq.heappop(priority_queue)
		if current_distance > dist[current]:
			continue
		for (neighbor, distance) in adj_list[current]:
			if current_distance + distance < dist[neighbor]:
				dist[neighbor] = current_distance + distance
				heapq.heappush(priority_queue, [dist[neighbor], neighbor])
	return dist
dist = dijkstra(N, adj_list, source)
for v in range(N):
	if dist[v] == INF:
		print('INF')
	else:
		print(dist[v])