#include<bits/stdc++.h>
using namespace std;
int n,m,u,v,l,s;
queue<int> q;
const int N=5e5+5;
const int R=2147483646;
int dis[N];
int pre[N];
bool vis[N];
struct edge{
int from,to,next,val;
}a[N];
int tot;
void add(int from,int to,int val){ /*存图*/
a[++tot].from=from;
a[tot].to=to;
a[tot].next=pre[from];
pre[from]=tot;
a[tot].val=val;
}
int main(){
scanf("%d%d%d",&n,&m,&s);
memset(dis,0x3f,sizeof(dis));
for(int i=1;i<=m;i++){
scanf("%d%d%d",&u,&v,&l);
add(u,v,l);
}
dis[s]=0; /*原点标记一下*/
q.push(s);
while(!q.empty()){
int x=q.front();/*队首*/
for(int i=pre[x];i!=0;i=a[i].next){
int y=a[i].to;/*可以到的点*/
if(dis[x]+a[i].val<dis[y]){
//当该路线比原来路线更短时,更新数组
dis[y]=dis[x]+a[i].val;
if(!vis[x]){
q.push(y);
}
}
}
q.pop();
}
for(int i=1;i<=n;i++){
if(i!=s&&dis[i]==0x3f3f3f3f)printf("%d ",R);
printf("%d ",dis[i]);
}
return 0;
}