Submission #917159

#TimeUsernameProblemLanguageResultExecution timeMemory
917159dilanyanEvacuation plan (IZhO18_plan)C++17
100 / 100
461 ms50376 KiB
//-------------dilanyan------------\\ #define _CRT_SECURE_NO_WARNINGS #include<bits/stdc++.h> #include<stdio.h> using namespace std; //------------------KarginDefines--------------------\\ #define ll long long #define pb push_back #define all(u) (u).begin(), (u).end() #define pqueue priority_queue #define upper upper_bound #define lower lower_bound #define umap unordered_map #define uset unordered_set #define Kargin ios_base::sync_with_stdio(false);cin.tie(NULL); #define Usaco freopen(".in", "r", stdin); freopen(".out", "w", stdout); //-------------------KarginConstants------------------\\ const ll mod = 1000000007; const ll inf = 1e9 + 15; //-------------------KarginCode------------------------\\ const int N = 100005; int n; vector<pair<int, int>> g[N]; int dist[N]; bool vis[N]; bool cmp(int u,int v) { return dist[u] > dist[v]; } int p[N], s[N]; vector<int> t[N]; void setdsu() { for (int i = 1;i <= n;i++) p[i] = i, s[i] = 1; } int get(int u) { return p[u] = (u == p[u] ? u : get(p[u])); } bool merge(int u, int v) { int pu = get(u), pv = get(v); if (pu == pv) return false; if (s[pu] < s[pv]) swap(u, v), swap(pu, pv); s[pu] += s[pv]; p[pv] = pu; t[u].pb(v), t[v].pb(u); return (s[pu] == n); } int tin[N], tout[N], tim = 0; int up[N][20], mn[N][20], d[N]; void dfs(int u, int p) { tin[u] = tim++; for (int v : t[u]) { if (p == v) continue; up[v][0] = u; mn[v][0] = min(dist[u], dist[v]); d[v] = d[u] + 1; dfs(v, u); } tout[u] = tim; } bool anc(int u, int v) { return (tin[u] <= tin[v] && tout[u] >= tout[v]); } int lca(int u, int v) { if (anc(u, v)) return u; if (anc(v, u)) return v; for (int i = 19;i >= 0;i--) { if (!anc(up[u][i], v)) u = up[u][i]; } return up[u][0]; } void KarginSolve() { int m; cin >> n >> m; for (int i = 0;i < m;i++) { int u, v, w; cin >> u >> v >> w; g[u].pb({ v,w }), g[v].pb({ u,w }); } int k; cin >> k; fill(dist + 1, dist + n + 1, inf); pqueue<pair<int, int>> pq; for (int i = 0; i < k;i++) { int u; cin >> u; pq.push({ 0,u }), dist[u] = 0; } while (!pq.empty()) { int u = pq.top().second; pq.pop(); if (vis[u]) continue; vis[u] = true; for (auto it : g[u]) { int v = it.first, w = it.second; if (dist[v] > dist[u] + w) { dist[v] = dist[u] + w; pq.push({ -dist[v],v }); } } } vector<int> a; for (int i = 1;i <= n;i++) a.pb(i); sort(all(a), cmp); setdsu(); for (int u : a) { for (auto it : g[u]) { int v = it.first; if(dist[u] > dist[v]) continue; if (merge(u, v)) break; } } // for (int i = 1;i <= n;i++) { // cout << i << " : " << dist[i] << "-> "; // for (int v : t[i]) cout << v << ' '; // cout << '\n'; // } dfs(1, 0); up[1][0] = 1, mn[1][0] = dist[1]; for (int j = 1;j < 20;j++) { for (int i = 1;i <= n;i++) { up[i][j] = up[up[i][j - 1]][j - 1]; mn[i][j] = min(mn[i][j - 1], mn[up[i][j - 1]][j - 1]); } } int q; cin >> q; while (q--) { int u, v; cin >> u>> v; int l = lca(u, v); int ans = inf; int du = d[u] - d[l], dv = d[v] - d[l]; for (int i = 0;i < 20;i++) { if (du & (1 << i)) ans = min(ans, mn[u][i]), u = up[u][i]; if (dv & (1 << i)) ans = min(ans, mn[v][i]), v = up[v][i]; } cout << ans << '\n'; } } int main() { //Usaco Kargin; int test = 1; //cin >> test; while (test--) { KarginSolve(); } return 0; }

Compilation message (stderr)

plan.cpp:1:1: warning: multi-line comment [-Wcomment]
    1 | //-------------dilanyan------------\\
      | ^
plan.cpp:8:1: warning: multi-line comment [-Wcomment]
    8 | //------------------KarginDefines--------------------\\
      | ^
plan.cpp:22:1: warning: multi-line comment [-Wcomment]
   22 | //-------------------KarginConstants------------------\\
      | ^
plan.cpp:27:1: warning: multi-line comment [-Wcomment]
   27 | //-------------------KarginCode------------------------\\
      | ^
#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...