Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-
V,E,r = map(int,raw_input().split())
edges = [[] for i in range(V)]

for i in xrange(E):
	(a,b,w) = map(int,raw_input().split())
	edges[a].append({'to':b,'weight':w})

INF = 2000000000

distance = [INF] * V	# A??????????????¢
visited = [False] * V	# ??????????¨??????????????????????

distance[r] = 0
for i in range(V):
	min_v = -1
	for v in range(V):
		if not visited[v] and (min_v == -1 or distance[v] < distance[min_v]):
			min_v = v
	visited[min_v] = True
	for e in edges[min_v]:
		distance[e['to']] = min(distance[min_v] + e['weight'],distance[e['to']])

for d in distance:
	print 'INF' if d == INF else d

Fixed submission:
# -*- coding: utf-8 -*-
import Queue
INF = 10000000000000000

# ?¨??????\???????????\????????¢?????§????????????
# |V| |E|
# ai bi wi
V,E,r = map(int,raw_input().split())
edges = [[] for i in range(V)]	# ??£??\?????????

for i in xrange(E):
	a,b,w = map(int,raw_input().split())
	edges[a].append({'to':b,'weight':w})
	# edges[b].append({'to':a,'weight':w})

distance = [INF] * V
visited = [False] * V

distance[r] = 0
pq = Queue.PriorityQueue()
pq.put((0,r))
while not pq.empty():
	d,v = pq.get()
	visited[v] = True
	for e in edges[v]:
		if visited[e['to']] or distance[v]+e['weight'] > distance[e['to']]: continue
		distance[e['to']] = distance[v] + e['weight']
		pq.put((d+e['weight'],e['to']))

for d in distance:
	if d >= INF:
		print "INF"
	else:
		print d

# for i in range(V):
# 	min_v = -1
# 	for v in range(V):
# 		if not visited[v] and (min_v == -1 or distance[v] < distance[min_v]):
# 			min_v = v

# 	visited[min_v] = True
# 	for e in edges[min_v]:
# 		distance[e['to']] = min(distance[min_v] + e['weight'],distance[e['to']])

# for d in distance:
# 	if d >= INF:
# 		print "INF"
# 	else:
# 		print d

Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-
INF = 1000000

# ?¨??????\???????????\????????¢?????§????????????
# |V| |E|
# ai bi wi
V,E,r = map(int,raw_input().split())
edges = [[] for i in range(V)]	# ??£??\?????????

for i in xrange(E):
	a,b,w = map(int,raw_input().split())
	edges[a].append({'to':b,'weight':w})
	# edges[b].append({'to':a,'weight':w})

distance = [INF] * V
visited = [False] * V

distance[r] = 0
for i in range(V):
	min_v = -1
	for v in range(V):
		if not visited[v] and (min_v == -1 or distance[v] < distance[min_v]):
			min_v = v

	visited[min_v] = True
	for e in edges[min_v]:
		distance[e['to']] = min(distance[min_v] + e['weight'],distance[e['to']])

for d in distance:
	if d >= INF:
		print "INF"
	else:
		print d

Fixed submission:
# -*- coding: utf-8 -*-
import Queue
INF = 10000000000000000

# ?¨??????\???????????\????????¢?????§????????????
# |V| |E|
# ai bi wi
V,E,r = map(int,raw_input().split())
edges = [[] for i in range(V)]	# ??£??\?????????

for i in xrange(E):
	a,b,w = map(int,raw_input().split())
	edges[a].append({'to':b,'weight':w})
	# edges[b].append({'to':a,'weight':w})

distance = [INF] * V
visited = [False] * V

distance[r] = 0
pq = Queue.PriorityQueue()
pq.put((0,r))
while not pq.empty():
	d,v = pq.get()
	visited[v] = True
	for e in edges[v]:
		if visited[e['to']] or distance[v]+e['weight'] > distance[e['to']]: continue
		distance[e['to']] = distance[v] + e['weight']
		pq.put((d+e['weight'],e['to']))

for d in distance:
	if d >= INF:
		print "INF"
	else:
		print d

# for i in range(V):
# 	min_v = -1
# 	for v in range(V):
# 		if not visited[v] and (min_v == -1 or distance[v] < distance[min_v]):
# 			min_v = v

# 	visited[min_v] = True
# 	for e in edges[min_v]:
# 		distance[e['to']] = min(distance[min_v] + e['weight'],distance[e['to']])

# for d in distance:
# 	if d >= INF:
# 		print "INF"
# 	else:
# 		print d