![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#define MAX 10005
#define max(a,b) (a>b?a:b)
int W;
int dp[MAX];
void ZoreOnePack(int cost , int weight){
//0-1背包
for (int i = W ; i >= weight ; -- i)
dp[i] = max(dp[i],dp[i-weight]+cost) ;
}
void CompletePack(int cost , int weight){
//完全背包
for (int i = weight ; i <= W ; ++ i)
dp[i] = max(dp[i],dp[i-weight]+cost) ;
}
void MultiPack(int cost , int weight , int num){
//多重背包
if (num*weight>=W)
CompletePack(cost,weight) ;
else {
int k = 1 ;
while (k<num) {
ZoreOnePack(cost*k,weight*k) ;
num -= k ; k += k ;
}
ZoreOnePack(cost*num,weight*num) ;
}
}
网上抄来的一份代码,还是相当不错的!