#include <bits/stdc++.h>
using namespace std;
const int maxn=1000000+10,inf=2147483647;
int n,m,s,tot,dis[maxn],head[maxn];
bool vis[maxn];
int read(){
int s=0,w=1;
char ch=getchar();
while(ch<='0'||ch>'9'){
if(ch=='-')
w=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9'){
s=s*10+ch-'0';
ch=getchar();
}
return s*w;
}
struct Edge{
int next,to,w;
}h[maxn];
void add(int u,int v,int w){
tot++;
h[tot].next=head[u];
h[tot].w=w;
h[tot].to=v;
head[u]=tot;
}
queue <int>q;
int main(){
n=read(),m=read(),s=read();
for(int i=1;i<=m;i++){
int u,v,w;
u=read();
v=read();
w=read();
add(u,v,w);
}
for(int i=1;i<=n;i++)
dis[i]=inf;
int u,v;
q.push(s);
dis[s]=0;
vis[s]=true;
while(!q.empty()){
u=q.front();
q.pop();
vis[u]=false;
for(int i=head[u];i;i=h[i].next){
v=h[i].to;
if(dis[v]>dis[u]+h[i].w){
dis[v]=dis[u]+h[i].w;
if(!vis[v]){
vis[v]=true;
q.push(v);
}
}
}
}
for(int i=1;i<=n;i++)
if(dis[i]<2147483647)
printf("%d ",dis[i]);
else
printf("2147483647 ");
return 0;
}
#include <bits/stdc++.h>
using namespace std;
const int maxn=10000+105,inf=2147483647;
struct node{
int to,z;
};
int p[maxn],d[maxn],n,m,s,x,y,z,f,l;
vector<node>e[maxn];
int q[maxn*105];
int main(){
scanf("%d%d%d",&n,&m,&s);
for(int i=1;i<=m;i++){
scanf("%d%d%d",&x,&y,&z);
node t;
t.to=y;
t.z=z;
e[x].push_back(t);
}
for(int i=1;i<=n;i++)
d[i]=inf;
d[s]=0;
q[1]=s;
p[s]=1;
l=1;
while(f<l){
f++;
int x=q[f];
for(int i=0;i<e[x].size();i++){
int u=e[x][i].to,v=e[x][i].z;
if(d[u]>d[x]+v){
d[u]=d[x]+v;
if(!p[u]){
q[++l]=u;
p[u]=1;
}
}
}
p[x]=0;
}
for(int i=1;i<=n;i++)
if(d[i]<inf)
printf("%d ",d[i]);
else
printf("2147483647 ");
return 0;
}