Submission #565711

#TimeUsernameProblemLanguageResultExecution timeMemory
565711RealSnakeEvacuation plan (IZhO18_plan)C++14
0 / 100
12 ms468 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[1001]; int d[1001], g[1001]; bool vis[1001], vis2[1001]; int n, m, s, t, o; int dfs2(int x) { if(!d[x]) return 0; if(g[x]) return g[x]; vis2[x] = 1; int y = 1e9; for(auto i : v[x]) { if(!vis2[i.first]) y = min(y, dfs2(i.first) + i.second); } g[x] = y; vis2[x] = 0; return y; } int dfs(int x) { if(x == t) return d[x]; if(d[x] == 1e9) { for(int i = 1; i <= n; i++) g[i] = 0; d[x] = dfs2(x); } vis[x] = 1; int mx = 0, o = 0; for(auto i : v[x]) { if(!vis[i.first]) { mx = max(mx, dfs(i.first)); o = 1; } } vis[x] = 0; if(!o) return 1e9; return min(d[x], mx); } 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; } int q; cin >> q; while(q--) { cin >> s >> t; if(d[s] == 1e9) { for(int i = 1; i <= n; i++) g[i] = 0; d[s] = dfs2(s); } if(d[t] == 1e9) { for(int i = 1; i <= n; i++) g[i] = 0; d[t] = dfs2(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; } cout << dfs(s) << "\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...