Submission #1044278

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