제출 #945296

#제출 시각아이디문제언어결과실행 시간메모리
945296oblantisEvacuation plan (IZhO18_plan)C++17
100 / 100
391 ms45728 KiB
//#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt") //#pragma GCC optimize("O3,unroll-loops") #include <bits/stdc++.h> #define all(v) v.begin(), v.end() #define pb push_back #define ss second #define ff first #define vt vector using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; const ll inf = 1e15; const int mod = 1e9+7; const int maxn = 1e5 + 1; vt<pii> g[maxn]; vt<int> adj[maxn]; ll dp[maxn]; int p[maxn], deep[maxn], up[maxn][20]; bool was[maxn]; bool cmp(int x, int y){ return dp[x] < dp[y]; } int get(int wt){ if(p[wt] == wt)return wt; p[wt] = get(p[wt]); return p[wt]; } void go(int v){ for(auto i : adj[v]){ deep[i] = deep[v] + 1; up[i][0] = v; go(i); } } int calc(int x, int y){ if(deep[x] > deep[y])swap(x, y); int fup = deep[y] - deep[x], j = 0; while(fup){ if(fup & 1){ y = up[y][j]; } j++; fup >>= 1; } if(y == x)return y; for(int i = 19; i >= 0; i--){ if(up[y][i] != up[x][i])x = up[x][i], y = up[y][i]; } return up[x][0]; } void solve() { int n, m; cin >> n >> m; fill(dp, dp + n + 1, inf); vt<int> it(n); for(int i = 1; i <= n; i++){ it.pb(i); p[i] = i; } for(int i = 0, u, v, w; i < m; i++){ cin >> u >> v >> w; g[u].pb({v, w}); g[v].pb({u, w}); } int k; priority_queue<pair<ll, int>, vt<pair<ll, int>>, greater<pair<ll, int>>> pq; cin >> k; for(int i = 0; i < k; i++){ int wt; cin >> wt; dp[wt] = 0; pq.push({0, wt}); } while(!pq.empty()){ pair<ll, int> nw = pq.top(); pq.pop(); if(nw.ff != dp[nw.ss])continue; for(auto i : g[nw.ss]){ if(dp[i.ff] > nw.ff + i.ss){ dp[i.ff] = nw.ff + i.ss; pq.push({dp[i.ff], i.ff}); } } } sort(all(it), cmp); for(int i = n - 1; i >= 0; i--){ was[it[i]] = 1; for(auto j : g[it[i]]){ if(!was[j.ff])continue; get(j.ff); if(p[j.ff] == it[i])continue; adj[it[i]].pb(p[j.ff]); p[p[j.ff]] = it[i]; } } go(it[0]); for(int i = 1; i < 20; i++)for(int j = 1; j <= n; j++)up[j][i] = up[up[j][i - 1]][i - 1]; int q; cin >> q; while(q--){ int s, t; cin >> s >> t; cout << dp[calc(s, t)] << '\n'; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int times = 1; //cin >> times; for(int i = 1; i <= times; i++) { solve(); } 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...