01背包,完全背包,多重背包 ,模板代码
01 背包
void bag01(int cost,int weight) { for(i=v;i>=cost;i--) if(dp[i]<dp[i-cost]+weight) dp[i]=dp[i-cost]+weight; }
完全背包
void complete(int cost,int weight) { for(i=cost;i<=v;i++) if(dp[i]<dp[i-cost]+weight) dp[i]=dp[i-cost]+weight; }
多重背包
void multiply(int cost,int weight,int amount) { if(cost*amount>=v) complete(cost,weight); else{ k=1; while(k<amount){ bag01(k*cost,k*weight); amount-=k; k+=k; } bag01(cost*amount,weight*amount); } }