#include <bits/stdc++.h>
using namespace std;
const int nn = 1e5 + 5;
const int mm = 2e5 + 5;
int d[nn],head[nn],ver[mm],edge[mm],nxt[mm],tot;
bool v[nn];
int n,m,s;
priority_queue<pair<int,int> > que;
void add(int x,int y,int z){
ver[++tot] = y;edge[tot] = z;//这条边的终点和边权
nxt[tot] = head[x];head[x] = tot;//相当于指针
}
void dijstra(){
memset(d,0x3f,sizeof(d));
d[s] = 0;
que.push(make_pair(0,s));
while(!que.empty()){
int x = que.top().second;//表示节点编号
que.pop();
if(v[x]) continue;
v[x] = 1;
//遍历所有出边
for(int i = head[x];i;i=nxt[i]){
int y = ver[i],z = edge[i];
if(d[y] > d[x] + z){
d[y] = d[x] + z;
que.push(make_pair(-d[y],y));
}
}
}
}
int main(){
//freopen("in","r",stdin);
ios::sync_with_stdio(0);
cin >> n >> m >> s;
while(m--){
int x,y,w;
cin >> x >> y >> w;
add(x,y,w);
}
dijstra();
for(int i = 1; i <= n; i++){
if(i == 1) cout << d[i];
else cout << " " << d[i];
}
return 0;
}