Submission #169216

#TimeUsernameProblemLanguageResultExecution timeMemory
169216blastEvacuation plan (IZhO18_plan)C++14
0 / 100
180 ms27528 KiB
#include <bits/stdc++.h> //#define FILENAME "" #define all(a) (a).begin(), (a).end() #define sz(a) (int)(a).size() #define pb push_back #define F first #define S second using namespace std; //#define int long long const int N = 1e5 + 5; const int INF = 1e9 + 5; const double PI = acos(-1); typedef long long ll; const int dx[] = {1, -1, 0, 0}; const int dy[] = {0, 0, 1, -1}; const int mod = 1e9 + 7; int n, m, k, u[N], v[N], d[N], p[N], s[N]; vector<pair<int,int>> g[N]; set<pair<int,int>> st; vector<int> gr[N]; int tin[N], tout[N], timer, up[N][21], cost[N][21], h[N]; int get(int a) { if (p[a] == a) return a; return p[a] = get(p[a]); } void unite(int a, int b) { a = get(a); b = get(b); if (s[a] < s[b]) swap(a, b); s[a] += s[b]; p[b] = a; } void dfs(int v, int p) { tin[v] = ++timer; up[v][0] = p; cost[v][0] = min(d[v], d[p]); h[v] = h[p] + 1; for (int i = 1; i <= 20; i++) { up[v][i] = up[up[v][i - 1]][i - 1]; cost[v][i] = min(cost[up[v][i - 1]][i - 1], cost[v][i - 1]); } for (int i = 0; i < sz(gr[v]); i++) { int to = gr[v][i]; if (to != p) dfs(to, v); } tout[v] = ++timer; } bool upper(int a, int b) { return tin[a] <= tin[b] && tout[a] >= tout[b]; } int lca(int a, int b) { if (upper(a, b)) return a; if (upper(b, a)) return b; for (int i = 20; i >= 0; i--) { if (!upper(up[a][i], b) && up[a][i]) a = up[a][i]; } return up[a][0]; } int ans(int a, int res) { int mn = d[a]; if (res == 0) return mn; for (int i = 0; i <= 20 && res; i++, res >>= 1) { if (res & 1) { mn = min(mn, cost[a][i]); a = up[a][i]; } } return min(mn, cost[a][0]); } int main() { #ifdef FILENAME freopen(FILENAME".in", "r", stdin); freopen(FILENAME ".out", "w", stdout); #endif ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> m; for (int i = 1, x; i <= m; i++) { cin >> u[i] >> v[i] >> x; g[u[i]].pb({v[i], x}); g[v[i]].pb({u[i], x}); } cin >> k; for (int i = 1; i <= n; i++) { d[i] = INF; p[i] = i; s[i] = 1; } for (int i = 1; i <= k; i++) { int x; cin >> x; st.insert({0, x}); d[x] = 0; } while(st.size()) { int v = st.begin() -> S; st.erase(st.begin()); for (int i = 0; i < sz(g[v]); i++) { int to = g[v][i].F; int len = g[v][i].S; if (d[v] + len < d[to]) { st.erase({d[to], to}); d[to] = d[v] + len; st.insert({d[to], to}); } } } vector<pair<int,pair<int,int>>> w; for (int i = 1; i <= m; i++) { w.pb({min(d[u[i]], d[v[i]]),{u[i], v[i]}}); } sort(all(w)); for (int i = sz(w) - 1; i >= 0; i--) { int a = w[i].S.F, b = w[i].S.S; if (get(a) != get(b)) { unite(a, b); gr[a].pb(b); gr[b].pb(a); } } d[0] = INF; for (int i = 1; i <= N - 5; i++) for (int j = 0; j <= 20; j++) cost[i][j] = INF; dfs(1, 0); int q; cin >> q; while(q--) { int a, b; cin >> a >> b; int l = lca(a, b); cout << min(ans(a, h[a] - h[l]), ans(b, h[b] - h[l])) << "\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...