Actually, it's even easier to mix backpacks.
There are n kinds of goods, the capacity of the backpack is V, next gives the weight of each item w[i], the value of v[i], the number of c[i], if C[i] is 0, indicating that this article has no number of pieces, to maximize the value
Obviously, the DP equation is divided into two types
If it's a full backpack, it's
Dp[j]=max (Dp[j],dp[j-w[i]]+v[i]) (j=w[i];j<=v;j++)
If it's a 01 or a multi-pack,
Dp[j]=max (Dp[j],dp[j-k*w[i]]+k*v[i]) (j-k*w[i]>=0 && 0<=k<=c[i])
Obvious
The code is as follows
#include <cstdio>#include<cstdlib>#include<algorithm>#include<iostream>#include<cstring>using namespacestd;intn,v,dp[10010],v[10005],w[10005],c[10005];intMain () {scanf ("%d%d",&v,&N); for(intI=1; i<=n;i++) {scanf (" %d%d%d",&w[i],&v[i],&C[i]); } for(intI=1; i<=n;i++) { if(c[i]==0) { for(intj=w[i];j<=v;j++) {Dp[j]=max (dp[j],dp[j-w[i]]+V[i]); } } Else { for(intj=v;j>=w[i];j--) { for(intk=0; K<=c[i] && j-k*w[i]>=0; k++) {Dp[j]=max (dp[j],dp[j-k*w[i]]+k*V[i]); } }}} printf ("%d\n", Dp[v]);}
Mixed backpack problem