#include <iostream>
#include <vector>
#define pb push_back
using namespace std;
const int N = 1e3 + 5;
int n, s, q, e;
int weight[N][N], dist[N];
pair<int, int> edge[N];
vector<int> g[N], shops;
bool is_esc[N];
void escape(int v, int p = -1) {
is_esc[v] = 1;
for (int to : g[v]) {
if (to != p) {
escape(to, v);
}
}
}
void calcDist(int v, int p = -1) {
dist[v] = 0;
for (int to : g[v]) {
if (to == p) continue;
dist[to] = min(dist[to], dist[v] + weight[v][to]);
calcDist(to, v);
}
}
void solve() {
cin >> n >> s >> q >> e;
for (int i = 0; i < n - 1; i++) {
int a, b, c;
cin >> a >> b >> c;
weight[a][b] = weight[b][a] = c;
g[a].pb(b);
g[b].pb(a);
edge[i] = {a, b};
}
while (s--) {
int c;
cin >> c;
shops.pb(c);
}
while (q--) {
int i, r;
cin >> i >> r;
escape(e);
if (is_esc[r]) {
cout << "escaped\n";
continue;
}
for (int ss : shops) {
calcDist(ss);
}
cout << dist[r] << '\n';
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
solve();
return 0;
}
| # | 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... |