#include<stdio.h>
#include<stdlib.h>
#define LARGE 100000
#define INF 2100000000
#define MIN(a, b) ((a) < (b) ? (a) : (b))

typedef struct edge{
	int to_node;
	int cost;
	struct edge *next;
}edge;

typedef struct node{
	int cost;
	int flg;
}node;

int V;
node N[LARGE];
edge* E[LARGE];


edge * Make_Edge(int t,int d){
	edge *new=malloc(sizeof(edge));
	new->to_node=t;
	new->cost=d;
	new->next=NULL;
	return new;
}

void Insert(int s,edge* new){
	edge **p=&E[s];

	while((*p)!=NULL){
		p=&((*p)->next);
	}
	(*p)=new;
	
	return;
}


void Dijkstra(int start,int d){
	int min_idx=-1;
	int min=INF+1;
	int i;
	edge *p;
	
	if(d==V-1)return;
	
	if(d==0){
		N[start].cost=0;
		N[start].flg=1;
	}
	
	
	p=E[start];
	
	while(p!=NULL){
		if(N[p->to_node].flg==0){
			if(N[p->to_node].cost > N[start].cost+(p->cost)){
				N[p->to_node].cost=N[start].cost+(p->cost);
				if(min > N[p->to_node].cost){
					min=N[p->to_node].cost;
					min_idx=p->to_node;
				}
			}
		}
		p=p->next;
	}
	
	if(min_idx==-1){
		for(i=0;i<V;i++){
		if(N[i].flg==0){
			min_idx=i;
			break;
		}
}
		
	}
	
	
	N[min_idx].flg=1;
	
	Dijkstra(min_idx,d+1);
	
	return;
	
}


int main(){
	int start;
	int s,t,d;
	int n,e;
	int i,j;
	edge* new;
	
	scanf("%d %d %d",&V,&e,&start);
	
	/*初期化*/
	for(i=0;i<V;i++){
		E[i]=NULL;
		N[i].cost=INF;
		N[i].flg=0;
	}
	
	/*隣接リスト作成*/
	for(i=0;i<e;i++){
		scanf("%d %d %d",&s,&t,&d);
		new=Make_Edge(t,d);
		Insert(s,new);
	}
	if(e==0)N[start].cost=0;
	
	Dijkstra(start,0);
	
	for(i=0;i<V;i++){
		if(N[i].cost==INF)printf("INF\n");
		else printf("%d\n",N[i].cost);
	}
	
	return 0;
}
