#include<bits/stdc++.h>#define ll long longusing namespace std;
const int N = 1e4+5, M = 5e5+5;
int head[N], cnt;
int v, e, x, y, z, s;
int dis[N], vis[N], inf = (1 << 31) - 1;
struct EDGE {
int next, to, dis;
} edge[M];//void addedge(int from, int to, int dis) {
edge[++cnt].next = head[from];
edge[cnt].to = to;
edge[cnt].dis = dis;
head[from] = cnt;
}//void SPFA(int s) {
queue<int> q;
q.push(s);
memset(dis, 0x3f, sizeof(dis));
dis[s] = 0;
vis[s] = 1;
while (q.size()) {
int x = q.front();
vis[x] = 0;
q.pop();
for (ll i = head[x]; i; i = edge[i].next) {
int y = edge[i].to;
int d = edge[i].dis;
if (dis[y] > dis[x] + d) {
dis[y] = dis[x] + d;
if (!vis[y]) {
vis[y] = 1;
q.push(y);
}
}
}
}
}
int main() {
cin >> v >> e >> s;
for (ll i = 1; i <= e; i++) {
cin >> x >> y >> z;
addedge(x, y, z);
}
SPFA(s);
for (ll i = 1; i <= v; i++)
if (dis[i] > 1e9) cout << inf << " ";
else cout << dis[i] << ' ';
return 0;
}