#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
struct edge{
ll v;
ll w;
};
bool operator<(edge a, edge b){return a.w > b.w;}
bool operator==(edge a, edge b){return make_pair(a.v,a.w) == make_pair(b.v,b.w);};
typedef vector<edge> ve;
typedef vector<ve> vve;
#define MAX 1e17
int main(){
ll N,S,Q,E;
cin >> N >> S >> Q >> E;
E--;
vi A(N-1),B=A,Sh(S),IS(N,0),I(Q),R(Q);
vl W(N-1);
vl W2(N-1);
for(ll i = 0;i < N-1;i++){
cin >> A[i] >> B[i] >> W[i];
A[i]--,B[i]--;
W2[min(A[i],B[i])] = W[i];
}
for(ll i = 0;i < S;i++){
cin >> Sh[i];
IS[--Sh[i]] = 1;
}
for(ll i = 0;i < Q;i++){
cin >> I[i] >> R[i];
I[i]--,R[i]--;
}
vl DL(N,MAX),DR=DL;
vl DLS(N,MAX),DRS=DLS;
for(int i = 0;i < N;i++){
if(IS[i]) DL[i] = 0, DLS[i] = 0;
else if(i != 0 && DL[i-1]+W[i-1] < DL[i]) DL[i] = DL[i-1]+W[i-1], DLS[i] = DLS[i-1]+1;
}
for(int i = N-1;i >= 0;i--){
if(IS[i]) DR[i] = 0, DRS[i] = 0;
else if(i != N-1 && DR[i+1]+W[i] < DR[i]) DR[i] = DR[i+1]+W[i], DRS[i] = DRS[i+1]+1;
}
for(int q = 0;q < Q;q++){
ll sep = min(A[I[q]],B[I[q]]);
if(R[q] <= sep == E <= sep){
cout << "escaped\n";
continue;
}
ll ans = MAX;
if(DRS[R[q]] == 0) ans = 0;
else{
if(clamp(sep,R[q],R[q]+DRS[R[q]]-1) != sep) ans = min(ans,DR[R[q]]);
if(clamp(sep,R[q]-DLS[R[q]],R[q]-1) != sep) ans = min(ans,DL[R[q]]);
}
if(ans == MAX){
cout << "oo\n";
continue;
}
cout << ans << '\n';
}
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... |