#include <iostream> #include <vector> #include <algorithm> #include <string> #include <set> #include <queue> #include <map> #include <sstream> #include <cstdio> #include <cstring> #include <numeric> #include <cmath> #include <iomanip> #include <deque> #include <bitset> #include <cassert> //#include <unordered_set> //#include <unordered_map> #define ll long long #define pii pair<int, int> #define rep(i,a,b) for(int i=a;i<=b;i++) #define dec(i,a,b) for(int i=a;i>=b;i--) #define forn(i, n) for(int i = 0; i < int(n); i++) using namespace std; int dir[4][2] = { { 1,0 },{ 0,1 } ,{ 0,-1 },{ -1,0 } }; const long long INF = 0x7f7f7f7f7f7f7f7f; const int inf = 0x3f3f3f3f; const double pi = acos(-1.0); const double eps = 1e-6; const ll mod = 1e9+7; inline ll read() { ll x = 0; bool f = true; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') f = false; c = getchar(); } while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar(); return f ? x : -x; } ll gcd(ll m, ll n) { return n == 0 ? m : gcd(n, m % n); } ll qpow(ll m, ll k) { ll res = 1, t = m; while (k) { if (k & 1) res = res * t % mod; t = t * t % mod; k >>= 1; } return res; } /**********************************************************/ const int N = 2e5 + 5; struct edge { int to, dis, next; }; edge e[N]; int head[N], dis[N], cnt; bool vis[N]; int n, m, s; void add(int u, int v, int w) { cnt++; e[cnt].dis = w; e[cnt].to = v; e[cnt].next = head[u]; head[u] = cnt; } struct node { int dis; int pos; bool operator < (const node& x)const { return x.dis < dis; } }; priority_queue<node> q; void dijkstra() { dis[s] = 0; q.push({ 0, s }); while (!q.empty()) { node f = q.top(); q.pop(); int x = f.pos, d = f.dis; if (vis[x]) continue; vis[x] = 1; for (int i = head[x]; i; i = e[i].next) { int to = e[i].to; if (dis[to] > dis[x] + e[i].dis) { dis[to] = dis[x] + e[i].dis; if (!vis[to]) { q.push({ dis[to],to }); } } } } } int main() { #ifdef _DEBUG freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif cin >> n >> m >> s; memset(dis, inf, sizeof(dis)); rep(i, 1, m) { int u, v, d; cin >> u >> v >> d; add(u, v, d); } dijkstra(); rep(i, 1, n) cout << dis[i] << " "; return 0; }