#include<bits/stdc++.h>
using namespace std;
const int inf=0x3f3f3f3f,N=500005;
int n,m,s,cnt,head[N],vis[N],dis[N];
struct node
{
int dis,pos;
bool operator <(const node &x)const
{
return x.dis<dis;
}
};
struct edge
{
int next,to,dis;
}e[N];
void add(int u,int v,int w)
{
e[++cnt].next=head[u];
e[cnt].to=v;
e[cnt].dis=w;
head[u]=cnt;
}
priority_queue<node> q;
void dijkstra()
{
for(int i=1;i<=n;i++)dis[i]=inf;
dis[s]=0;
q.push((node){0,s});
while(!q.empty())
{
node tmp=q.top();
q.pop();
int d=tmp.dis,pos=tmp.pos;
if(vis[pos]) continue;
vis[pos]=1;
for(int i=head[pos];i;i=e[i].next)
{
int v=e[i].to;
if(dis[pos]+e[i].dis<dis[v])
{
dis[v]=dis[pos]+e[i].dis;
if(!vis[v]) q.push((node){dis[v],v});
}
}
}
}
int main()
{
cin>>n>>m>>s;
for(int i=1;i<=m;i++)
{
int u,v,w;
cin>>u>>v>>w;
add(u,v,w);
}
dijkstra();
for(int i=1;i<=n;i++) cout<<dis[i]<<" ";
return 0;
}