Submission #341389

#TimeUsernameProblemLanguageResultExecution timeMemory
341389_aniEvacuation plan (IZhO18_plan)C++17
54 / 100
4069 ms55540 KiB
#include <iostream> #include <vector> #include <algorithm> #include <queue> #include <map> using namespace std; vector<pair<int, int>> g[100002]; priority_queue<pair<int, int>> q; int d[100002], p[100002]; map<pair<int, int>, int> f; int main() { int n, m; cin >> n >> m; for (int i = 1; i <= n; i++) d[i] = 1'000'000'00 +69; for (int i = 0; i < m; i++) { int a, b, w; cin >> a >> b >> w; g[a].push_back({ b,w }); g[b].push_back({ a,w }); if (a > b)swap(a, b); f[{a, b}] = 1; } int k; cin >> k; for (int i = 0; i < k; i++) { int x; cin >> x; d[x] = 0; q.push({ 0, x }); } while (!q.empty()) { int sz = -q.top().first; int v = q.top().second; q.pop(); if (sz > d[v])continue; for (auto to : g[v]) if (to.second + d[v] < d[to.first]) { d[to.first] = to.second + d[v]; q.push({ -d[to.first], to.first }); } } int Q; cin >> Q; while (Q--) { int s, t; cin >> s >> t; if (f[{min(s, t), max(s, t)}]) { cout << min(d[s], d[t]) << '\n'; continue; } for (int i = 1; i <= n; i++) p[i] = -1'000'000; p[s] = d[s]; q.push({ d[s], s }); while (!q.empty()) { int sz = q.top().first; int v = q.top().second; q.pop(); if (sz < p[v])continue; for (auto to : g[v]) if (min(d[to.first], p[v]) > p[to.first]) { p[to.first] = min(d[to.first], p[v]); q.push({ p[to.first], to.first }); } } cout << p[t] << '\n'; } 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...