Submission #392826

#TimeUsernameProblemLanguageResultExecution timeMemory
392826knightron0Evacuation plan (IZhO18_plan)C++14
100 / 100
905 ms227984 KiB
#include <bits/stdc++.h> using namespace std; #define pb push_back #define fr first #define sc second #define clr(a, x) memset(a, x, sizeof(a)) #define dbg(x) cout<<"("<<#x<<"): "<<x<<endl; #define printvector(arr) for (auto it = arr.begin(); it != arr.end(); ++it) cout<<*it<<" "; cout<<endl; #define all(v) v.begin(), v.end() #define lcm(a, b) (a * b)/__gcd(a, b) #define int long long int #define printvecpairs(vec) for(auto it: vec) cout<<it.fr<<' '<<it.sc<<endl; #define endl '\n' #define pii pair<int, int> #define float long double const int MOD = 1e9 + 7; const int INF = 2e15; const int MAXN = 1e5 + 5; int n, m, k, l; vector<pii> adj[MAXN]; vector<int> mst[MAXN]; int dist[MAXN], b[MAXN], up[MAXN][100], tout[MAXN], tin[MAXN], upmn[MAXN][100], depth[MAXN]; int timer = 0; void dfs(int v, int p, int d = 0){ timer++; tin[v] = timer; up[v][0] = p; upmn[v][0] = min(dist[v], dist[p]); for(int i=1;i<=l;i++){ up[v][i] = up[up[v][i-1]][i-1]; upmn[v][i] = min(upmn[up[v][i-1]][i-1], upmn[v][i-1]); } depth[v] = d; for(int u: mst[v]){ if(u != p){ dfs(u, v, d+1); } } timer++; tout[v] = timer; } bool isanc(int a, int b){ return tin[a] <= tin[b] && tout[a] >= tout[b]; } int lca(int u, int v){ if(isanc(u, v)) return u; if(isanc(v, u)) return v; for(int i = l;i >= 0;i--){ if(!isanc(up[u][i], v)){ u = up[u][i]; } } return up[u][0]; } int getmn(int u, int kx){ int ans = dist[u]; for(int i= 0;i<=l;i++){ if(kx & (1<<i)){ ans = min(ans, upmn[u][i]); u = up[u][i]; } } return ans; } void dstra(){ for(int i=1;i<=n;i++){ dist[i] = INF; } priority_queue<pii> pq; for(int j= 1;j<=k;j++){ dist[b[j]] = 0; pq.push({0, b[j]}); } while(!pq.empty()){ int u = pq.top().sc; pq.pop(); for(auto e: adj[u]){ int w = e.sc, v = e.fr; if(dist[u]+w < dist[v]){ dist[v] = dist[u] + w; pq.push({-dist[v], v}); } } } } int parent[MAXN] = {-1}; void init(){ for(int i= 1;i<=n;i++) parent[i] = i; timer = 0; clr(tout, 0); clr(tin, 0); l = ceil(log2(n)); clr(up, 0); } int find_set(int s){ if(s == parent[s]){ return s; } return parent[s] = find_set(parent[s]); } bool union_sets(int a, int b){ a = find_set(a); b = find_set(b); if(a != b){ parent[b] = a; return true; } else { return false; } } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); #ifdef LOCAL freopen("input.txt", "r", stdin); #endif cin>>n>>m; vector<pii> edges; for(int i=0;i<m;i++){ int u, v, w; cin>>u>>v>>w; adj[u].pb({v, w}); adj[v].pb({u, w}); edges.pb({u, v}); } cin>>k; for(int i= 1;i<=k;i++){ cin>>b[i]; } dstra(); init(); vector<pair<int, pii>> wedges; for(auto edge: edges){ wedges.pb({min(dist[edge.fr], dist[edge.sc]), {edge}}); } sort(all(wedges)); reverse(all(wedges)); for(auto edge: wedges){ int u = edge.sc.fr, v = edge.sc.sc; if(union_sets(u, v)){ mst[v].pb(u); mst[u].pb(v); } } dfs(1, 1); int q; cin>>q; while(q--){ int u, v; cin>>u>>v; int lc = lca(u, v); int du = depth[u]-depth[lc]; int dv = depth[v]-depth[lc]; cout<<min(getmn(u, du), getmn(v, dv))<<endl; } 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...