Submission #1118360

#TimeUsernameProblemLanguageResultExecution timeMemory
1118360gdragonEvacuation plan (IZhO18_plan)C++17
100 / 100
589 ms42668 KiB
#include <bits/stdc++.h> using namespace std; #define TASK "long" #define fi first #define se second #define ll long long #define pb push_back #define ALL(x) (x).begin(), (x).end() #define GETBIT(mask, i) ((mask) >> (i) & 1) #define MASK(i) ((1LL) << (i)) #define SZ(x) ((int)(x).size()) #define mp make_pair #define CNTBIT(mask) __builtin_popcount(mask) template<class X, class Y> bool maximize(X &x, Y y){ if (x < y) {x = y; return true;} return false;}; template<class X, class Y> bool minimize(X &x, Y y){ if (x > y) {x = y; return true;} return false;}; typedef pair<int, int> ii; const int N = 1e5 + 5; const int M = 5e5 + 5; const int inf = 1e9 + 7; const long long INF = (long long)1e18 + 7; const int mod = 1e9 + 7; struct DSU { int n; vector<int> par; vector<vector<int>> sz; DSU(int _n = 0) { n = _n; par.assign(n + 5, -1); sz.assign(n + 2, vector<int>()); for(int i = 1; i <= n; i++) sz[i].push_back(i); } int root(int v) { return par[v] < 0 ? v : (par[v] = root(par[v])); } bool join(int x, int y) { // x = root(x); // y = root(y); // if (x == y) return false; if (par[y] < par[x]) swap(x, y); par[x] += par[y]; for(int p: sz[y]) sz[x].push_back(p); sz[y].clear(); par[y] = x; return true; } } dsu; vector<ii> adj[N]; int n, m, q, numNpp; vector<int> Npps, val; int d[N]; pair<int, int> e[M]; vector<ii> queries[N]; void read() { cin >> n >> m; for(int i = 1; i <= m; i++) { int u, v, c; cin >> u >> v >> c; adj[u].push_back({v, c}); adj[v].push_back({u, c}); e[i] = {u, v}; } cin >> numNpp; Npps.assign(numNpp, 0); for(int &i: Npps) cin >> i; } void calDistToNpp() { priority_queue<ii, vector<ii>, greater<ii>> heap; memset(d, 0x3f, sizeof(d)); for(int i: Npps) { heap.push({0, i}); d[i] = 0; } while(!heap.empty()) { ii tmp = heap.top(); heap.pop(); int u = tmp.se; int du = tmp.fi; if (du != d[u]) continue; for(auto [v, c]: adj[u]) { if (minimize(d[v], d[u] + c)) { heap.push(mp(d[v], v)); } } } } bool vis[N]; bool dfs(int u, int t, int lim) { if (vis[u]) return false; vis[u] = 1; if (d[u] < lim) return false; if (u == t) return true; for(auto [v, c]: adj[u]) if (d[v] >= lim) { if (dfs(v, t, lim)) return true; } return false; } bool check(int s, int t, int lim) { fill(vis + 1, vis + n + 1, 0); return dfs(s, t, lim); } int ans[N]; void solve() { calDistToNpp(); cin >> q; for(int i = 1; i <= q; i++) { int u, v; cin >> u >> v; queries[u].push_back({v, i}); queries[v].push_back({u, i}); } sort(e + 1, e + m + 1, [&](const ii &x, const ii &y) { return min(d[x.fi], d[x.se]) > min(d[y.fi], d[y.se]); }); dsu = DSU(n); for(int i = 1; i <= m; i++) { // dsu.join(e[i].fi, e[i].se); int r1 = dsu.root(e[i].fi), r2 = dsu.root(e[i].se); if (r1 == r2) continue; int mn = min(d[e[i].fi], d[e[i].se]); if (SZ(dsu.sz[r1]) > SZ(dsu.sz[r2])) swap(r1, r2); for(int x: dsu.sz[r1]) { for(auto [y, id]: queries[x]) if (ans[id] == 0) { if (dsu.root(y) == r2) { ans[id] = mn; } } } dsu.join(r1, r2); // cerr << e[i].fi << ' ' << d[e[i].fi] << ' ' << e[i].se << ' ' << d[e[i].se] << endl; // for(auto [x, id]: queries[e[i].fi]) if (dsu.root(x) == dsu.root(e[i].fi)) { // if (ans[id] == 0) ans[id] = min(d[e[i].fi], d[e[i].se]); // cout << i << ' ' << ans[id] << ' ' << min(d[e[i].fi], d[e[i].se]) << endl; // } // for(auto [x, id]: queries[e[i].se]) if (dsu.root(x) == dsu.root(e[i].se)) { // if (ans[id] == 0) ans[id] = min(d[e[i].fi], d[e[i].se]); // cout << i << ' ' << ans[id] << ' ' << min(d[e[i].fi], d[e[i].se]) << endl; // } } for(int i = 1; i <= q; i++) cout << ans[i] << endl; // return; // for(int i = 1; i <= n; i++) val.push_back(d[i]); // sort(ALL(val)); // val.erase(unique(ALL(val)), val.end()); // cin >> q; // for(int iter = 0; iter < q; iter++) { // int s, t; cin >> s >> t; // if (max(n, q) > 1e3 && q != 1) { // cout << min(d[s], d[t]) << endl; // continue; // } // int l = 0, r = SZ(val) - 1, ans = 0; // while(l <= r) { // int mid = (l + r) >> 1; // if (check(s, t, val[mid])) { // ans = val[mid]; // l = mid + 1; // } // else r = mid - 1; // } // cout << ans << endl; // } } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); if (fopen(TASK".inp", "r")) { freopen(TASK".inp", "r", stdin); freopen(TASK".out", "w", stdout); } int test = 1; // cin >> test; while(test--) { read(); solve(); } return 0; } // rmq - rmq2d // hash // fw - fw2d // segtree

Compilation message (stderr)

plan.cpp: In function 'int main()':
plan.cpp:163:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  163 |         freopen(TASK".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
plan.cpp:164:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  164 |         freopen(TASK".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#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...