#include<stdio.h>
#include<stdlib.h>
#include<limits.h>
int *dist, *prev, *visit, **matrix;
void Dijkstra(int, int);
main()
{
        int i, j, v, e, r, s, t, d;
        scanf("%d%d%d", &v, &e, &r);
        dist = (int*)malloc(sizeof(int) * v);
        prev = (int*)malloc(sizeof(int) * v);
        visit = (int*)malloc(sizeof(int) * v);
        matrix = (int**)malloc(sizeof(int*) * v);
        for (i = 0; i < v; i++) {
                matrix[i] = (int*)malloc(sizeof(int) * v);
                for (j = 0; j < v; j++) {
                        matrix[i][j] = -1;
                }
                dist[i] = INT_MAX;
                prev[i] = -1;
                visit[i] = 0;
        }
        for (i = 0; i < e; i++) {
                scanf("%d%d%d", &s, &t, &d);
                matrix[s][t] = d;
        }
        Dijkstra(r, v);
        for (i = 0; i < v; i++) {
        	if (dist[i] == INT_MAX) {
        		printf("INF\n");
        	} else {
            	printf("%d\n", dist[i]);
        	}
            free(matrix[i]);
        }
        free(matrix);
        free(dist);
        free(prev);
        free(visit);

}
void Dijkstra(int p, int size)
{
        dist[p] = 0;
        while (1) {
                int i, u = -1, min = INT_MAX;
                for (i = 0; i < size; i++) {
                        if (!visit[i] && dist[i] < min) {
                                u = i;
                                min = dist[i];
                        }
                }
                if (u == -1) break;
                visit[u] = 1;
                for (i = 0; i < size; i++) {
                        if (matrix[u][i] >= 0) {
                                int w = matrix[u][i], newlen = dist[u] + w;
                                if (newlen < dist[i]) {
                                        dist[i] = newlen;
                                        prev[i] = u;
                                }
                        }
                }
        }
}
