#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int maxn = 100001;
int n, m, s, t;
vector<pair<int, int>> adj[maxn];
void nhap(){
cin >> n >> m >> s;
for(int i = 0; i < m; i++){
int x, y, w; cin >> x >> y >> w;
adj[x].push_back({y, w});
//adj[y].push_back({x, w});
}
}
const int INF = 1e9;
int pre[maxn];
void dijkstra(int s){
//Mang luu khoang cach duong di
vector<ll> d(n + 1, INF);
d[s] = 0;
priority_queue<pair<int, int>, vector<pair<int, int>> , greater<pair<int,int>>> Q;
//{khoang cach, dinh}
Q.push({0, s});
while(!Q.empty()){
//Chọn ra đỉnh có khoảng cách từ s nhỏ nhất
pair<int, int> top = Q.top(); Q.pop();
int u = top.second;
int kc = top.first;
if(kc > d[u]) continue;
//Relaxtion : Thông qua đỉnh u đã biết khoảng cách ngắn nhất tính từ S, cập
//nhật khoảng cách với các đỉnh kề với u
for(auto it : adj[u]){
int v = it.first;
int w = it.second;
if(d[v] > d[u] + w){
d[v] = d[u] + w;
Q.push({d[v], v});
}
}
}
for(int i = 1; i <= n; i++){
cout << d[i] << ' ';
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
nhap();
dijkstra(s);
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7Cgp1c2luZyBsbCA9IGxvbmcgbG9uZzsKCmNvbnN0IGludCBtYXhuID0gMTAwMDAxOwppbnQgbiwgbSwgcywgdDsKdmVjdG9yPHBhaXI8aW50LCBpbnQ+PiBhZGpbbWF4bl07Cgp2b2lkIG5oYXAoKXsKCWNpbiA+PiBuID4+IG0gPj4gczsKCWZvcihpbnQgaSA9IDA7IGkgPCBtOyBpKyspewoJCWludCB4LCB5LCB3OyBjaW4gPj4geCA+PiB5ID4+IHc7CgkJYWRqW3hdLnB1c2hfYmFjayh7eSwgd30pOwoJCS8vYWRqW3ldLnB1c2hfYmFjayh7eCwgd30pOwoJfQp9Cgpjb25zdCBpbnQgSU5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