DP big combat-combination backpack and dp big combat backpack
Description
Combination backpack: Some items can be taken only once (01 backpack), some items can be taken unlimited times (full backpack), and some items can be taken up to a maximum of times (multiple backpacks ).
DD Daniel's pseudocode for I = 1 to N if item I belongs to 01 backpack ZeroOnePack (F, Ci, Wi) else if item I belongs to complete backpack CompletePack (F, ci, Wi) else if the I-th item belongs to MultiplePack (F, Ci, Wi, Ni)
Input
The first number is the number of data groups n 1 <= n <= 10
The next n groups of test data, each of which consists of two parts.
First behavior: backpack capacity V, number of item types N. 1 <= V <= 200, 1 <= N <=
In the next N rows, each row contains three values: item value v, item weight w, and item quantity M. M = 233 indicates that the item is infinite.
1 <= v, w <= 200, 1 <= M <= 25
Output
For each group of data, output a row, the maximum value of the items that the backpack can accommodate
Input example
110 32 2 2333 2 14 3 3
Output example
13
Source of http://biancheng.love/contest/10/problem/F/index
Solution:
Combination backpack: 0-1 backpack, full backpack, multiple backpacks.
Therefore, we need to combine the solution of the above three backpack problems to solve the backpack.
0-1 backpack code:
1 void Zeronepack(int w,int v)2 {3 for(int i=V; i>=w; i--)4 if(dp[i]<dp[i-w]+v)5 dp[i]=dp[i-w]+v;6 }
Full backpack code:
1 void Compack(int w,int v)2 {3 for(int i=w; i<=V; i++)4 if(dp[i]<dp[i-w]+v)5 dp[i]=dp[i-w]+v;6 }
Code for multiple backpacks:
1 void Multipack(int w,int v,int num) 2 { 3 int k; 4 if(w*num>=V) 5 { 6 Compack(w,v); 7 return; 8 } 9 for(k=1; k<num; k<<1)10 {11 Zeronepack(k*w,k*v);12 num-=k;13 }14 Zeronepack(num*w,num*v);15 }
Code for combining this question with a backpack:
1 #include <bits/stdc++.h> 2 int dp[30005]; 3 int V,N; 4 5 void Compack(int w,int v) 6 { 7 for(int i=w; i<=V; i++) 8 if(dp[i]<dp[i-w]+v) 9 dp[i]=dp[i-w]+v;10 }11 12 void Zeronepack(int w,int v)13 {14 for(int i=V; i>=w; i--)15 if(dp[i]<dp[i-w]+v)16 dp[i]=dp[i-w]+v;17 }18 19 void Multipack(int w,int v,int num)20 {21 int k;22 if(w*num>=V)23 {24 Compack(w,v);25 return;26 }27 for(k=1; k<num; k<<1)28 {29 Zeronepack(k*w,k*v);30 num-=k;31 }32 Zeronepack(num*w,num*v);33 }34 35 int main()36 {37 int kase,v,w,m;38 scanf("%d",&kase);39 while(kase--)40 {41 memset(dp,0,sizeof(dp));42 scanf("%d%d",&V,&N);43 for(int i=1;i<=N;i++)44 {45 scanf("%d%d%d",&v,&w,&m);46 if(m==1)47 Zeronepack(w,v);48 else if(m==233)49 Compack(w,v);50 else51 Multipack(w,v,m);52 }53 printf("%d\n",dp[V]);54 }55 return 0;56 }