#include<cstdio>
#include<queue>
using namespace std;
queue<int>q;
int head[1000010],nxt[1000010],w[1000010],to[1000010],tot;
int dis[500010],vis[500010];
int INF=2e9;
int n,m,s;
long long ans1=2147483647;
void add(int x,int y,int t)
{
to[++tot]=y;
w[tot]=t;
nxt[tot]=head[x];
head[x]=tot;
}
void spfa()
{
for(int i=1;i<=n;i++) dis[i]=INF;
int u,v;
q.push(s);
dis[s]=0;vis[s]=1;
while(!q.empty())
{
u=q.front();
q.pop();
vis[u]=0;
for(int i=head[u];i;i=nxt[i])
{
v=to[i];
if(dis[v]>dis[u]+w[i])
{
dis[v]=dis[u]+w[i];
if(!vis[v])
{
vis[v]=1;
q.push(v);
}
}
}
}
}
int main()
{
scanf("%d%d%d",&n,&m,&s);
for(int i=1;i<=m;i++)
{
int x,y,t;
scanf("%d%d%d",&x,&y,&t);
add(x,y,t);
}
spfa();
for(int i=1;i<=n;i++)
{
if(dis[i]==INF) printf("%lld ",ans1);
else printf("%d ",dis[i]);
}
return 0;
}