Question link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 2191
There are capital N and M types of rice, for the I type of rice, the price, quantity, number of bags are: PI, hi, CI, ask the maximum weight of rice that can be purchased.
Start with multiple backpacks ~~~~~~
In the 01 backpack, there is a heavy loop between the two duplicates. 0 ~ Bag [I]: indicates the number of items in Type I ranges from 0 ~ Bag [I] enumeration.
1 # include <iostream> 2 # include <cstring> 3 # include <cstdio> 4 using namespace STD; 5 6 const int maxn = 200 + 5; 7 8 int W [maxn], V [maxn], num [maxn]; 9 int DP [maxn]; 10 11 int main () 12 {13 int T, money, kind; 14 While (scanf ("% d", & T )! = EOF) 15 {16 while (t --) 17 {18 scanf ("% d", & money, & kind); 19 for (INT I = 0; I <kind; I ++) 20 scanf ("% d", & V [I], & W [I], & num [I]); 21 memset (DP, 0, sizeof (DP); 22 for (INT I = 0; I <kind; I ++) 23 {24 for (Int J = 0; j <num [I]; j ++) // This loop is added !!! 25 {26 for (int K = money; k> = V [I]; k --) 27 DP [k] = max (DP [K], DP [k-V [I] + W [I]); 28} 29} 30 printf ("% d \ n", DP [money]); 31} 32} 33 return 0; 34}