제출 #340840

#제출 시각아이디문제언어결과실행 시간메모리
340840ivan_tudorEvacuation plan (IZhO18_plan)C++14
100 / 100
1377 ms45044 KiB
#include<bits/stdc++.h> using namespace std; const int N = 1e5 + 5; struct edge{ int x, c; }; vector<edge> gr[N]; struct dst{ int d, id; }; int dist[N]; dst ord[N]; bool mark[N]; bool cmpdist(dst a, dst b){ return a.d > b.d; } int par[N]; vector<int> arb[N]; int findt(int nod){ if(nod == par[nod]) return nod; par[nod] = findt(par[nod]); return par[nod]; } void join(int son,int dad){ son = findt(son); if(son != dad){ par[son] = dad; arb[son].push_back(dad); arb[dad].push_back(son); } } int pr[20][N]; int in[N], out[N]; int cnt = 0; void dfs_arb(int nod,int dad){ pr[0][nod] = dad; in[nod] = ++cnt; for(auto x:arb[nod]){ if(x!=dad){ dfs_arb(x, nod); } } out[nod] = cnt; } bool isdad(int dad, int son){ return in[dad] <= in[son] && out[son] <= out[dad]; } int main() { int n, m; cin>>n>>m; for(int i=1;i<=m;i++){ int a, b, c; cin>>a>>b>>c; gr[a].push_back({b, c}); gr[b].push_back({a, c}); } for(int i=1;i<=n;i++){ dist[i] = INT_MAX; } priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq; int k; cin>>k; for(int i=1;i<=k;i++){ int o; cin>>o; dist[o] = 0; pq.push({0, o}); } while(pq.size()){ auto top = pq.top(); pq.pop(); int nod = top.second; int cost = top.first; if(cost == dist[nod]){ for(auto e:gr[nod]){ if(cost + e.c < dist[e.x]){ dist[e.x] = cost + e.c; pq.push({dist[e.x], e.x}); } } } } for(int i=1;i<=n;i++){ ord[i] = {dist[i], i}; par[i] = i; } sort(ord + 1, ord + n + 1, cmpdist); int root = 0; for(int i=1; i<=n;i++){ int nod = ord[i].id; mark[nod] = true; root = nod; for(auto x:gr[nod]){ int vec = x.x; if(mark[vec] == true){ join(vec, nod); // il pun pe nod tata la vec; } } } dfs_arb(root, 0); for(int log =1; log < 17;log++){ for(int nod = 1; nod<=n;nod++){ pr[log][nod] = pr[log-1][pr[log-1][nod]]; } } int q; cin>>q; for(int i =1 ; i<=q;i++){ int a, b; cin>>a>>b; for(int p2 = 16;p2>=0;p2--){ if(pr[p2][a] != 0 && isdad(pr[p2][a], b)== false){ a = pr[p2][a]; } } if(isdad(a, b) == false){ a = pr[0][a]; } cout<<dist[a]<<"\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...