Submission #566848

#TimeUsernameProblemLanguageResultExecution timeMemory
5668481zaid1Evacuation plan (IZhO18_plan)C++17
22 / 100
4066 ms20660 KiB
#include <bits/stdc++.h> using namespace std; #define endl '\n' #define int long long typedef long long ll; const int M = 1e5 + 5, MOD = 1e9+7; vector<pair<int, int>> node[M]; int dist[M]; // sparse int d[M], up[M][50], mn[M][50]; void dfs(int s, int p = 1, int dp = 1, int ed = INT_MAX) { d[s] = dp++; up[s][0] = p; mn[s][0] = ed; for (auto [i, x]:node[s]) { if (i != p) dfs(i, s, dp, x); } } int lca(int a, int b) { if (d[a] < d[b]) swap(a, b); int l = 0; while (d[a]-d[b] > 0) { if ((d[a]-d[b])&(1<<l)) a = up[a][l]; l++; } l = 20; while (a != b) { while (l >= 0 && up[a][l] == up[b][l]) l--; if (l < 0) return up[a][0]; a = up[a][l]; b = up[b][l]; } return a; } int solve(int a, int b) { int ans = INT_MAX; int x = d[a]-d[b]; int cur = a; for (int i = 0; i < 31; i++) { if (x&(1<<i)) { ans = min(ans, mn[cur][i]); cur = up[cur][i]; } } return ans; } // dsu int p[M], rnk[M], cnt; int find(int a) { return (a==p[a]?a:p[a]=find(p[a])); } void uni(int a, int b) { if (rnk[a] < rnk[b]) swap(a, b); if (rnk[a] == rnk[b]) rnk[a]++; p[b] = a; cnt--; } struct e {int a, b, c;}; signed main() { ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); srand(time(0)); int n, m; cin >> n >> m; vector<e> v; for (int i = 1; i <= m; i++) { int a, b, c; cin >> a >> b >> c; node[a].push_back({b, c}); node[b].push_back({a, c}); v.push_back({a, b, c}); } priority_queue<pair<int, int>> q; int k; cin >> k; for (int i = 1; i <= n; i++) dist[i] = INT_MAX; for (int i = 1; i <= k; i++) { int a; cin >> a; dist[a] = 0; q.push({0, a}); } while (!q.empty()) { auto [dis, nod] = q.top(); q.pop(); if (dis < dist[nod]) continue; for (auto [x, d]:node[nod]) { if (d + dis < dist[x]) { dist[x] = d + dis; q.push({dist[x], x}); } } } for (auto&[a, b, c]:v) c = min(dist[a], dist[b]); sort(v.begin(), v.end(), [](e a, e b){return a.c < b.c;}); // for (auto [a, b, c]:v) cout << a << ' ' << b << ' ' << c << endl; for (int i = 1; i <= n; i++) node[i].clear(); for (int i = 1; i <= n; i++) p[i] = i; cnt = n; while (cnt > 1) { auto [a, b, c] = v.back(); v.pop_back(); if (find(a) != find(b)) { node[a].push_back({b, c}); node[b].push_back({a, c}); // cout << a << ' ' << b << endl; uni(find(a), find(b)); } } dfs(1); d[0] = INT_MAX; for (int i = 1; (1<<i) <= 20; i++) { for (int j = 1; j <= n; j++) { up[j][i] = up[up[j][i-1]][i-1]; mn[j][i] = min(mn[j][i-1], mn[up[j][i-1]][i-1]); } } int t; cin >> t; while (t--) { int a, b; cin >> a >> b; int l = lca(a, b); cout << min(solve(a, l), solve(b, l)) << endl; } return 0; } /* 9 12 1 9 4 1 2 5 2 3 7 2 4 3 4 3 6 3 6 4 8 7 10 6 7 5 5 8 1 9 5 7 5 4 12 6 8 2 2 4 7 5 1 6 5 3 4 8 5 8 1 5 */
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...