이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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 int INF=1e18;
const LL NN=2e5+100;
vector<ii> g[NN];
LL dp[NN][111];
LL n,k;
LL ans=INF;
multiset<LL> 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();
st.insert(INF);
for(auto x:g[u]){
if(x.F==p)continue;
if(i-x.S>=0){
st.insert(dp[x.F][i-x.S]);
while(st.size()>2){
st.erase(st.find(*st.rbegin()));
}
}
}
for(auto x:g[u]){
if(x.F==p)continue;
if(i-x.S>=0&&st.find(dp[x.F][i-x.S])!=st.end())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,ret+2);
}
if(i-x.S>=0){
st.insert(dp[x.F][i-x.S]);
while(st.size()>2){
st.erase(st.find(*st.rbegin()));
}
}
}
}
}
int best_path(int N, int K, int H[][2], int L[])
{
ans=INF;
n=N;
k=K;
for(LL i=0;i<n;i++){
for(LL j=0;j<=k;j++){
dp[i][j]=INF;
}
}
for(LL i=0;i<n-1;i++){
LL u=H[i][0];
LL v=H[i][1];
LL w=L[i];
g[u].push_back({v,w});
g[v].push_back({u,w});
}
dfs(0,0);
for(LL i=0;i<n;i++){
ans=min(ans,dp[i][k]);
}
if(ans==INF)ans=-1;
int ann=ans;
return ann;
}
컴파일 시 표준 에러 (stderr) 메시지
race.cpp:8:15: warning: overflow in conversion from 'double' to 'int' changes value from '1.0e+18' to '2147483647' [-Woverflow]
8 | const int INF=1e18;
| ^~~~
# | 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... |