#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+10;
int n,m,s;
int h[maxn],ne[maxn],e[maxn],w[maxn],idx;
int dist[maxn],cnt[maxn];
bool st[maxn];
void add(int a,int b,int c)
{
e[idx]=b,w[idx]=c,ne[idx]=h[a],h[a]=idx++;
}
void spfa()
{
memset(dist,0x3f,sizeof dist);
dist[s]=0;
queue<int>q;
q.push(s);
while(!q.empty())
{
int t=q.front();
q.pop();
st[t]=false;
for(int i=h[t];i!=-1;i=ne[i])
{
int j=e[i];
if(dist[j]>dist[t]+w[i])
{
dist[j]=dist[t]+w[i];
if(!st[j])
{
q.push(j);
st[j]=true;
}
}
}
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
cin>>n>>m>>s;
memset(h,-1,sizeof h);
while(m--)
{
int a,b,c;
cin>>a>>b>>c;
add(a,b,c);
}
spfa( );
for (int i = 1; i <= n; i++) {
cout << dist[i] << " ";
}
return 0;
}