제출 #785731

#제출 시각아이디문제언어결과실행 시간메모리
785731chanhchuong123Valley (BOI19_valley)C++14
100 / 100
213 ms38856 KiB
#include <bits/stdc++.h> using namespace std; #define task "" #define fi first #define se second #define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i) #define FORD(i, a, b) for (int i = (b), _a = (a); i >= _a; --i) template <typename T1, typename T2> bool minimize(T1 &a, T2 b) { if (a > b) {a = b; return true;} return false; } template <typename T1, typename T2> bool maximize(T1 &a, T2 b) { if (a < b) {a = b; return true;} return false; } const int dx[] = {-1, 0, 0, +1}; const int dy[] = {0, -1, +1, 0}; const long long INF = 1e18 + 7; const int MAX = 100010; int N, S, Q, E; bool isShop[MAX]; vector<pair<int, int>> adj[MAX]; pair<int, pair<int, int>> edges[MAX]; int timer; int tin[MAX], tout[MAX]; int h[MAX], anc[17][MAX]; long long dep[MAX], down[MAX], dp[17][MAX]; void dfs(int u, int p) { // calc down[_], dp[_] tin[u] = ++timer; if (isShop[u]) { down[u] = 0; } for (pair<int, int> &x: adj[u]) if (x.fi != p) { int v = x.fi, w = x.se; dep[v] = dep[u] + w; h[v] = h[u] + 1; anc[0][v] = u; dfs(v, u); minimize(down[u], down[v] + w); } tout[u] = timer; } void reDfs(int u, int p) { for (pair<int, int> &x: adj[u]) if (x.fi != p) { int v = x.fi; minimize(dp[0][v], down[u] - dep[u]); for (int j = 1; j <= 16; ++j) { anc[j][v] = anc[j - 1][anc[j - 1][v]]; dp[j][v] = min(dp[j - 1][v], dp[j - 1][anc[j - 1][v]]); } reDfs(v, u); } } long long jump(int u, int p) { long long res = down[u] - dep[u]; int k = h[u] - h[p]; for (int j = 0; (1 << j) <= k; ++j) { if (k >> j & 1) { res = min(res, dp[j][u]); u = anc[j][u]; } } return res; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); if (fopen(task".inp", "r")) { freopen(task".inp", "r", stdin); freopen(task".out", "w", stdout); } cin >> N >> S >> Q >> E; for (int i = 1; i < N; ++i) { int u, v, w; cin >> u >> v >> w; adj[u].push_back({v, w}); adj[v].push_back({u, w}); edges[i] = {w, {u, v}}; } for (int i = 0; i < S; ++i) { int x; cin >> x; isShop[x] = true; } for (int i = 1; i <= N; ++i) { down[i] = INF; for (int j = 0; j <= 16; ++j) { dp[j][i] = INF; } } dfs(E, -1); reDfs(E, -1); while (Q--) { int i, R; cin >> i >> R; int u = edges[i].se.fi, v = edges[i].se.se; if (h[u] > h[v]) swap(u, v); // v is child of u if (tin[v] <= tin[R] && tout[R] <= tout[v]) { long long res = jump(R, v) + dep[R]; if (res >= 1e16) cout << "oo\n"; else cout << res << '\n'; } else cout << "escaped\n"; } // cerr << "\nTime elapsed: " << 1000.0 * clock() / CLOCKS_PER_SEC << " ms.\n"; return 0; }

컴파일 시 표준 에러 (stderr) 메시지

valley.cpp: In function 'int main()':
valley.cpp:73:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   73 |   freopen(task".inp", "r", stdin);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
valley.cpp:74:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   74 |   freopen(task".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...