#include <bits/stdc++.h>
using namespace std;
struct node {
int v, w;
node() {}
node(int _v, int _w) {
v = _v;
w = _w;
}
};
vector<node> G[10005];
int d[10005];
bool vis[10005];
set<pair<int, int> > min_heap;
int main() {
int n, m, s;
cin >> n >> m >> s;
for (int i = 1; i <= m; i++) {
int u, v, w;
cin >> u >> v >> w;
G[u].push_back(node(v, w));
}
memset(d, 0x3f, sizeof(d));
d[s] = 0;
min_heap.insert(make_pair(0, s));
while (min_heap.size()) {
int mind = min_heap.begin() -> first;
int v = min_heap.begin() -> second;
min_heap.erase(min_heap.begin());
for (int i = 0; i < G[v].size(); i++) {
if (d[G[v][i].v] > d[v] + G[v][i].w) {
min_heap.erase(make_pair(d[G[v][i].v], i));
d[G[v][i].v] = d[v] + G[v][i].w;
min_heap.insert(make_pair(d[G[v][i].v], i));
}
}
}
for (int i = 1; i <= n; i++) {
cout << d[i] << ' ';
}
return 0;
}
wwwww