Submission #159373

#TimeUsernameProblemLanguageResultExecution timeMemory
159373sofhiasouzaEvacuation plan (IZhO18_plan)C++14
100 / 100
1057 ms21880 KiB
#include <bits/stdc++.h> #define pb push_back #define F first #define S second using namespace std; typedef pair < int, int > ii; const int maxn = 1e5+10, inf = 0x3f3f3f3f; int n, m, k, q, pai[maxn], peso[maxn], resp[maxn], vist[maxn], cont, nivel[maxn]; ii dist[maxn]; bool npp[maxn]; vector < ii > grafo[maxn]; void dijkstra() { priority_queue < int, vector < int >, greater < int > > fila; for(int i = 1 ; i <= n ; i++) { if(npp[i]) { fila.push(i); dist[i] = {0, i}; } else dist[i] = {inf, i}; } while(fila.size()) { int u = fila.top(); fila.pop(); for(ii k : grafo[u]) { int v = k.F; int cst = k.S; if(dist[v].F > dist[u].F + cst) { fila.push(v); dist[v].F = dist[u].F + cst; } } } } bool cmp(ii a, ii b) { return a.F > b.F; } int find(int x) { if(pai[x] == x) return x; return find(pai[x]); } void join(int u, int v) { u = find(u); v = find(v); if(u == v) return; if(peso[u] < peso[v]) swap(u, v); pai[v] = u, peso[u] += peso[v], resp[v] = cont; } void dfs(int x) { vist[x] = 1; if(pai[x] == x) { nivel[x] = 0; return; } if(!vist[pai[x]]) dfs(pai[x]); nivel[x] = nivel[pai[x]] + 1; } int get(int u, int v) { int ans = 0; while(u != v) { if(nivel[u] > nivel[v]) ans = max(ans, resp[u]), u = pai[u]; else ans = max(ans, resp[v]), v = pai[v]; } return ans; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cin >> n >> m; for(int i = 1 ; i <= m ; i++) { int a, b, c; cin >> a >> b >> c; grafo[a].pb({b, c}); grafo[b].pb({a, c}); } memset(npp, false, sizeof npp); cin >> k; for(int i = 1 ; i <= k ; i++) { int a; cin >> a; npp[a] = true; } dijkstra(); sort(dist+1, dist+1+n, cmp); for(int i = 1 ; i <= n ; i++) { pai[i] = i; peso[i] = 1; resp[i] = 1; } for(int i = 1 ; i <= n ; i++) { int u = dist[i].S; cont++; for(ii k : grafo[u]) { int v = k.F; if(vist[v]) join(u, v); } vist[u] = 1; } memset(vist, 0, sizeof vist); for(int i = 1 ; i <= n ; i++) { if(!vist[i]) dfs(i); } cin >> q; for(int i = 1 ; i <= q ; i++) { int a, b; cin >> a >> b; cout << dist[get(a, b)].F << "\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...