제출 #565763

#제출 시각아이디문제언어결과실행 시간메모리
565763RealSnakeEvacuation plan (IZhO18_plan)C++14
23 / 100
4067 ms22300 KiB
#include "bits/stdc++.h" using namespace std; #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; #define ll long long #define mod 1000000007 ofstream fout(".out"); ifstream fin(".in"); vector<pair<int, int>> v[100001]; int d[100001], g[100001]; bool vis[100001]; int n, m, s, t, o, mn, qq; void dijkstra(int y) { set<pair<int, int>> s; s.insert({0, y}); while(s.size()) { pair<int, int> p = *s.begin(); s.erase(s.begin()); int x = p.second; int cost = p.first; if(cost > d[x]) continue; d[x] = cost; for(auto i : v[x]) { if(cost + i.second < d[i.first]) { d[i.first] = cost + i.second; s.insert({cost + i.second, i.first}); } } } } pair<int, int> dfs(int x) { if(x == t) return {d[x], 1}; if(g[x]) return {g[x], 1}; vis[x] = 1; int mx = 0, o = 0; for(auto i : v[x]) { if(d[i.first] && !vis[i.first]) { pair<int, int> p = dfs(i.first); if(p.second) { mx = max(mx, p.first); o = 1; } } } vis[x] = 0; if(o && qq != 1) g[x] = min(d[x], mx); return {min(d[x], mx), o}; } signed main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> n >> m; for(int i = 1; i <= n; i++) d[i] = 1e9; for(int i = 0; i < m; i++) { int a, b, c; cin >> a >> b >> c; v[a].push_back({b, c}); v[b].push_back({a, c}); } int k; cin >> k; for(int i = 0; i < k; i++) { int x; cin >> x; d[x] = 0; } for(int i = 1; i <= n; i++) { if(!d[i]) dijkstra(i); } int q; cin >> q; qq = q; while(q--) { cin >> s >> t; bool b = 0; for(auto i : v[s]) { if(i.first == t) { b = 1; break; } } if(b) { cout << min(d[s], d[t]) << "\n"; continue; } mn = 1e9; for(int i = 1; i <= n; i++) g[i] = 0; cout << dfs(s).first << "\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...