제출 #411609

#제출 시각아이디문제언어결과실행 시간메모리
411609A_D경주 (Race) (IOI11_race)C++14
9 / 100
2007 ms212292 KiB
#include "race.h" #include <bits/stdc++.h> #define LL long long #define ii pair<LL,LL> #define F first #define S second using namespace std; const LL NN=2e5+100; vector<ii> g[NN]; LL dp[NN][111]; LL n,k; int ans=1e9; multiset<int> st; void dfs(LL u,LL p) { dp[u][0]=0; for(auto x:g[u]){ if(x.F==p)continue; dfs(x.F,u); } for(auto x:g[u]){ if(x.F==p)continue; for(LL i=0;i<=k;i++){ if(i-x.S>=0)dp[u][i]=min(dp[x.F][i-x.S]+1,dp[u][i]); } } if((g[u].size()==2&&u!=p)||g[u].size()==1)return; for(LL i=0;i<=k;i++){ st.clear(); for(auto x:g[u]){ if(x.F==p)continue; if(i-x.S>=0)st.insert(dp[x.F][i-x.S]); } for(auto x:g[u]){ if(x.F==p)continue; if(i-x.S>=0)st.erase(st.find(dp[x.F][i-x.S])); LL j=(k-i)-x.S; if(j>=0){ LL ret=*st.begin(); ret+=dp[x.F][j]; ans=min(ans,(int)ret+2); } if(i-x.S>=0)st.insert(dp[x.F][i-x.S]); } } } int best_path(int N, int K, int H[][2], int L[]) { ans=1e9; n=N; k=K; for(int i=0;i<n;i++){ for(int j=0;j<=k;j++){ dp[i][j]=1e9; } } for(int i=0;i<n-1;i++){ int u=H[i][0]; int v=H[i][1]; int w=L[i]; g[u].push_back({v,w}); g[v].push_back({u,w}); } dfs(0,0); for(int i=0;i<n;i++){ ans=min(ans,(int)dp[i][k]); } if(ans==1e9)ans=-1; return ans; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...