typedeflonglong ll; #define inf 0x3f3f3f3f constint N = 3e5 + 10; constint mod = 1e9 + 7; structedge { int to, w; } e; vector<edge> g[N]; voidaddedge(int u, int v, int w) { e.to = v; e.w = w; g[u].push_back(e); } int n, r, p, s, f[N], vis[N], cnt, dis[N], dig[N]; queue<int> tuo; priority_queue<pair<int, int>> q; voiddfs(int x) { f[x] = cnt; vis[x] = 1; for (int i = 0; i < g[x].size(); i++) if (!vis[g[x][i].to]) dfs(g[x][i].to); }
intmain() { scanf("%d%d%d%d", &n, &r, &p, &s); for (int i = 1; i <= r; i++) { int u, v, w; scanf("%d%d%d", &u, &v, &w); addedge(u, v, w); addedge(v, u, w); } for (int i = 1; i <= n; i++) if (!vis[i]) ++cnt, dfs(i); //cout<<1<<endl; for (int i = 1; i <= p; i++) { int u, v, w; scanf("%d%d%d", &u, &v, &w); addedge(u, v, w); dig[f[v]]++; } //cout<<1<<endl; memset(vis, 0, sizeof(vis)); memset(dis, 0x7f, sizeof(dis)); tuo.push(f[s]); for (int i = 1; i <= cnt; i++) if (!dig[i]) tuo.push(i); dis[s] = 0; while (!tuo.empty()) { int u = tuo.front(); //cout<<u<<endl; tuo.pop(); for (int i = 1; i <= n; i++) if (f[i] == u) q.push(make_pair(-dis[i], i)); while (!q.empty()) { int x = q.top().second; q.pop(); if (vis[x]) continue; vis[x] = 1; for (int i = 0; i < g[x].size(); i++) {
int v = g[x][i].to; // cout<<v<<endl; // cout<<"----"<<endl; int w = g[x][i].w; if (dis[v] > w + dis[x]) { dis[v] = w + dis[x]; if (f[v] == f[x]) q.push(make_pair(-dis[v], v)); } if (f[v] != f[x]) { dig[f[v]]--; if (dig[f[v]] == 0) tuo.push(f[v]); } } } } for (int i = 1; i <= n; i++) if (dis[i] >= inf) printf("NO PATH\n"); else printf("%d\n", dis[i]); }