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 ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define ff first
#define ss second
#define pb push_back
#define SZ(x) ((int)(x).size())
#define all(x) x.begin(), x.end()
#define debug(x) cout << #x << ": " << x << " "
#define nl cout << "\n"
#define rep(i, a, b) for(int i = (a); i < (b); i++)
#define per(i, a, b) for(int i = (a); i >= (b); i--)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
struct Edge{
int u, w, i;
};
const int MAXN = 2e5+100;
int N, S, Q, E, shop[MAXN];
pii edge[MAXN];
vector<Edge> g[MAXN];
void dijkstra(int s, int ind){
vector<ll> d(N, LLONG_MAX);
d[s] = 0;
priority_queue<pll, vector<pll>, greater<pll>> pq;
pq.push({d[s], s});
while(!pq.empty()){
auto [dist, v] = pq.top();
pq.pop();
if(dist > d[v]) continue;
for(auto [u, w, i] : g[v]){
if(i == ind) continue;
if(d[u] > d[v] + w){
d[u] = d[v] + w;
pq.push({d[u], u});
}
}
}
if(d[E] != LLONG_MAX){
cout << "escaped\n";
} else{
ll ans = LLONG_MAX;
rep(i, 0, S){
ans = min(ans, d[shop[i]]);
}
if(ans != LLONG_MAX){
cout << ans << "\n";
} else{
cout << "oo\n";
}
}
}
int timer, tin[MAXN], tou[MAXN];
void dfs(int v, int p){
tin[v] = timer++;
for(auto [u, w, i] : g[v]){
if(u != p){
dfs(u, v);
}
}
tou[v] = timer - 1;
}
bool is_ancestor(int u, int v){
return (tin[u] <= tin[v] && tou[v] <= tou[u]);
}
void solve(){
cin >> N >> S >> Q >> E;
--E;
rep(i, 0, N - 1){
int u, v, w;
cin >> u >> v >> w;
--u, --v;
g[u].pb({v, w, i});
g[v].pb({u, w, i});
edge[i] = {u, v};
}
rep(i, 0, S){
cin >> shop[i];
--shop[i];
}
if(S == N){
dfs(E, -1);
rep(q, 0, Q){
int i, x;
cin >> i >> x;
--i, --x;
auto [u, v] = edge[i];
if(is_ancestor(u, x) && is_ancestor(v, x)){
cout << "0\n";
} else{
cout << "escaped\n";
}
}
return;
}
rep(q, 0, Q){
int i, v;
cin >> i >> v;
--i, --v;
dijkstra(v, i);
}
}
int main(){
ios_base::sync_with_stdio(false), cin.tie(nullptr);
solve();
return 0;
}
Compilation message (stderr)
valley.cpp: In function 'void dijkstra(int, int)':
valley.cpp:31:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
31 | auto [dist, v] = pq.top();
| ^
valley.cpp:34:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
34 | for(auto [u, w, i] : g[v]){
| ^
valley.cpp: In function 'void dfs(int, int)':
valley.cpp:60:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
60 | for(auto [u, w, i] : g[v]){
| ^
valley.cpp: In function 'void solve()':
valley.cpp:92:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
92 | auto [u, v] = edge[i];
| ^
# | 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... |