#include<bits/stdc++.h>
using namespace std;
int ans[100001];
bool bj[100001];
int n,m,q,tot=0;
struct Node{
int to,w;
};
Node b[100001];
vector <Node> a[100001];
void work(int x){
if(x/2>=1&&b[x].w<b[x/2].w){
swap(b[x].w,b[x/2].w);
work(x/2);
}
}
void work1(int x){
if(b[x*2].w<b[x*2+1].w&&x*2+1<=tot&&b[x*2].w<b[x].w){
swap(b[x*2].w,b[x].w);
work1(x*2);
}
else if(x*2+1<=tot&&b[x*2+1].w<b[x].w){
swap(b[x*2+1],b[x]);
work1(x*2+1);
}
else if(x*2+1>tot&&x*2<=tot&&b[x*2].w<b[x].w){
swap(b[x*2],b[x]);
work1(x*2);
}
else{
return;
}
}
void bfs(int i,int js){
if(bj[i]){
return;
}
bj[i]=1;
ans[i]=js;
bj[i]=1;
for(auto it:a[i]){
if(!bj[it.to]){
b[++tot].w=it.w+js;
b[tot].to=it.to;
work(tot);
}
}
}
int main(){
scanf("%d%d%d",&n,&m,&q);
for(int i=1;i<=m;i++){
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
a[u].push_back({v,w});
}
b[++tot]={q,0};
while(tot!=0){
bfs(b[1].to,b[1].w);
swap(b[1],b[tot]);
tot--;
work1(1);
}
for(int i=1;i<=n;i++){
printf("%d ",ans[i]);
}
return 0;
}