This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int long long
#define dbg(x) cerr << #x << ": " << x << endl;
const int MAX_N = 1e5 + 5;
const ll INF = 1e17;
vector<tuple<int, ll, int>> g[MAX_N];
bool special[MAX_N];
ll dp[MAX_N];
bool used[MAX_N];
int t;
bool found = false;
void dfs(int v, int p, int edge) {
if (special[v]) {
dp[v] = 0;
} else {
dp[v] = INF;
}
if (v == t) {
found = true;
}
for (const auto& [u, w, idx] : g[v]) {
if (idx == edge) continue;
if (u == p) continue;
dfs(u, v, edge);
dp[v] = min(dp[v], dp[u] + w);
}
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n, s, q;
cin >> n >> s >> q >> t;
--t;
for (int i = 0; i < n - 1; ++i) {
int u, v;
cin >> u >> v;
--u, --v;
int w;
cin >> w;
g[u].emplace_back(v, w, i);
g[v].emplace_back(u, w, i);
}
for (int i = 0; i < s; ++i) {
int v;
cin >> v;
--v;
special[v] = true;
}
while (q--) {
int edge, r;
cin >> edge >> r;
--edge, --r;
found = false;
dfs(r, -1, edge);
if (found) {
cout << "escaped\n";
continue;
}
if (dp[r] == INF) {
cout << "oo\n";
} else {
cout << dp[r] << '\n';
}
}
return 0;
}
/*
5 2 3 4
1 2 1
2 3 1
3 4 1
4 5 1
1 5
4 4
2 4
2 3
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |