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

typedef long long int int64;

#define MAX(a,b) ((a)>(b)?(a):(b))
#define MIN(a,b) ((a)<(b)?(a):(b))
#define ABS(a) ((a)>(0)?(a):-(a))

const int inf=1000000007;

typedef struct heapNode{
  int v;
  int val;
} node;

int cmp(const node *a,const node *b){
  return a->val<=b->val;
}

void swap(node *a,node *b){
  node t=*a;
  *a=*b;
  *b=t;
  return;
}

void push(node *h,int v,int val){
  h[0].v++;
  int k=h[0].v;
  h[k].v=v;
  h[k].val=val;
  while(k>1){
    if(cmp(h+k/2,h+k)) return;
    swap(h+k/2,h+k);
    k/=2;
  }
  return;
}

int pop(node *h){
  int res=h[1].v;
  h[1]=h[h[0].v--];
  int k=1;
  int n=h[0].v;
  while(2*k+1<=n){
    if(cmp(h+k,h+2*k) && cmp(h+k,h+2*k+1)) break;
    int next=cmp(h+2*k,h+2*k+1)?2*k:2*k+1;
    swap(h+k,h+next);
    k=next;
  }
  if(2*k<=n && !cmp(h+k,h+2*k)) swap(h+k,h+2*k);
  return res;
}

void dijkstra(int from,int *start,int *vertex,int *cost,int *next,int *path,int v){
  int *used=(int *)malloc(sizeof(int)*v);
  int i;
  for(i=0;i<v;i++){
    path[i]=inf;
    used[i]=0;
  }
  node *h=(node *)malloc(sizeof(node)*500000);
  h[0].v=0;
  path[from]=0;
  push(h,from,0);
  while(h[0].v>0){
    int s=pop(h);
    if(used[s]) continue;
    used[s]=1;
    int p=start[s];
    while(p>0){
      int t=vertex[p];
      if(path[s]+cost[p]<path[t]){
	path[t]=path[s]+cost[p];
	push(h,t,path[t]);
      }
      p=next[p];
    }
  }
  free(h);
  free(used);
  return;
}

void run(void){
  int v,e,r;
  scanf("%d%d%d",&v,&e,&r);
  int *start=(int *)malloc(sizeof(int)*v);
  int *vertex=(int *)malloc(sizeof(int)*(e+1));
  int *cost=(int *)malloc(sizeof(int)*(e+1));
  int *next=(int *)malloc(sizeof(int)*(e+1));
  int i;
  for(i=0;i<v;i++) start[i]=0;
  for(i=1;i<=e;i++){
    int s,t,d;
    scanf("%d%d%d",&s,&t,&d);
    vertex[i]=t;
    cost[i]=d;
    next[i]=start[s];
    start[s]=i;
  }
  int *path=(int *)malloc(sizeof(int)*v);
  dijkstra(r,start,vertex,cost,next,path,v);
  for(i=0;i<v;i++){
    if(path[i]==inf){
      printf("INF\n");
    } else {
      printf("%d\n",path[i]);
    }
  }
}

int main(void){
  run();
  return 0;
}

