// AOJ GRL_1_A Shortest Path - Single Source Shortest Path
// 2018.4.26 bal4u

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define QMAX 100000
typedef struct { int t, s; } QUE;
QUE que[QMAX]; int qsize;

#define PARENT(i) ((i)>>1)
#define LEFT(i)   ((i)<<1)
#define RIGHT(i)  (((i)<<1)+1)

void min_heapify(int i)
{
	int l, r, min;
	QUE qt;

	l = LEFT(i), r = RIGHT(i);
	if (l < qsize && que[l].t < que[i].t) min = l; else min = i;
	if (r < qsize && que[r].t < que[min].t) min = r;
	if (min != i) {
		qt = que[i], que[i] = que[min], que[min] = qt;
		min_heapify(min);
	}
}

void deq()
{
	que[0] = que[--qsize];
	min_heapify(0);
}

void enq(int s, int t)
{
	int i, min;
	QUE qt;

	i = qsize++;
	que[i].s = s, que[i].t = t;
	while (i > 0 && que[min = PARENT(i)].t > que[i].t) {
		qt = que[i], que[i] = que[min], que[min] = qt;
		i = min;
	}
}

#if 1
#define gc() getchar_unlocked()
#define pc(c) putchar_unlocked(c)
#else
#define gc() getchar()
#define pc(c) putchar(c)
#endif
int in()
{
	int n = 0, c = gc();
	do n = 10*n + (c & 0xf), c = gc(); while (c >= '0');
	return n;
}

#define INF    0x70707070
#define MAX_V  100002
int V;         // 頂点数
int hi[MAX_V], *to[MAX_V], *d[MAX_V];
int dist[MAX_V];

void dijkstra(int start)
{
	int i, s, e, t, nt;

	memset(dist, INF, V<<2);
	qsize = 0;
	dist[start] = 0, enq(start, 0);
	while (qsize) {
		s = que[0].s, t = que[0].t, deq();
		if (dist[s] < t) continue;
		for (i = 0; i < hi[s]; i++) {
			e = to[s][i], nt = t + d[s][i];
			if (dist[e] > nt) dist[e] = nt, enq(e, nt);
		}
	}
}

char buf[20];
void mypr(int n)
{
	int i = 0;
	if (n == 0) buf[i++] = '0';
	else while (n) buf[i++] = n%10 + '0', n/=10;
	while (i) pc(buf[--i]);
	pc('\n');
}

int main()
{
	int E, r, i, j, k, s;
	int *memo;

	V = in(), E = in(), r = in();
	memo = malloc(sizeof(int)*3*E);
	j = 0; for (i = 0; i < E; i++) {
		memo[j++] = s = in(), hi[s]++;
		memo[j++] = in(), memo[j++] = in();
	}
	for (i = 0; i < V; i++) {
		if (hi[i]) to[i] = malloc(hi[i]<<2), d[i] = malloc(hi[i]<<2);
	}
	memset(hi, 0, V<<2);
	j = 0; while (E--) {
		s = memo[j++];
		k = hi[s]++, to[s][k] = memo[j++], d[s][k] = memo[j++];
	}
	free(memo);
	dijkstra(r);
	for (i = 0; i < V; i++) {
		if (dist[i] == INF) pc('I'), pc('N'), pc('F'), pc('\n');
		else mypr(dist[i]);
	}
	return 0;
}
