# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1087104 | PieArmy | Valley (BOI19_valley) | C++17 | 170 ms | 52308 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
typedef long long ll;
ll pie(ll army){return (1ll<<army);}
#include <bits/stdc++.h>
#define fr first
#define sc second
#define pb push_back
#define endl '\n'
#define mid ((left+right)>>1)
const ll inf=2000000000000000005;
const int sonsuz=2000000005;
using namespace std;
ll fpow(ll x,ll y,ll m=0){if(y<0){cout<<"powError";return -1;}if(m)x%=m;ll res=1;while(y>0){if(y&1)res*=x;x*=x;if(m){x%=m;res%=m;}y>>=1;}return res;}
int n,s,q,root;
int dep[100001],par[100001][17];
ll dis[100001][17],res[100001][17];
ll bes[100001];
int shop[100001];
vector<pair<int,pair<int,int>>>komsu[100001];
int subroot[100001];
void dfs(int pos){
for(int i=1;i<17;i++){
par[pos][i]=par[par[pos][i-1]][i-1];
dis[pos][i]=dis[pos][i-1]+dis[par[pos][i-1]][i-1];
}
if(shop[pos])bes[pos]=0;
for(pair<int,pair<int,int>>x:komsu[pos]){
if(x.fr==par[pos][0]){
subroot[x.sc.sc]=pos;
continue;
}
par[x.fr][0]=pos;
dis[x.fr][0]=x.sc.fr;
dep[x.fr]=dep[pos]+1;
dfs(x.fr);
bes[pos]=min(bes[pos],bes[x.fr]+x.sc.fr);
}
}
void code(){
cin>>n>>s>>q>>root;
for(int i=1;i<=n;i++){
bes[i]=inf;
}
for(int i=1;i<n;i++){
int x,y,w;
cin>>x>>y>>w;
komsu[x].pb({y,{w,i}});
komsu[y].pb({x,{w,i}});
}
par[root][0]=root;
for(int i=1;i<=s;i++){
int x;cin>>x;
shop[x]=1;
}
dfs(root);
for(int i=1;i<=n;i++){
res[i][0]=min(bes[i],bes[par[i][0]]+dis[i][0]);
}
for(int i=1;i<17;i++){
for(int j=1;j<=n;j++){
res[j][i]=min(res[j][i-1],res[par[j][i-1]][i-1]+dis[j][i-1]);
}
}
while(q--){
int a,b;cin>>b>>a;
b=subroot[b];
ll ans=bes[a];
if(dep[b]>dep[a]){
cout<<"escaped\n";
continue;
}
ll sum=0;
for(int i=0;i<17;i++){
if((dep[a]-dep[b])&pie(i)){
ans=min(ans,res[a][i]+sum);
sum+=dis[a][i];
a=par[a][i];
}
}
if(a!=b){
cout<<"escaped\n";
continue;
}
if(ans==inf){
cout<<"oo\n";
continue;
}
cout<<ans<<endl;
}
}
int main(){
ios_base::sync_with_stdio(false);cin.tie(NULL);
bool usaco=0;if(usaco){freopen(".in","r",stdin);freopen(".out","w",stdout);}
int t=1;
if(!t)cin>>t;
while(t--){code();cout<<endl;}
return 0;
}
Compilation message (stderr)
# | 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... |