Submission #1129495

#TimeUsernameProblemLanguageResultExecution timeMemory
1129495zhasynEvacuation plan (IZhO18_plan)C++20
100 / 100
1186 ms61088 KiB
#include <bits/stdc++.h> #define pb push_back #define pf push_front using namespace std; #define F first #define S second typedef long long ll; #define pii pair <int, int> #define pll pair <ll, ll> typedef long double ld; const ll N = 2 * 1e5 + 100, M = 500 + 10, len = 315, inf = 1e18; const ll mod = 998244353; ll um(ll a, ll b){ return (1LL * a * b) % mod; } ll subr(ll a, ll b){ return ((1LL * a - b) % mod + mod) % mod; } ll bp(ll x, ll step){ ll res = 1; while(step){ if(step & 1) res = um(res, x); x = um(x, x); step /= 2; } return res; } ll inv(ll x){ return bp(x, mod - 2); } int n, m, k, d[N], p[N], sz[N]; vector <pii> q[N], qr[N]; void bfs(int x){ set <pii> st; d[x] = 0; st.insert({d[x], x}); while((int)st.size() != 0){ int v = st.begin()->S; st.erase(st.begin()); for(auto u : q[v]){ if(d[u.F] > d[v] + u.S){ d[u.F] = d[v] + u.S; st.insert({d[u.F], u.F}); } } } } int tin[N], tout[N], timer, up[N][25], mn[N][25]; vector <pii> rev[N]; void dfs(int v, int p, int from){ tin[v] = ++timer; up[v][0] = p; mn[v][0] = from; 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]); } for(auto u : rev[v]){ if(u.F == p) continue; dfs(u.F, v, u.S); } tout[v] = timer; } bool upper(int a, int b){ if(tin[a] <= tin[b] && tout[b] <= tout[a]) return true; else return false; } int lca(int a, int b){ if(upper(a, b)) swap(a, b); int ans = INT_MAX; if(upper(b, a)){ for(int i = 20; i >= 0; i--){ if(up[a][i] == 0) continue; if(upper(up[a][i], b) == false){ ans = min(ans, mn[a][i]); a = up[a][i]; } } return min(ans, mn[a][0]); } for(int i = 20; i >= 0; i--){ if(up[a][i] == 0) continue; if(upper(up[a][i], b) == false){ ans = min(ans, mn[a][i]); a = up[a][i]; } } for(int i = 20; i >= 0; i--){ if(up[b][i] == 0) continue; if(upper(up[b][i], a) == false){ ans = min(ans, mn[b][i]); b = up[b][i]; } } return min(ans, min(mn[a][0], mn[b][0])); } int get(int x){ if(x == p[x]) return x; else return p[x] = get(p[x]); } void addthem(ll a, ll b, ll x){ int oa = a, ob = b; a = get(a); b = get(b); if(a != b){ rev[oa].pb({ob, x}); rev[ob].pb({oa, x}); if(sz[b] > sz[a]) swap(a, b); sz[a] += sz[b]; p[b] = a; } } int main() { //ios_base::sync_with_stdio(false); //cin.tie(nullptr); //cout.tie(nullptr); cin >> n >> m; for(int i = 0, a, b, c; i < m; i++){ cin >> a >> b >> c; q[a].pb({b, c}); q[b].pb({a, c}); } for(int i = 1; i <= n; i++){ d[i] = INT_MAX; p[i] = i; sz[i] = 1; } cin >> k; for(int i = 0, x; i < k; i++){ cin >> x; bfs(x); } vector <pair <int, pii>> vec; for(int i = 1; i <= n; i++){ for(auto u : q[i]){ if(i > u.F) vec.pb({-min(d[u.F], d[i]), {i, u.F}}); } } sort(vec.begin(), vec.end()); for(auto u : vec){ addthem(u.S.F, u.S.S, -u.F); } dfs(1, 0, INT_MAX); // for(int i = 1; i <= n; i++){ // for(auto u : rev[i]){ // cout << i << " "<< u.F << " " << -u.S << endl; // } // } int quer; cin >> quer; for(int i = 0, a, b; i < quer; i++){ cin >> a >> b; cout << lca(a, b) << endl; } 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...