题目:有N种物品和一个容量为V的背包。第i种物品最多有num[i]件可用,每件费用是c[i],价值是w[i]。求解将哪些物品装
入背包可使这些物品的费用总和不超过背包容量,且价值总和最大。
分析:状态转移为:
题目:http://acm.hdu.edu.cn/showproblem.php?pid=2191
#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std;
const int N = 1005;
int dp[N];
int c[N],w[N],num[N];
int n,m;
void ZeroOne_Pack(int cost,int weight,int n)
{
for(int i=n; i>=cost; i--)
dp[i] = max(dp[i],dp[i-cost] + weight);
}
void Complete_Pack(int cost,int weight,int n)
{
for(int i=cost; i<=n; i++)
dp[i] = max(dp[i],dp[i-cost] + weight);
}
int Multi_Pack(int c[],int w[],int num[],int n,int m)
{
memset(dp,0,sizeof(dp));
for(int i=1; i<=n; i++)
{
if(num[i]*c[i] > m)
Complete_Pack(c[i],w[i],m);
else
{
int k = 1;
while(k < num[i])
{
ZeroOne_Pack(k*c[i],k*w[i],m);
num[i] -= k;
k <<= 1;
}
ZeroOne_Pack(num[i]*c[i],num[i]*w[i],m);
}
}
return dp[m];
}
int main()
{
int t;
cin>>t;
while(t--)
{
cin>>m>>n;
for(int i=1; i<=n; i++)
cin>>c[i]>>w[i]>>num[i];
cout<<Multi_Pack(c,w,num,n,m)<<endl;
}
return 0;
}