Submission #1098321

#TimeUsernameProblemLanguageResultExecution timeMemory
1098321vjudge1Evacuation plan (IZhO18_plan)C++17
100 / 100
436 ms54732 KiB
#include <bits/stdc++.h> using namespace std; #define ll long long #define pll pair<ll, ll> #define fi first #define se second const ll N = 1e5+5; ll n, m, q, k, s[N], p[N], dist[N], ans[N], lab[N]; vector<pll> adj[N]; set<ll> S[N]; void Dijkstra() { priority_queue<pll, vector<pll>, greater<pll>> pq; memset(dist, 0x3f, sizeof dist); for (int i= 1; i<= k; i++) { dist[s[i]] = 0; pq.push({0, s[i]}); } while (!pq.empty()) { ll dis = pq.top().fi; ll u = pq.top().se; pq.pop(); if (dis > dist[u]) continue; for (auto v : adj[u]) if (dist[v.fi] > dist[u] + v.se) { dist[v.fi] = dist[u] + v.se; pq.push({dist[v.fi], v.fi}); } } } ll find_set(ll u) { if (lab[u] < 0) return u; return lab[u] = find_set(lab[u]); } void union_set(ll u, ll v, ll w) { u = find_set(u); v = find_set(v); if (u == v) return; if (lab[u] > lab[v]) swap(u, v); for (ll x : S[v]) { if (S[u].find(x) != S[u].end()) { ans[x] = w; S[u].erase(x); } else S[u].insert(x); } S[v].clear(); lab[u] += lab[v]; lab[v] = u; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin>>n>>m; for (int i= 1; i<= m; i++) { ll x, y, w; cin>>x>>y>>w; adj[x].push_back({y, w}); adj[y].push_back({x, w}); } cin>>k; for (int i= 1; i<= k; i++) cin>>s[i]; cin>>q; for (int i= 1; i<= q; i++) { ll x, y; cin>>x>>y; S[x].insert(i); S[y].insert(i); } Dijkstra(); for (int i= 1; i<= n; i++) p[i] = i; sort(p+1, p+n+1, [](ll x, ll y) { return dist[x] > dist[y]; }); memset(lab, -1, sizeof lab); for (int i= 1; i<= n; i++) { ll u = p[i]; for (auto v : adj[u]) if (dist[v.fi] >= dist[u]) union_set(u, v.fi, dist[u]); } for (int i= 1; i<= q; i++) cout<<ans[i]<<'\n'; }
#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...