Submission #1043670

#TimeUsernameProblemLanguageResultExecution timeMemory
1043670vjudge1Evacuation plan (IZhO18_plan)C++17
100 / 100
301 ms55764 KiB
#include <bits/stdc++.h> #define ll long long #define fi(i, a, b) for(int i = a; i <= b; i++) #define fid(i, a, b) for(int i = a; i >= b; i--) #define maxn (int) 2e5 + 7 using namespace std; const ll mod = 1e9 + 7; const ll base = 911382323; int n, m, q, k; int spe[maxn], P[maxn]; vector<pair<int, int>> edges[maxn]; ll dist[maxn], ans[maxn]; void DJK() { priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>> > q; memset(dist, 60, sizeof dist); fi(i, 1, k) q.push({0, spe[i]}), dist[spe[i]] = 0; while(!q.empty()) { int u = q.top().second; ll g = q.top().first; q.pop(); if(g > dist[u]) continue; for(auto [v, w] : edges[u]) if(dist[v] > dist[u] + w) { dist[v] = dist[u] + w; q.push({dist[v], v}); } } } struct DSU { set<int> S[maxn]; int lab[maxn]; DSU() { memset(lab, -1, sizeof lab); } int get_rt(int u) { return lab[u] < 0 ? u : lab[u] = get_rt(lab[u]); } void uni(int u, int v, ll w) { int p = get_rt(u), q = get_rt(v); if(p == q) return; if(lab[p] > lab[q]) swap(p, q); for(int x : S[q]) { if(S[p].find(x) != S[p].end()) ans[x] = w, S[p].erase(x); else S[p].insert(x); } S[q].clear(); lab[p] += lab[q], lab[q] = p; } } Dsu; void solve() { cin >> n >> m; fi(i, 1, m) { int x, y, w; cin >> x >> y >> w; edges[x].push_back({y, w}); edges[y].push_back({x, w}); } cin >> k; fi(i, 1, k) cin >> spe[i]; cin >> q; fi(i, 1, q) { int x, y; cin >> x >> y; Dsu.S[x].insert(i), Dsu.S[y].insert(i); } DJK(); fi(i, 1, n) P[i] = i; sort(P + 1, P + 1 + n, [](int x, int y) { return dist[x] > dist[y]; }); fi(i, 1, n) { int u = P[i]; for(auto [v, w] : edges[u]) if(dist[v] >= dist[u]) Dsu.uni(u, v, dist[u]); } fi(i, 1, q) cout << ans[i] << '\n'; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int nTest = 1; while(nTest--) 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...