#include<stdio.h>
#include<string.h>
struct node
{
int value,number;
}a[110];
#define maxx 100010
int f[maxx];
/*
procedure MultiplePack(cost,weight,amount)
if cost*amount>=V
CompletePack(cost,weight)
return
integer k=1
while k<amount
ZeroOnePack(k*cost,k*weight)
amount=amount-k
k=k*2
ZeroOnePack(amount*cost,amount*weight)
*/
int max(int x,int y)
{
if(x>y)
return x;
return y;
}
void Complete(int cost,int weight,int m)
{
for(int i=cost;i<=m;i++)
f[i]=max(f[i],f[i-weight]+cost);
}
void Zero_One(int cost,int weight,int m)
{
for(int i=m;i>=cost;i--)
f[i]=max(f[i],f[i-weight]+cost);
}
void MultiplePack(int cost,int weight,int m,int number)
{
// int t=a[i].value*a[i].number;
if(weight*number>m)
Complete(cost,weight,m);
else
{
int k=1;
while(k<number)
{
Zero_One(k*cost,k*cost,m);
number-=k;
k<<=1;
}
Zero_One(number*cost,number*cost,m);
}
}
int main()
{
int n,m,count,i,j,k,t;
while(scanf("%d%d",&n,&m)!=EOF)
{
if(n==0 && m==0)break;
for(i=0;i<n;i++)
scanf("%d",&a[i].value);
for(i=0;i<n;i++)
scanf("%d",&a[i].number);
memset(f,0,sizeof(f));
for(i=0;i<n;i++)
{
MultiplePack(a[i].value,a[i].value,m,a[i].number);
}
int ans=0;
for(int i=1;i<=m;i++)
{
if(f[i]==i)
ans++;
}
printf("%d\n",ans);
}
}
背包模版 自己用(hdu2844)
最新推荐文章于 2021-03-09 15:37:20 发布
本文介绍了解决多重背包问题的一种高效算法实现,通过完全背包和0-1背包的组合使用来解决物品数量有限制的情况,并提供了详细的C语言代码实现。
623

被折叠的 条评论
为什么被折叠?



