Submission #1135246

#TimeUsernameProblemLanguageResultExecution timeMemory
1135246mmdrzadaValley (BOI19_valley)C++20
36 / 100
13 ms492 KiB
// Alphine walley


#include <bits/stdc++.h>

using namespace std;

#define int long long

#define vi vector<int>
#define REP(i, k) for(int i = 0 ; i < k ; i ++)
#define pb push_back
#define pii pair<int, int>
#define ll long long

const int N = 1e3+10;
int n, s, q, e;
vector<pii> adj[N];
bool shop[N];
array<int, 3> E[N];
int dis[N];

int32_t main() {
    cin >> n >> s >> q >> e; e--;

    REP(i, n-1) {
        int a, b, w; cin >> a >> b >> w; a--, b--;
        E[i] = {a, b, w};
        adj[a].pb({b, w}), adj[b].pb({a, w});
    }

    REP(i, s) {
        int c; cin >> c; c--;
        shop[c] = true;
    }

    while(q--) {
        int a, b; cin >> a >> b; a--; b--;
        
        queue<int> q;
        q.push(b);
        fill(dis, dis+n, 1e18);
        dis[b] = 0;
        bool escape = false;
        int ans = 1e18;
        while(!q.empty()) {
            int u = q.front();
            q.pop();
            if (u == e) {
                escape = true;
                break;
            }
            if (shop[u]) ans = min(ans, dis[u]);
            for(auto [neigh, w]: adj[u]) {
                if ((u == E[a][0] && neigh == E[a][1]) || (u == E[a][1] && neigh == E[a][0])) continue;
                if (dis[u] + w < dis[neigh]) {
                    dis[neigh] = dis[u] + w;
                    q.push(neigh);
                }
            }
        }
        if (escape) cout << "escaped\n";
        else if (ans == 1e18) cout << "oo\n";
        else cout << ans << '\n';
    }

    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...