Submission #169225

#TimeUsernameProblemLanguageResultExecution timeMemory
169225blastEvacuation plan (IZhO18_plan)C++14
22 / 100
269 ms30324 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) { 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 ans(int a, int b) { int mn = INF; if (upper(a, b)) { for (int i = 20; i >= 0; i--) { if (!upper(up[b][i], a) && up[b][i]) { mn = min(mn, cost[b][i]); b = up[b][i]; } } mn = min(mn, cost[b][0]); } if (upper(b, a)) { for (int i = 20; i >= 0; i--) { if (!upper(up[a][i], b) && up[a][i]) { mn = min(mn, cost[a][i]); a = up[a][i]; } } mn = min(mn, cost[a][0]); } if (!upper(a, b) && !upper(b, a)) { for (int i = 20; i >= 0; i--) { if (!upper(up[a][i], b) && up[a][i]) { mn = min(mn, cost[a][i]); a = up[a][i]; } } mn = min(mn, cost[a][0]); for (int i = 20; i >= 0; i--) { if (!upper(up[b][i], a) && up[b][i]) { mn = min(mn, cost[b][i]); b = up[b][i]; } } mn = min(mn, cost[b][0]); } return mn; } 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(get(a), get(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; cout << ans(a, b) << "\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...