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;
#define all(x) begin(x),end(x)
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { string sep; for (const T &x : v) os << sep << x, sep = " "; return os; }
#define debug(a) cerr << "(" << #a << ": " << a << ")\n";
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pi;
const int mxN = 1e1+1;
const ll oo = 1e17;
ll dist[mxN]={}, dp[18][mxN]={};
int jump[18][mxN], d[mxN];
int go(int at, int delta) {
for(int i=0;1<<i <= delta;++i) {
if(1<<i & delta) {
at = jump[i][at];
}
}
return at;
}
ll solve(int at, int delta) {
delta++;
ll res = oo;
int to = at;
for(int i=0;1<<i <= delta;++i) {
if(1<<i & delta) {
res = min(res,dp[i][to]);
to = jump[i][to];
}
}
return dist[at]+res;
}
vector<pi> adj[mxN];
bitset<mxN> shop;
void dfs(int at, int from) {
if(!shop[at]) {
dp[0][at] = oo;
} else dp[0][at] = 0;
for(auto [to,w]: adj[at]) if(to!=from) {
dist[to] = dist[at]+w;
d[to] = d[at]+1;
jump[0][to] = at;
dfs(to,at);
dp[0][at] = min(dp[0][at],w+dp[0][to]);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n,k,q,root; cin >> n >> k >> q >> root; root--;
struct edge{
int a,b,w;
}; vector<edge> edges(n-1);
for(auto& e : edges) {
cin >> e.a >> e.b >> e.w;
e.a--;e.b--;
adj[e.a].emplace_back(e.b,e.w);
adj[e.b].emplace_back(e.a,e.w);
}
while(k--) {
int s; cin >> s;--s;
shop[s] = true;
}
jump[0][root] = root;
dfs(root,-1);
for(int i=0;i<n;++i) dp[0][i]-=dist[i];
for(int i=1;(1<<i)<n;++i) {
for(int j=0;j<n;++j) {
int to = jump[i-1][j];
jump[i][j] = jump[i-1][to];
dp[i][j] = min(dp[i-1][j], dp[i-1][to]);
}
}
while(q--) {
int eid,at; cin >> eid >> at; --eid,--at;
auto& e = edges[eid];
if(d[e.a]<d[e.b]) swap(e.a,e.b);
int delta = d[at]-d[e.a];
int from = go(at,delta);
if(from!=e.a) {
cout << "escaped\n";
continue;
}
auto tmp = solve(at,delta);
if(tmp>=oo/2) cout << "oo\n";
else cout << tmp << '\n';
}
}
# | 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... |