Submission #42501

#TimeUsernameProblemLanguageResultExecution timeMemory
42501MatheusLealVEvacuation plan (IZhO18_plan)C++14
100 / 100
655 ms34952 KiB
#include <bits/stdc++.h> #define N 100005 #define inf 2000000000 #define f first #define s second using namespace std; typedef long long ll; typedef pair<int, int> pii; int n, m, k, q, P[N], id[N], dist[N], rev[N], pai[N], peso[N]; vector<pii> grafo[N]; list<int> l[N]; list<pii> hist[N]; void dijkstra() { priority_queue<pii, vector<pii>, greater<pii> > pq; for(int i = 1; i <= n; i++) dist[i] = inf; for(int i = 1; i <= k; i++) dist[ P[i] ] = 0, pq.push({0, P[i]}); while(!pq.empty()) { int x = pq.top().s, d = pq.top().f; pq.pop(); if(d > dist[x]) continue; for(auto v: grafo[x]) { if(dist[v.f] > dist[x] + v.s) { dist[v.f] = dist[x] + v.s; pq.push({dist[v.f], v.f}); } } } vector<pii> v; for(int i = 1; i <= n; i++) v.push_back({dist[i], i}); sort(v.begin(), v.end()); for(int i = 0; i < v.size(); i++) id[ v[i].s ] = i + 1, rev[i + 1] = v[i].s; } void persistent_join(int a, int b, int t) { a = pai[a], b = pai[b]; if(a != b) { if(peso[a] > peso[b]) swap(a, b); for(auto it = l[a].begin(); it != l[a].end(); it++) { hist[*it].push_back(pii(t, b)); pai[*it] = b; } peso[b] += peso[a]; l[b].splice(l[b].end(), l[a]); } } int p_query(int a, int b) { int ans = 0; for(auto x: hist[a]) for(auto y: hist[b]) if(x.s == y.s) ans = max(ans, min(x.f, y.f)); return ans; } int main() { ios::sync_with_stdio(false); cin.tie(0); cin>>n>>m; for(int i = 1, a, b, c; i <= m; i++) { cin>>a>>b>>c; grafo[a].push_back({b, c}); grafo[b].push_back({a, c}); } cin>>k; for(int i = 1; i <= k; i++) cin>>P[i]; dijkstra(); for(int i = 1; i <= n; i++) { l[i].push_back(i); hist[i].push_back(pii(dist[i], i)); peso[i] = 1, pai[i] = i; } for(int x = n; x >= 1; x--) { int xi = rev[x]; for(auto v: grafo[xi]) if(id[v.f] > x) persistent_join(v.f, xi, dist[xi]); } cin>>q; for(int t = 1, a, b; t <= q; t++) { cin>>a>>b; cout<<p_query(a, b)<<"\n"; } }

Compilation message (stderr)

plan.cpp: In function 'void dijkstra()':
plan.cpp:51:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i = 0; i < v.size(); i++) id[ v[i].s ] = i + 1, rev[i + 1] = v[i].s;
                 ~~^~~~~~~~~~
#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...