Submission #378614

#TimeUsernameProblemLanguageResultExecution timeMemory
378614wiwihoEvacuation plan (IZhO18_plan)C++14
100 / 100
862 ms70368 KiB
#include <bits/stdc++.h> #define mp make_pair #define F first #define S second #define eb emplace_back #define printv(a, b) { \ for(auto pv : a) b << pv << " "; \ b << "\n"; \ } using namespace std; typedef long long ll; using pll = pair<ll, ll>; using pii = pair<int, int>; const ll MAX = 1LL << 60; ostream& operator<<(ostream& o, pll p){ return o << '(' << p.F << ',' << p.S << ')'; } ll iceil(ll a, ll b){ return (a + b - 1) / b; } vector<int> dsu; vector<vector<int>> g; vector<ll> dvw; int dv; void initDSU(int n){ dsu.resize(n + 1); for(int i = 1; i <= n; i++) dsu[i] = i; } int findDSU(int a){ if(dsu[a] != a) dsu[a] = findDSU(dsu[a]); return dsu[a]; } void unionDSU(int a, int b, ll w){ a = findDSU(a); b = findDSU(b); if(a == b) return; int id = dv++; g[id].eb(a); g[id].eb(b); dsu[a] = id; dsu[b] = id; dvw[id] = w; } vector<vector<int>> anc; vector<int> in, out; int ts = 0; void dfs(int now, int p){ anc[now][0] = p; in[now] = ts++; for(int i : g[now]){ dfs(i, now); } out[now] = ts++; } bool isAnc(int a, int b){ return in[a] <= in[b] && out[a] >= out[b]; } int lca(int a, int b){ if(isAnc(a, b)) return a; for(int i = 19; i >= 0; i--){ if(!isAnc(anc[a][i], b)) a = anc[a][i]; } return anc[a][0]; } int main(){ ios_base::sync_with_stdio(false); cin.tie(0); int n, m; cin >> n >> m; dv = n + 1; vector<vector<pll>> tg(n + 1); for(int i = 0; i < m; i++){ int u, v, w; cin >> u >> v >> w; tg[u].eb(mp(v, w)); tg[v].eb(mp(u, w)); } int k; cin >> k; vector<ll> dis(n + 1, MAX); priority_queue<pll, vector<pll>, greater<>> pq; for(int i = 0; i < k; i++){ int t; cin >> t; dis[t] = 0; pq.push(mp(0, t)); } while(!pq.empty()){ int now = pq.top().S; ll d = pq.top().F; pq.pop(); if(d != dis[now]) continue; for(pll i : tg[now]){ if(dis[i.F] > d + i.S){ dis[i.F] = d + i.S; pq.push(mp(d + i.S, i.F)); } } } //printv(dis, cerr); vector<pll> v(n); for(int i = 0; i < n; i++) v[i] = mp(dis[i + 1], i + 1); sort(v.begin(), v.end(), greater<>()); g.resize(2 * n); dvw.resize(2 * n); initDSU(2 * n - 1); vector<bool> ok(n + 1); for(pll i : v){ int now = i.S; ok[now] = true; for(pll j : tg[now]){ if(ok[j.F]) unionDSU(now, j.F, i.F); } } /*for(int i = 1; i < 2 * n; i++){ cerr << i << " "; printv(g[i], cerr); } printv(dvw, cerr);*/ anc.resize(2 * n, vector<int>(20)); in.resize(2 * n); out.resize(2 * n); int rt = findDSU(1); dfs(rt, rt); for(int i = 1; i < 20; i++){ for(int j = 1; j < 2 * n; j++){ anc[j][i] = anc[anc[j][i - 1]][i - 1]; } } int q; cin >> q; while(q--){ int a, b; cin >> a >> b; int t = lca(a, b); cout << dvw[t] << "\n"; } return 0; }
#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...