Submission #173433

#TimeUsernameProblemLanguageResultExecution timeMemory
173433touristEvacuation plan (IZhO18_plan)C++14
100 / 100
1498 ms48116 KiB
//https://oj.uz/submission/173432 #include <iostream> #include <algorithm> #include <assert.h> #include <vector> #include <queue> using namespace std; #define ll long long #define sz(x) (int)x.size() #define pii pair < int, int > #define METH ios::sync_with_stdio(0); cin.tie(0); #define BEGIN cout << "BEGIN" << endl; #define END cout << "END" << endl; const int N = 2e5 + 9; const int mod = 1e9 + 7; /// ANOTHER HASH MOD: 228228227 const int log = 20; const int prime = 29; /// ANOTHER HASH PRIME: 997 const int INF = 1e9 + 7; int used[N], anc[N][log + 5], tout[N]; int n, m, k, q, boss[N], tin[N], tamer; int par[N]; priority_queue < pii > pq; vector < int > dist(N, INF), ng[N]; vector < pii > g[N]; inline void read() {///OK int a, b, c; cin >> n >> m; for (int i = 1; i <= m; i++) { cin >> a >> b >> c; g[a].push_back({b, c}); g[b].push_back({a, c}); } cin >> k; for (int i = 1; i <= k; i++) { scanf("%d", &a); pq.push({0, a}); dist[a] = 0; } } void dij() {///OK while (pq.size()) { int cur = pq.top().second; int d = -pq.top().first; pq.pop(); if (d > dist[cur]) { continue; } for (pii i : g[cur]) { int u = i.first; int len = i.second; if (dist[cur] + len < dist[u]) { dist[u] = dist[cur] + len; pq.push({-dist[u], u}); } } } } int get_par(int a) {///OK if (a == boss[a]) { return a; } return boss[a] = get_par(boss[a]); } void connect(int a, int b) {///OK a = get_par(a); b = get_par(b); if (a != b) { boss[a] = b; } } void dfs(int v) { tin[v] = ++tamer; anc[v][0] = par[v]; for (int i = 1; i <= log; i++) { anc[v][i] = anc[anc[v][i - 1]][i - 1]; } for (int i : ng[v]) { if (i == par[v]) { continue; } dfs(i); } tout[v] = ++tamer; } void build_tree() { vector < pii > temp; for (int i = 1; i <= n; i++) { temp.push_back({dist[i], i}); boss[i] = i; } sort(temp.begin(), temp.end(), greater < pii >()); int last = 0; for (pii i : temp) { int a = i.second; used[a] = 1; last = a; for (pii j : g[a]) { int b = j.first; if (used[b]) { b = get_par(b); if (a != b) { connect(b, a); par[b] = a; ng[a].push_back(b); ng[b].push_back(a); } } } } par[last] = last; dfs(last); } bool upper(int a, int b) { return tin[a] <= tin[b] && tout[a] >= tout[b]; } int get_lca(int a, int b) { if (upper(a, b)) { return a; } if (upper(b, a)) { return b; } for (int i = log; i >= 0; i--) { if (!upper(anc[a][i], b)) { a = anc[a][i]; } } return anc[a][0]; } inline void solve() { dij(); build_tree(); cin >> q; for (int i = 1; i <= q; i++) { int s, t; scanf("%d %d", &s, &t); int lca = get_lca(s, t); cout << dist[lca] << endl; } } int main() { int t = 1; while (t--) { read(); solve(); } }

Compilation message (stderr)

plan.cpp: In function 'void read()':
plan.cpp:42:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &a);
   ~~~~~^~~~~~~~~~
plan.cpp: In function 'void solve()':
plan.cpp:150:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   int s, t; scanf("%d %d", &s, &t);
             ~~~~~^~~~~~~~~~~~~~~~~
#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...