Submission #1102349

#TimeUsernameProblemLanguageResultExecution timeMemory
1102349InvMODValley (BOI19_valley)C++14
0 / 100
84 ms36680 KiB
#include<bits/stdc++.h> #define fi first #define se second #define pb push_back using namespace std; using ll = long long; using ull = unsigned long long; template<typename T> bool ckmx(T& a, const T& b){if(a <= b) return a = b, true; return false;} template<typename T> bool ckmn(T& a, const T& b){if(a >= b) return a = b, true; return false;} const int N = 1e5+5; const ll inf = 1e18+1; const int lg = 21; int n, q, S, Exit, timerDFS; int h[N], par[lg][N], tin[N], tout[N]; ll val[lg][N], dist[N], near[N]; bool isShop[N]; vector<pair<int,ll>> adj[N]; pair<int,int> Edge[N]; bool in(int u, int v){ return tin[u] <= tin[v] && tout[v] <= tout[u]; } void dfs(int x, int p){ tin[x] = ++timerDFS; near[x] = (isShop[x] ? dist[x] : inf); for(pair<int,ll> e : adj[x])if(e.fi != p){ int v = e.fi; ll w = e.se; h[v] = h[x] + 1; dist[v] = dist[x] + w; par[0][v] = x; for(int i = 1; i < lg; i++){ par[i][v] = par[i-1][par[i-1][v]]; } dfs(v, x); ckmn(near[x], near[v]); } tout[x] = ++timerDFS; return; } ll get(int x, int asc){ int k = h[x] - h[asc]+1; ll res = inf; for(int i = 0; i < lg; i++){ if(k>>i&1){ ckmn(res, val[i][x]); x = par[i][x]; } } return res; } void solve() { cin >> n >> S >> q >> Exit; for(int i = 1; i < n; i++){ int u,v,w; cin >> u >> v >> w; adj[u].pb(make_pair(v, w)); adj[v].pb(make_pair(u, w)); Edge[i] = make_pair(u, v); } for(int i = 1; i <= S; i++){ int v; cin >> v; isShop[v] = true; } dfs(1, -1); for(int i = 1; i <= n; i++){ if(near[i] == inf){ val[0][i] = inf; } else{ val[0][i] = -2*dist[i] + near[i]; } } for(int i = 1; i < lg; i++){ for(int j = 1; j <= n; j++){ val[i][j] = min(val[i-1][j], val[i-1][par[i-1][j]]); } } while(q--){ int i,R; cin >> i >> R; if(h[Edge[i].fi] > h[Edge[i].se]) swap(Edge[i].fi, Edge[i].se); int v = Edge[i].se; if((in(v, R) && in(v, Exit)) || (!in(v, R) && !in(v, Exit))){ cout << "escaped\n"; } else{ if(!in(v, R)){ ll answer = near[R]; if(answer == inf){ cout << "oo\n"; } else{ cout << answer <<"\n"; } } else{ ll answer = get(R, v); if(answer == inf){ cout << "oo\n"; } else{ cout << dist[R] + answer <<"\n"; } } } } } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define name "task" if(fopen(name".INP", "r")){ freopen(name".INP", "r", stdin); freopen(name".OUT", "w", stdout); } solve(); return 0; }

Compilation message (stderr)

valley.cpp: In function 'int32_t main()':
valley.cpp:138:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  138 |         freopen(name".INP", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
valley.cpp:139:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  139 |         freopen(name".OUT", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...