#include<bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define pb push_back
#define PII pair<int,int >
#define IOS ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
using namespace std;
const int N = 1e5+5;
int n,m,s;
vector<PII> v[N];
int dis[N];
bool vis[N];
void dijkstra(int s)
{
priority_queue<PII,vector<PII>,greater<PII> > q;
for(int i=1;i<=n;i++) dis[i] = 1e9;
q.push({0,s});
dis[s] = 0;
while(q.size())
{
auto t = q.top();
q.pop();
int now = t.se;
if(vis[now]==1) continue;
vis[now] = 1;
for(auto tt:v[now])
{
int spot = tt.fi,w = tt.se;
if(dis[spot]>dis[now]+w)
{
dis[spot] = dis[now]+w;
q.push({dis[spot],spot});
}
}
}
}
int main()
{
IOS;
cin>>n>>m>>s;
while(m--)
{
int a,b,w;
cin>>a>>b>>w;
v[a].pb({b,w});
}
dijkstra(s);
for(int i=1;i<=n;i++) cout<<dis[i]<<' ';
return 0;
}