算法源于《背包九讲》
下面是实现代码:
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<sstream>
#include<vector>
#include<map>
#include<stack>
#include<list>
#include<set>
#include<queue>
#define LL long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1 | 1
using namespace std;
const int maxn=100005;
int w[maxn],v[maxn],num[maxn];//代价数组和价值数组,以及相应的物品最大件数
int W,n;//背包容量和物品件数
int dp[maxn];//背包更新数组
void ZeroOnePack(int weight,int value)//0-1背包
{
for(int i=W;i>=weight;i--)
dp[i]=max(dp[i],dp[i-weight]+value);
}
void CompletePack(int weight,int value)//完全背包
{
for(int i=weight;i<=W;i++)
dp[i]=max(dp[i],dp[i-weight]+value);
}
void MultiplePack(int weight,int value,int cnt)//多重背包
{
if(weight*cnt>=W) CompletePack(weight,value);
else
{
for(int k=1;k<cnt;k<<=1)
ZeroOnePack(k*weight,k*value),cnt-=k;
ZeroOnePack(cnt*weight,cnt*value);
}
}
int main()
{
//读入数据
return 0;
}