I Love sneakers!Time
limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 4538 Accepted Submission (s): 1866
Problem descriptionafter months of hard working, Iserlohn finally wins awesome amount of scholarship. As a great zealot of sneakers, he decides to spend all the he money on them in a sneaker store.
There is several brands of sneakers that Iserlohn wants to collect, such as Air Jordan and Nike Pro. And each brand have released various products. For the reason-Iserlohn is definitely a sneaker-mania, he desires-buy at least one product for each brand.
Although the fixed price of each product have been labeled, Iserlohn sets values for each of them based on his own tendency . With handsome but limited, he wants to maximize the total value of the shoes he was going to buy. Obviously, as a collector, he won ' t buy the same product twice.
Now, Iserlohn needs-help him find the best solution of he problem, which means to maximize the total value of the Products he can buy. Inputinput contains multiple test cases. Each test case begins with three integers 1<=n<=100 representing the total number of products, 1 <= m<= 10000 The money Iserlohn gets, and 1<=k<=10 representing the sneaker brands. The following N lines each represents a product with three positive integers 1<=a<=k, B and C, 0<=b,c<100000, Meaning the brand s number it belongs, the labeled Price, and the value of this product. Process to End of File. Outputfor each test case, print a integer which is the maximum total value of the sneakers that Iserlohn purchases. Print "Impossible" if Iserlohn ' s demands can ' t be satisfied. Sample Input
5 10000 4 5 4 991 772
Sample Output
255
Test instructions
Iserlohn has M yuan, now has n pairs of shoes, shoes have K brand, each pair of shoes have three parameters brand A, price B, there is a value of C, asked whether Iserlohn can be each brand of shoes to buy at least a pair, if not, output "impossible", Can output can get the maximum value of shoes and.
Analysis:
First look at what is a group backpack.
There are n items and a backpack with a capacity of V. The cost of article I is CI, the value is WI. These items are divided into K-groups, each of which conflicts with each other, with a maximum of one item selected. The solution of which items are loaded into the backpack allows the sum of the costs of these items to be no more than the backpack capacity and the maximum value.
We note that the group backpack is only a maximum of one item per set, which requires that each group be chosen at least one, but, to be said, the idea is roughly similar.
Set Dp[k][v] Indicates the sum of the maximum value that can be taken in the case of a K-group before I use the amount of money as v. This problem not only requires the maximum value sum, but also to determine the feasibility, then we use-inf or-one to initialize the DP array
The state transition equation is:
Dp[k][v] = max { dp[k][v], max{ dp[k][v-shoes[k][i].price], Dp[k-1][v-shoes[k][i].price] } + Shoes[k][i].val UE | Item I ∈ Group K}
This problem has the solution of the rolling array, only need dp[2][10000+5], but the scroll array optimization is not particularly obvious, the reader self-Baidu search scrolling array method, I do not give out. Space permitting, I think it is better to open a two-dimensional array, intuitive and not error-prone, of course, the practice of rolling arrays is also very important in DP.
#include <vector> #include <cstdio> #include <string> #include <cstring> #include <iostream > #include <algorithm>using namespace std;typedef __int64 ll;const int maxk = ten + 1;const int maxn = + 5;cons t int maxm = 10000 + 5;int N, M, k;int dp[maxk][maxm];struct shoe{int price, value; Shoe () {} Shoe (int p, int v): Price (P), value (v) {}};vector<shoe> Shoes[11];int main () {//freopen ("input.in "," R ", stdin); while (~SCANF ("%d%d", &n, &m, &k)) {int brand, price, value; for (int k = 0; k <= k; k++) shoes[k].clear (); for (int i = 0; i < N; i++) {scanf ("%d%d%d", &brand, &price, &value); Shoes[brand].push_back (Shoe (price, value)); }//The following code is the key to the whole question, so I'll give you a little weight in the comments in the code below ... O (∩_∩) o haha ~//Here must be the DP initialized to-1, or-inf,dp[k][v] = 1 Or-inf This is to indicate that the former K group of money to V can not take any shoes. Memset (DP,-1, sizeof (DP)); Without a group, no matter how much money you have, of course, the maximum value at this time is 0. for (int v = 0; v <= M; v++) dp[0][v] = 0; or memset (dp[0],0,sizeof (dp[0]); The following three-layer cycle ensures that at least one item in each group is added to the backpack.//First, enumerate the number of groups for (int k = 1; k <= K; k++) {///Then, for all of group K The element is enumerated for (int i = 0; i < shoes[k].size (); i++) {////Why decrement? Here is the same as the implementation of the 01 backpack scrolling array, as Only one item can be selected for Article K Group I. for (int v = M; v >= shoes[k][i].price; v--) {//The following is the case of judging the current selection, I have this brand has been selected once, if elected, I You can also continue to select if (dp[k][v-shoes[k][i].price]! =-1) dp[k][v] = max (Dp[k][v], dp[k][v -Shoes[k][i].price] + shoes[k][i].value); if (Dp[k-1][v-shoes[k][i].price]! =-1) dp[k][v] = max (Dp[k][v], Dp[k-1][v-shoes[k][i].price ] + Shoes[k][i].value); }}}//If DP[K][M] is 1, then it proves that there is no feasible solution if (Dp[k][m] < 0) printf ("impossible\n"); else printf ("%d\n", Dp[k][m]); } return 0;}
Grouped knapsack problems are called a group of mutually exclusive items, which creates a good model. Many knapsack problems can be transformed into a group of knapsack problem, the knapsack problem can further define the concept of "generalized items", is very helpful to solve problems.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
HDU 3033 I Love sneakers! "detail DP Pack Backpack"