http://acm.hdu.edu.cn/showproblem.php?pid=2844
多重背包问题,在网上搜了很多这道题的解题报告,发现最后面都是判断 if(dp[i]==i)就ans++; 主要是因为这里的cost 和weight 是相等的。
#include <iostream>
using namespace std;
const int MAX = 100000 + 10;
int f[MAX];
int v;
int c[MAX];
int a[MAX];
void ZeroOnePack(int cost,int weight)
{
int i;
for(i=v;i>=cost;i--)
{
f[i] = max(f[i],f[i-cost]+weight);
}
}
void CompletePack(int cost,int weight)
{
int i;
for(i=cost;i<=v;i++)
{
f[i] = max(f[i],f[i-cost]+weight);
}
}
void MultiplePack(int cost,int weight,int amount)
{
if(v <= cost*amount)
{
CompletePack(cost,weight);
return;
}
else
{
int k = 1;
while(k<amount)
{
ZeroOnePack(k*cost,k*weight);
amount = amount - k;
k = k*2;
}
ZeroOnePack(amount*cost,amount*weight);
}
}
int main()
{
int n;
while(cin>>n>>v,n+v)
{
memset(f,0,sizeof(f));
int i;
for(i=1;i<=n;i++)
{
cin>>c[i];
}
for(i=1;i<=n;i++)
{
cin>>a[i];
}
for(i=1;i<=n;i++)
{
MultiplePack(c[i],c[i],a[i]);
}
int sum = 0;
for(i=1;i<=v;i++)
{
if(f[i]==i)
{
sum++;
}
}
cout<<sum<<endl;
}
return 0;
}