Submission #65177

#TimeUsernameProblemLanguageResultExecution timeMemory
65177kingpig9Evacuation plan (IZhO18_plan)C++11
100 / 100
1483 ms20992 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; const int MAXN = 1e5 + 10, INF = 1e8; #define debug(...) fprintf(stderr, __VA_ARGS__) #define all(v) (v).begin(), (v).end() #define fi first #define se second #define fillchar(a, s) memset((a), (s), sizeof(a)) int N, M; vector<pii> adj[MAXN]; struct union_find { int par[MAXN]; void reset() { for (int i = 0; i < N; i++) { par[i] = i; } } int find (int x) { return x == par[x] ? x : par[x] = find(par[x]); } void merge (int x, int y) { par[find(x)] = find(y); } } uf; int K, G[MAXN]; int dist[MAXN]; bool vis[MAXN]; int dord[MAXN]; //order of dist int Q; int S[MAXN], T[MAXN]; int ind[MAXN], lo[MAXN], hi[MAXN], mid[MAXN]; //code for dijkstra void dijkstra() { priority_queue<pii, vector<pii>, greater<pii>> pq; for (int i = 0; i < N; i++) { dist[i] = INF; } for (int i = 0; i < K; i++) { int x = G[i]; dist[x] = 0; pq.push({0, x}); } while (!pq.empty()) { int u = pq.top().se, w = pq.top().fi; pq.pop(); if (vis[u]) { continue; } vis[u] = true; for (pii e : adj[u]) { int v = e.se; int nw = e.fi + w; if (dist[v] > nw) { dist[v] = nw; pq.push({nw, v}); } } } /* for (int i = 0; i < N; i++) { debug("dist[%d] = %d\n", i, dist[i]); } */ } bool cmpd (int x, int y) { return dist[x] > dist[y]; } bool cmp (int x, int y) { return mid[x] > mid[y]; } int main() { scanf("%d %d", &N, &M); for (int i = 0; i < M; i++) { int a, b, w; scanf("%d %d %d", &a, &b, &w); a--; b--; adj[a].push_back({w, b}); adj[b].push_back({w, a}); } scanf("%d", &K); for (int i = 0; i < K; i++) { scanf("%d", &G[i]); G[i]--; } dijkstra(); scanf("%d", &Q); for (int i = 0; i < Q; i++) { scanf("%d %d", &S[i], &T[i]); S[i]--; T[i]--; lo[i] = 0; //ans always >= lo[i], always < hi[i] hi[i] = INF; } iota(dord, dord + N, 0); sort(dord, dord + N, cmpd); //code for parallel bsearch for (int iter = 0; iter < 28; iter++) { for (int i = 0; i < Q; i++) { mid[i] = (lo[i] + hi[i]) / 2; ind[i] = i; } sort(ind, ind + Q, cmp); uf.reset(); int dptr = 0; for (int ii = 0; ii < Q; ii++) { int i = ind[ii]; for (; dptr < N && dist[dord[dptr]] >= mid[i]; dptr++) { //try to merge if you can int x = dord[dptr]; for (pii e : adj[x]) { int y = e.se; if (dist[y] >= mid[i]) { uf.merge(x, y); } } } if (uf.find(S[i]) == uf.find(T[i])) { lo[i] = mid[i]; } else { hi[i] = mid[i]; } } } //code for final output for (int i = 0; i < Q; i++) { printf("%d\n", lo[i]); } }

Compilation message (stderr)

plan.cpp: In function 'int main()':
plan.cpp:92:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d", &N, &M);
  ~~~~~^~~~~~~~~~~~~~~~~
plan.cpp:95:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d %d", &a, &b, &w);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
plan.cpp:102:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &K);
  ~~~~~^~~~~~~~~~
plan.cpp:104:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &G[i]);
   ~~~~~^~~~~~~~~~~~~
plan.cpp:110:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &Q);
  ~~~~~^~~~~~~~~~
plan.cpp:112:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d", &S[i], &T[i]);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~
#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...