# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
1087104 | PieArmy | Valley (BOI19_valley) | C++17 | 170 ms | 52308 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
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;
}
컴파일 시 표준 에러 (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... |