#include<bits/stdc++.h>
#define int long long
#define PII pair<int,int>
using namespace std;
const int N = 2e5 + 10;
int n,m,s;
int dist[N],vis[N];
vector<PII> g[N];
void init(){
for(int i=1;i<=n;i++){
dist[i]=INT_MAX;
vis[i]=0;
}
}
void dij(int s){
init();
priority_queue<PII,vector<PII>,greater<PII>> q;
q.push({dist[s]=0,s});
while(q.size()){
int var=q.top().second;
q.pop();
if(vis[var]) continue;
vis[var]=1;
for(auto x:g[var]){
int e=x.first,w=x.second;
if(dist[e]>dist[var]+w){
q.push({dist[e]=dist[var]+w,e});
}
}
}
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
cin>>n>>m>>s;
for(int i=1;i<=m;i++){
int a,b,c;
cin>>a>>b>>c;
g[a].push_back({b,c});
}
dij(s);
for(int i=1;i<=n;i++){
if(dist[i]==INT_MAX) cout<<INT_MAX-1<<" ";
else cout<<dist[i]<<" ";
}
return 0;
}