제출 #679522

#제출 시각아이디문제언어결과실행 시간메모리
679522Chal1shkanEvacuation plan (IZhO18_plan)C++14
23 / 100
1129 ms83184 KiB
#include <bits/stdc++.h> # define pb push_back # define ff first # define ss second # define nl "\n" typedef long long ll; typedef unsigned long long ull; typedef long double ld; const ll maxn = 1e5 + 25; const ll maxl = 23 + 0; const ll inf = 1e18 + 0; const ll mod = 998244353; const ll dx[] = {-1, 1, 0, 0}; const ll dy[] = {0, 0, -1, 1}; using namespace std; ll n, m, k, q, d[maxn]; vector <pair <ll, ll> > g[maxn]; ll par[maxn], sz[maxn]; vector <ll> gr[maxn]; ll tin[maxn], tout[maxn]; ll up[maxn][maxl], timer; ll mn[maxn][maxl], l[maxn]; void dijkstra (ll s) { d[s] = 0; set <pair <ll, ll> > q; q.insert({0, s}); while (!q.empty()) { ll v = q.begin() -> ss; q.erase(q.begin()); for (auto i : g[v]) { ll to = i.ff, w = i.ss; if (d[v] + w < d[to]) { q.erase({d[to], to}); d[to] = d[v] + w; q.insert({d[to], to}); } } } } ll find_set (ll v) { if (v == par[v]) return v; else return par[v] = find_set(par[v]); } bool unite (ll u, ll v) { u = find_set(u); v = find_set(v); if (u != v) { if (sz[u] < sz[v]) swap(u, v); par[v] = u; sz[u] += sz[v]; return 1; } else { return 0; } } void build () { vector <pair <ll, ll> > v; for (ll i = 1; i <= n; ++i) { par[i] = i; sz[i] = 1; v.pb({d[i], i}); } sort(v.rbegin(), v.rend()); for (auto it : v) { ll x = it.ss; tin[x] = 1; for (auto i : g[x]) { ll to = i.ff; if (tin[to]) { if (unite(x, to)) { gr[x].pb(to); gr[to].pb(x); } } } } } void dfs (ll v, ll pa) { up[v][0] = pa; mn[v][0] = d[v]; l[v] = l[pa] + 1; for (int i = 1; i < maxl; ++i) { up[v][i] = up[up[v][i - 1]][i - 1]; mn[v][i] = min(mn[v][i - 1], mn[up[v][i - 1]][i - 1]); } tin[v] = ++timer; for (ll to : gr[v]) { if (!tin[to]) { dfs(to, v); } } tout[v] = timer; } bool ok(ll u, ll v) { return (tin[u] <= tin[v] && tout[v] <= tout[u]); } ll get (ll u, ll v) { if (l[u] > l[v]) swap(u, v); ll ans = min(d[u], d[v]); ll dist = l[v] - l[u]; for (ll i = maxl - 1; i >= 0; --i) { if (dist & (1LL << i)) { ans = min(ans, mn[v][i]); v = up[v][i]; } } if (u == v) { return ans; } else { for (ll i = maxl - 1; i >= 0; --i) { if (up[u][i] != up[v][i]) { ans = min(ans, mn[u][i]); ans = min(ans, mn[v][i]); u = up[u][i]; v = up[v][i]; } } ans = min(ans, mn[u][0]); return ans; } } void ma1n (){ cin >> n >> m; for (ll i = 1; i <= n; ++i){ d[i] = inf; for(ll j = 0; j < maxl; ++j) mn[i][j] = inf; } for (ll i = 1; i <= m; ++i){ ll u, v, w; cin >> u >> v >> w; g[u].pb({v, w}); g[v].pb({u, w}); } cin >> k; for(ll i = 1; i <= k; ++i){ ll v; cin >> v; dijkstra(v); } build(); memset(tin, 0, sizeof (tin)); dfs(1, 1); cin >> q; for (ll i = 1; i <= q; ++i){ ll u, v; cin >> u >> v; cout << get(u, v) << nl; } } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); // freopen("mooyomooyo.in", "r", stdin); // freopen("mooyomooyo.out", "w", stdout); int ttt = 1; // cin >> ttt; for (int test = 1; test <= ttt; ++test) { // cout << "Case " << test << ":" << nl; ma1n(); } 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...