#include<stdio.h>
#include<stdlib.h>
struct node{
  int key;
  int cost;
  struct node *next;
};
typedef struct node *GG;
int d[100002],H[100002],v,heapS=0;
GG G[100002];
GG MNode(int,int,GG);
void Insert(int,int,int);
int HEmin();
void minHeapify(int);
void HI(int);
void Increase(int,int);
int main()
{
  int i,j,n,count=0,c,c1,v1,e,r;
  int color[100002],pi[100002],mincost,ii;
  GG GGG;
  scanf("%d%d%d",&v,&e,&r);
  for(i=1;i<v+1;i++)G[i]=MNode(0,0,NULL);
  for(i=0;i<e;i++){
    scanf("%d%d%d",&n,&v1,&c1);
    Insert(n+1,v1+1,c1);
  }
  for(i=1;i<v+1;i++){
    color[i]=0;
    pi[i]=-1;
    d[i]=10000001;
  }
  d[r+1]=0;
  HI(r+1);
  while(heapS>=1){
    ii=HEmin();
    color[ii]=2;
    for(GGG=G[ii]->next;GGG!=NULL;GGG=GGG->next){
      if(color[GGG->key]!=2 && (GGG->cost+d[ii])<d[GGG->key]){
	pi[GGG->key]=ii;
	d[GGG->key]=GGG->cost+d[ii];
	color[GGG->key]=1;
	HI(GGG->key);
      }
    }
  }
  for(i=1;i<v+1;i++){
    if(d[i]!=10000001)printf("%d\n",d[i]);
    else printf("INF\n");
  }
  
  return 0;
}
 
GG MNode(int key,int cost,GG Node)
{
  GG n;
  n=(GG)malloc(sizeof(struct node));
  n->key=key;
  n->cost=cost;
  n->next=Node;
  return n;
}
void Insert(int s,int t,int u)
{
  GG GGG;
  GGG=MNode(t,u,G[s]->next);
  G[s]->next=GGG;
}

int HEmin()
{
  int min=H[1];
  H[1]=H[heapS];
  heapS--;
  minHeapify(1);
  return min;
}

void minHeapify(int i)
{
  int r,l,small,tmp;
  l=i*2;
  r=i*2+1;

  if(l<=heapS && d[H[l]]>d[H[i]])small=l;
  else small=i;

  if(r<=heapS && d[H[r]]>d[H[small]])small=r;

  if(small!=i){
    tmp=H[i];
    H[i]=H[small];
    H[small]=tmp;
    minHeapify(small);
  }
}
void HI(int key)
{
  heapS+=1;
  H[heapS]=-1;
  Increase(heapS,key);
}
void Increase(int i,int key)
{
  int tmp;
  H[i]=key;
  while(i>1 && d[H[i/2]]>d[H[i]]){
    tmp=H[i];
    H[i]=H[i/2];
    H[i/2]=tmp;
    i=i/2;
  }
}