Submission #1045482

#TimeUsernameProblemLanguageResultExecution timeMemory
1045482vjudge1Evacuation plan (IZhO18_plan)C++17
100 / 100
329 ms53208 KiB
#include <bits/stdc++.h> using namespace std; #define fi first #define se second const int oo = 1e18; int n, m, x, y, w, k, a[100005], q; int id[100005], ans[100005], parent[100005], sz[100005]; vector<pair<int, int>> adj[100005]; set<int> s[100005]; int dist[100005]; bool mark[100005]; bool sapxep(int a, int b){ return dist[a] > dist[b]; } void dijkstra(){ for(int i = 1; i <= n; i++) dist[i] = oo; priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> q; for(int i = 1; i <= k; i++){ dist[a[i]] = 0; q.push({0, a[i]}); }; while(!q.empty()){ int u = q.top().se; q.pop(); if(mark[u]) continue; mark[u] = true; for(auto it : adj[u]){ int v = it.fi; int w = it.se; if(dist[v] > dist[u]+w){ dist[v] = dist[u]+w; q.push({dist[v], v}); } } } } int find_set(int u){ if(u == parent[u]) return u; return parent[u] = find_set(parent[u]); } void union_set(int u, int v){ u = find_set(u); v = find_set(v); if(u == v) return; if(sz[u] < sz[v]) swap(u, v); parent[v] = u; sz[u]+=sz[v]; for(auto it : s[v]){ if(s[u].find(it) != s[u].end()){ s[u].erase(it); ans[it] = w; } else s[u].insert(it); } } void nhap() { cin >> n >> m; while(m--){ 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 >> a[i]; } dijkstra(); cin >> q; for(int i = 1; i <= q; i++){ cin >> x >> y; s[x].insert(i); s[y].insert(i); } } void solve() { for(int i = 1; i <= n; i++){ id[i] = i; parent[i] = i; sz[i] = 1; } sort(id+1, id+n+1, sapxep); memset(mark, 0, sizeof mark); for(int i = 1; i <= n; i++){ x = id[i]; mark[x] = true; w = dist[x]; for(auto it : adj[x]){ y = it.fi; if(mark[y]) union_set(x, y); } } for(int i = 1; i <= q; i++){ cout << ans[i] << '\n'; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); nhap(); solve(); return 0; }

Compilation message (stderr)

plan.cpp:5:16: warning: overflow in conversion from 'double' to 'int' changes value from '1.0e+18' to '2147483647' [-Woverflow]
    5 | const int oo = 1e18;
      |                ^~~~
#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...