제출 #467786

#제출 시각아이디문제언어결과실행 시간메모리
467786spike1236Evacuation plan (IZhO18_plan)C++14
0 / 100
304 ms37972 KiB
#include <bits/stdc++.h> using namespace std; #define pb push_back #define mp make_pair #define f first #define s second #define ll long long #define ld long double #define all(_v) _v.begin(), _v.end() #define sz(_v) (int)_v.size() #define pii pair <int, int> #define pll pair <ll, ll> #define veci vector <int> #define vecll vector <ll> const int dx[4] = {1, -1, 0, 0}; const int dy[4] = {0, 0, -1, 1}; const double PI = 3.1415926535897932384626433832795; const double eps = 1e-9; const int MOD1 = 1e9 + 7; const int MOD2 = 998244353; const int MAXN = 1e5 + 10; int n, m, k; vector <pii> g[MAXN]; ll d[MAXN]; int bad[MAXN]; int up[MAXN][20]; ll mn[MAXN][20]; int tin[MAXN], tout[MAXN], tmr; int dep[MAXN]; void dfs(int v, int p) { dep[v] = dep[p] + 1; mn[v][0] = d[v]; up[v][0] = p; for(int i = 1; i < 20; ++i) { up[v][i] = up[up[v][i - 1]][i - 1]; mn[v][i] = min(mn[up[v][i - 1]][i - 1], mn[v][i - 1]); } tin[v] = ++tmr; for(auto to : g[v]) if(to.f != p) dfs(to.f, v); tout[v] = ++tmr; } bool upper(int v, int u) { return (tin[v] <= tin[u] && tout[v] >= tout[u]); } int lca(int v, int u) { if(upper(v, u)) return v; if(upper(u, v)) return u; for(int i = 19; i >= 0; --i) if(up[v][i] && !upper(up[v][i], u)) v = up[v][i]; return up[v][0]; } ll get_mn(int v, int lca) { assert(upper(lca, v)); ll res = 1e18; int len = dep[lca] - dep[v]; for(int i = 19; i >= 0; --i) if(up[v][i] && ((len >> i) & 1)) { res = min(res, mn[v][i]); v = up[v][i]; } return res; } bool cmp(const pii &a, const pii &b) { return (min(d[a.f], d[a.s]) < min(d[b.f], d[b.s])); } int get(int v) { return (v == tin[v] ? v : tin[v] = get(tin[v])); } bool unite(int v, int u) { v = get(v), u = get(u); if(v == u) return 0; if(rand() & 1) tin[v] = u; else tin[u] = v; return 1; } void solve() { cin >> n >> m; vector <pii> ed; for(int i = 1; i <= m; ++i) { int u, v, w; cin >> u >> v >> w; ed.pb(mp(u, v)); g[u].pb(mp(v, w)); g[v].pb(mp(u, w)); } cin >> k; for(int i = 1, v; i <= k; ++i) { cin >> v; bad[v] = 1; } priority_queue <pair <ll, int> > q; for(int i = 1; i <= n; ++i) { if(bad[i]) q.push(mp(0ll, i)); else d[i] = 1e18; } while(!q.empty()) { auto v = q.top(); q.pop(); v.f = -v.f; if(v.f != d[v.s]) continue; for(auto to : g[v.s]) { if(d[to.f] > v.f + to.s) { d[to.f] = v.f + to.s; q.push(mp(-d[to.f], to.f)); } } } sort(all(ed), &cmp); reverse(all(ed)); for(int i = 1; i <= n; ++i) g[i].clear(), tin[i] = i; for(auto it : ed) { if(unite(it.f, it.s)) { g[it.f].pb(mp(it.s, 0)); g[it.s].pb(mp(it.f, 0)); } } for(int i = 0; i <= n; ++i) for(int j = 0; j < 20; ++j) mn[i][j] = 1e18; dfs(1, 0); cin >> k; for(int cs = 1, u, v; cs <= k; ++cs) { cin >> u >> v; int LCA = lca(u, v); cout << min(get_mn(u, LCA), get_mn(v, LCA)) << '\n'; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int CNT_TESTS = 1; ///cin >> CNT_TESTS; for(int NUMCASE = 1; NUMCASE <= CNT_TESTS; ++NUMCASE) { solve(); if(NUMCASE != CNT_TESTS) cout << '\n'; } return 0; }/** 9 12 1 9 4 1 2 5 2 3 7 2 4 3 4 3 6 3 6 4 8 7 10 6 7 5 5 8 1 9 5 7 5 4 12 6 8 2 2 4 7 5 1 6 5 3 4 8 5 8 1 5 */
#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...