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>
#define endl "\n"
#define mp make_pair
#define pb push_back
#define F first
#define S second
#define sz size()
#define rep(i,k,n) for(int i = k ; i <= n ; ++i)
#define per(i,k,n) for(int i = k ; i >= n ; --i)
#define Zymraq() ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define all(x) x.begin(),x.end()
#define fr(x) freopen(x".in","r",stdin),freopen(x".out","w",stdout)
#define toqta return 0
#define PERMUTE next_permutation
#define no cout<<"No"<<endl;
#define yes cout<<"Yes"<<endl;
#define a() a + 1, a + n + 1
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef string S;
typedef double ld;
typedef long double lld;
const int N = 1e6 + 17;
const int modd = 1e9 + 7;
const int INF = 1e9 - 19;
const int P = 37;
const ll NN = 1e7 + 17;
const ld eps = 1e-19;
const double pi = 3.141592653589793238462643383279 ;
bool sortbysec(const pair<int,int> &a, const pair<int,int> &b){
return (a.second < b.second);
}
void pre (ll a) {
cout << fixed << setprecision(a);
return;
}
ll n, s, q, e;
vector <pair <ll, ll>> road;
vector <ll> g[200];
bool closed[200][200], is[200];
ll dist[200][200], dst[200];
ll ok = 0;
void dfs (ll v, ll pr) {
if (ok == 1) return;
if (v == e) {
ok = 1;
return;
}
for (auto to: g[v]) {
if (to != pr && !closed[v][to]) {
dst[to] = dst[v] + dist[v][to];
dfs (to, v);
}
}
return;
}
int main () {
Zymraq();
cin >> n >> s >> q >> e;
for (int i = 1; i < n; ++ i) {
ll x, y, d;
cin >> x >> y >> d;
dist[y][x] = d;
dist[x][y] = dist[y][x];
road.pb (mp (x, y));
g[x].pb (y);
g[y].pb (x);
}
for (int i = 1; i <= s; ++ i) {
ll shop;
cin >> shop;
is[shop] = 1;
}
while (q --) {
ll l, r, ans = INF;
cin >> l >> r;
closed[road[l - 1].F][road[l - 1].S] = 1;
closed[road[l - 1].S][road[l - 1].F] = 1;
ok = 0;
for (int i = 1; i <= n; ++ i) {
dst[i] = 0;
}
if (is[r] == 1) ans = 0;
if (r == e) {
cout << "escaped\n";
continue;
}
dfs (r, -1);
closed[road[l - 1].F][road[l - 1].S] = 0;
closed[road[l - 1].S][road[l - 1].F] = 0;
if (ok == 1) {
cout << "escaped\n";
continue;
}
for (int i = 1; i <= n; ++ i) {
if (is[i] && dst[i] != 0) ans = min (ans, dst[i]);
}
if (ans != INF) {
cout << ans << '\n';
continue;
}
if (ans == INF && dst[e] == 0) {
cout << "oo\n";
continue;
}
}
return 0;
}
/*
3 3 2 1
1 2 1
1 3 1
1 2 3
2 2
3 1
*/
| # | 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... |