제출 #595127

#제출 시각아이디문제언어결과실행 시간메모리
595127tamthegodEvacuation plan (IZhO18_plan)C++14
100 / 100
655 ms73104 KiB
#include<bits/stdc++.h> #define int long long #define pb push_back #define fi first #define se second using namespace std; using ll = long long; using ld = long double; using ull = unsigned long long; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); const int maxN = 5e5 + 5; const int mod = 1e9 + 7; const ll oo = 1e9; int n, m; int lab[maxN]; int d[maxN]; int id[maxN]; int q; struct TQuery { int s, t, id; int low, high, mid; } qu[maxN]; int Findset(int u) { return lab[u] < 0 ? u : lab[u] = Findset(lab[u]); } void Unite(int u, int v) { int r = Findset(u), s = Findset(v); if(r == s) return; if(lab[r] > lab[s]) swap(r, s); lab[r] += lab[s]; lab[s] = r; } struct TEdge { int u, v, w; } e[maxN]; struct TAdj { int v, w; }; vector<TAdj> adj[maxN]; int k, a[maxN]; struct TPQRect { int vertex, dlab; bool operator < (const TPQRect& other) const { return other.dlab < dlab; } bool Valid() const { return d[vertex] == dlab; } }; bool Relax(int x, int y, int w) { if(d[y] > d[x] + w) { d[y] = d[x] + w; return true; } return false; } void Dijkstra() { priority_queue<TPQRect> pq; memset(d, 3, sizeof d); for(int i=1; i<=k; i++) { d[a[i]] = 0; pq.push({a[i], 0}); } do { TPQRect r = pq.top(); pq.pop(); if(!r.Valid()) continue; int u = r.vertex; for(TAdj a : adj[u]) { if(Relax(u, a.v, a.w)) pq.push({a.v, d[a.v]}); } } while(!pq.empty()); } void ReadInput() { cin >> n >> m; for(int i=1; i<=m; i++) { int u, v, w; cin >> u >> v >> w; e[i] = {u, v, w}; adj[u].pb({v, w}); adj[v].pb({u, w}); } cin >> k; for(int i=1; i<=k; i++) cin >> a[i]; cin >> q; for(int i=1; i<=q; i++) { int s, t; cin >> s >> t; qu[i].s = s, qu[i].t = t, qu[i].id = i; qu[i].low = 1, qu[i].high = oo; } } bool chk(int s, int t, int val) { memset(lab, -1, sizeof lab); for(int i=1; i<=m; i++) if(d[e[i].u] >= val && d[e[i].v] >= val) Unite(e[i].u, e[i].v); return Findset(s) == Findset(t); } void parallel_bs() { memset(lab, -1, sizeof lab); for(int i=1; i<=q; i++) qu[i].mid = (qu[i].low + qu[i].high) / 2; sort(qu + 1, qu + q + 1, [](TQuery p, TQuery q) { return p.mid > q.mid; }); int p = 1; for(int i=1; i<=q; i++) { while(p <= m && min(d[e[p].u], d[e[p].v]) >= qu[i].mid) { Unite(e[p].u, e[p].v); p++; } //cout << (Findset(qu[i].s) == Findset(qu[i].t));exit(0); if(Findset(qu[i].s) == Findset(qu[i].t)) qu[i].low = qu[i].mid + 1; else qu[i].high = qu[i].mid - 1; } } void Solve() { Dijkstra(); sort(e + 1, e + m + 1, [](TEdge p, TEdge q) { return min(d[p.u], d[p.v]) > min(d[q.u], d[q.v]); }); for(int _=1; _<=30; _++) parallel_bs(); sort(qu + 1, qu + q + 1, [](TQuery p, TQuery q) { return p.id < q.id; }); // cout << qu[1].high;return; for(int i=1; i<=q; i++) cout << qu[i].high << '\n'; } int32_t main() { // freopen("x.inp", "r", stdin); ios_base::sync_with_stdio(false); cin.tie(nullptr); ReadInput(); Solve(); }
#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...