#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int>PII;
const int N=100010;
int n,m,k;
int h[N],w[N],e[N],ne[N],idx,dist[N];
bool st[N];
void add(int a,int b,int c)
{
e[idx]=b,w[idx]=c,ne[idx]=h[a],h[a]=idx++;
}
void dijkstra()
{
memset(dist,0x3f,sizeof dist);
dist[k]=0;
priority_queue<PII,vector<PII>,greater<PII>> heap;
heap.push({0,k});
while(heap.size())
{
auto t=heap.top();
heap.pop();
int ver=t.second,distance=t.first;
if(st[ver]) continue;
st[ver]=true;
for(int i=h[ver];i!=-1;i=ne[i])
{
int j=e[i];
if(dist[j]>dist[ver]+w[i])
{
dist[j]=dist[ver]+w[i];
heap.push({dist[j],j});
}
}
}
}
int main()
{
cin>>n>>m>>k;
memset(h,-1,sizeof h);
while(m--)
{
int a,b,c;
scanf("%d %d %d",&a,&b,&c);
add(a,b,c);
}
dijkstra();
for(int i=1;i<=n;i++)
cout<<dist[i]<<" ";
return 0;
}