#include<bits/stdc++.h>
using namespace std;
const int N=5*1e5+10;
int n,m,s;
int d[N];
int h[N],e[N],ne[N],w[N],idx;
bool st[N];
void add(int a,int b,int c)
{
e[idx]=b;
ne[idx]=h[a];
w[idx]=c;
h[a]=idx++;
}
void spfa(int s)
{
memset(d,0x3f,sizeof d);
d[s]=0;
queue<int>q;
q.push(s);
st[s]=true;
while(q.size())
{
auto t = q.front();
q.pop();
for (int i = h[t]; i != -1; i = ne[i])
{
int j = e[i];
if (d[j] > d[t] + w[i])
{
d[j] = d[t] + w[i];
if (!st[j])
{
q.push(j);
st[j] = true;
}
}
}
}
for(int i=1;i<=n;i++)
{
if(d[i]==0x3f3f3f3f)cout<<INT_MAX<<" ";
else cout<<d[i]<<" ";
}
}
int main()
{
memset(h,-1,sizeof h);
cin>>n>>m>>s;
while(m--)
{
int x,y,z;
cin>>x>>y>>z;
add(x,y,z);
}
spfa(s);
return 0;
}