#include<bits/stdc++.h>
using namespace std;
struct Edge{
int v,e;
struct Edge* next;
};
const int N=1e5+9;
char flag[N];
int l[N];
struct rule{
bool operator()(const int& a,const int& b){
return l[a]>l[b];
}
};
int main(){
memset(l,0x3f,sizeof(l));
memset(flag,false,sizeof(flag));
int n,m,s,k,count=0;
cin>>n>>m>>s;
vector<Edge*> v(n+9,NULL);
priority_queue<int,vector<int>,rule> q;
l[s]=0;
q.push(s);
for(int i=0;i<m;i++){
int v1,v2,e;
cin>>v1>>v2>>e;
if(!v[v1]){
v[v1]=new Edge;
v[v1]->v=v2;
v[v1]->e=e;
v[v1]->next=NULL;
}
else{
Edge* p=new Edge;
p->v=v2;
p->e=e;
p->next=v[v1]->next;
v[v1]->next=p;
}
}
while(1){
int k=q.top();
if(flag[k]){
q.pop();
continue;
}
Edge* p=v[k];
while(p){
if(flag[p->v]==false){
l[p->v]=min(l[k]+p->e,l[p->v]);
q.push(p->v);
}
p=p->next;
}
flag[k]=true;
q.pop();
count++;
if(count==n)
break;
}
for(int i=1;i<=n;i++){
cout<<l[i]<<' ';
}
return 0;
}