This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//#include "roads.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define x first
#define y second
ll n;
vector<vector<pair<ll,ll>>> cad;
ll k;
vector<vector<ll>> cache;
vector<vector<ll>> cache2;
ll sol(ll node,ll B, ll P)
{
if(B>k) return 1e14;
if(cache2[node][B]!=-1) return cache2[node][B];
if(cache[node][B]!=-1) return cache[node][B];
vector<pair<ll,ll>> asd;
for(auto i:cad[node])
{
if(i.x==P) continue;
asd.push_back({sol(i.x,1,node),i.y+sol(i.x,0,node)});
}
if(asd.size()==0) return cache[node][B]=0;
vector<ll> dp(min((ll)asd.size()+1,k-B+1),1e14);
vector<ll> dp2=dp;
dp[0]=0;
for(int i=0; i<asd.size(); i++)
{
dp2.assign(dp.size(),1e14);
for(int j=dp.size()-1; j>-1; j--)
{
dp2[j]=dp[j]+asd[i].y;
if(j>0) dp2[j]=min(dp[j-1]+asd[i].x,dp2[j]);
}
for(int j=1; j<dp.size(); j++)
dp2[j]=min(dp2[j],dp2[j-1]);
swap(dp,dp2);
}
if(k-B>=dp.size())
{
cache2[node][B]=dp.back();
return cache[node][B]=dp.back();
}
return cache[node][B]=dp[k-B];
}
std::vector<long long> minimum_closure_costs(int N, std::vector<int> U,std::vector<int> V,std::vector<int> W) {
n=N;
cad.resize(n+1);
for(int i=0; i<n-1; i++)
{
cad[U[i]].push_back({V[i],W[i]});
cad[V[i]].push_back({U[i],W[i]});
}
vector<ll> s(n,0);
cache2.assign(n,{-1,-1});
for(int i=0; i<n; i++)
{
k=i;
cache.assign(n,{-1,-1});
s[i]=sol(0,0,-1);
if(s[i]==0) break;
}
return s;
}
Compilation message (stderr)
roads.cpp: In function 'll sol(ll, ll, ll)':
roads.cpp:27:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
27 | for(int i=0; i<asd.size(); i++)
| ~^~~~~~~~~~~
roads.cpp:35:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
35 | for(int j=1; j<dp.size(); j++)
| ~^~~~~~~~~~
roads.cpp:39:11: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
39 | if(k-B>=dp.size())
| ~~~^~~~~~~~~~~
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |