이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
long long n, q, d[1001][1001], inf=1e18+1, x[1001], y[1001], z[1001], f[1001], r, k, s, e;
vector <pair<long long, long long>>ke[1001];
void dij(int u, int f1, int f2){
priority_queue<pair<long long, long long>, vector<pair<long long, long long>>, greater<pair<long long, long long>>> qu;
d[u][u]=0;
qu.push({d[u][u], u});
while(!qu.empty()){
int t=qu.top().second, g=qu.top().first;
qu.pop();
if(g>d[u][t])continue;
for(pair<long long, long long> v : ke[t]){
if((t==f1 and v.first==f2)||(t==f2 and v.first==f1))continue;
if(d[u][v.first]>d[u][t]+v.second){
d[u][v.first]=d[u][t]+v.second;
qu.push({d[u][v.first], v.first});
}
}
}
}
int main(){
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
//freopen("valley.inp", "r", stdin);
//freopen("valley.out", "w", stdout);
cin>>n>>s>>q>>e;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
d[i][j]=inf;
}
}
for(int i=1;i<n;i++){
cin>>x[i]>>y[i]>>z[i];
ke[x[i]].push_back({y[i], z[i]});
ke[y[i]].push_back({x[i], z[i]});
}
for(int i=1;i<=s;i++)cin>>f[i];
for(int i=1;i<=q;i++){
cin>>k>>r;
dij(r, x[k], y[k]);
if(d[r][e]!=inf)cout<<"escaped\n";
else {
long long ans=inf;
for(int i=1;i<=s;i++){
ans=min(ans, d[r][f[i]]);
}
if(ans!=inf)cout<<ans<<"\n";
else cout<<"oo\n";
}
for(int i=1;i<=n;i++)d[r][i]=inf;
}
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... |