1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
| #include <algorithm> #include <cassert> #include <cmath> #include <cstdio> #include <cstring> #include <deque> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <utility> #include <vector> #define rep(i, l, r) for (int i = (l); i <= (r); ++i) #define per(i, l, r) for (int i = (l); i >= (r); --i) using std::cerr; using std::cin; using std::cout; using std::endl; using std::make_pair; using std::pair; typedef pair<int, int> pii; typedef long long ll; typedef unsigned int ui;
const int N = 25030, inf = 0x3f3f3f3f;
std::vector<pii> g[N]; std::vector<pii> ng[N]; std::vector<int> gg[N]; std::vector<int> block[N]; int color[N], cnt = 0, deg[N], able[N], dis[N], vis[N];
void dfs(int u) { block[color[u]].push_back(u); for (auto i : g[u]) { int v = i.first; if (!color[v]) color[v] = color[u], dfs(v); } }
void dfs_gg(int u) { able[u] = 1; for (auto v : gg[u]) { if (!able[v]) dfs_gg(v); } }
int main() { #ifdef LOCAL freopen("input", "r", stdin); #endif std::memset(dis, 0x3f, sizeof(dis)); std::ios::sync_with_stdio(false); cout.tie(0); int n, r, p, s; cin >> n >> r >> p >> s; rep(i, 1, r) { int u, v, w; cin >> u >> v >> w; g[u].push_back(pii(v, w)); g[v].push_back(pii(u, w)); } rep(i, 1, n) if (!color[i]) color[i] = ++cnt, dfs(i);
rep(i, 1, p) { int u, v, w; cin >> u >> v >> w; ng[u].push_back(pii(v, w)); gg[color[u]].push_back(color[v]); } int ss = color[s]; dfs_gg(ss); rep(u, 1, cnt) { if (!able[u]) continue; for (auto v : gg[u]) deg[v]++; }
dis[s] = 0; std::queue<int> q; q.push(ss); std::priority_queue<pii, std::vector<pii>, std::greater<pii>> heap; while (q.size()) { int block_id = q.front(); q.pop();
for (auto i : block[block_id]) if (dis[i] < inf) heap.push(pii(dis[i], i));
while (heap.size()) { int u = heap.top().second; heap.pop(); if (vis[u]) continue; vis[u] = 1; for (auto i : g[u]) { int v = i.first, w = i.second; if (dis[v] > dis[u] + w) dis[v] = dis[u] + w, heap.push(pii(dis[v], v)); } for (auto i : ng[u]) { int v = i.first, w = i.second; if (!able[color[v]]) continue; deg[color[v]]--; if (deg[color[v]] == 0) q.push(color[v]); dis[v] = std::min(dis[v], dis[u] + w); } } }
rep(i, 1, n) { if (dis[i] == inf) cout << "NO PATH" << endl; else cout << dis[i] << endl; } return 0; }
|