https://i0.wp.com/eblogarithm.com/wp-content/uploads/2017/08/Codeforces-Round-419-Div-1-problem-C-Karen-and-Supermarket-Solution-In-CC1.png?fit=640%2C360

Codeforces Round #419 (Div. 1), problem: (C) Karen and Supermarket Solution In C/C++

#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;
const int N=5010;
int n,m,c[N],d[N],fa[N];
int w[N],head[N],next[N];
void add(int f,int t){
static int cnt=0;
w[++cnt]=t;
next[cnt]=head[f];
head[f]=cnt;
}
int f[N][N],g[N][N],size[N];
//f[i][j],g[i][j]表示在子树i中买j件物品的最小代价
//其中g要求购买从根到i的物品
void dfs(int x){
for (int i=1;i<=n;i++) f[x][i]=g[x][i]=1e9+1;
g[x][0]=g[x][1]=c[x]-d[x];
f[x][1]=c[x];
size[x]=1;
for (int i=head[x];i;i=next[i]){
int v=w[i];
dfs(v);
for (int a=size[x];a>=0;a–)
for (int b=1;b<=size[v];b++){
f[x][a+b]=min(f[x][a+b],f[x][a]+f[v][b]);
g[x][a+b]=min(g[x][a+b],g[x][a]+min(f[v][b],g[v][b]));
}
size[x]+=size[v];
}
}
int main()
{
scanf(“%d%d”,&n,&m);
scanf(“%d%d”,&c[1],&d[1]);
for (int i=2;i<=n;i++){
scanf(“%d%d%d”,&c[i],&d[i],&fa[i]);
add(fa[i],i);
}
dfs(1);
int ans=0;
for (int i=1;i<=n;i++)
if (m>=f[1][i]||m>=g[1][i]) ans=i;
printf(“%d\n”,ans);
return 0;
}

(Visited 15 times, 1 visits today)



There are no comments

Add yours

Leave a Reply

%d bloggers like this: