Title Description
Kim is very happy today, the home purchase of the new house on the key, the new house has a very spacious room of his own dedicated. To his delight, the mother said to him yesterday: "Your room needs to buy what items, how to decorate, you decide, as long as not more than n yuan money on the line." Early this morning, he began to make a budget, he wanted to buy items divided into two categories: the main parts and accessories, attachments are subordinate to a certain main parts, the following table is some examples of the main parts and accessories:
Main parts Accessories
Computer printers, scanners
Bookcase Books
Desk lamp, stationery
Work Chair No
If you want to buy an item that is classified as an accessory, you must first buy the main part that the attachment belongs to. Each main piece can have 0, one, or 2 attachments. Attachments no longer have attachments that belong to them. Jinming want to buy a lot of things, will certainly exceed the mother limit of N yuan. As a result, he set an important degree of each article, divided into 5, such as: The whole number of the 5th, and so the most important. He also found the price of each item (all multiples of 10 yuan) from the Internet. He hoped that the sum of the product of the price and the importance of each item would be the largest if not more than n yuan (which could equal N).
The price of the article J is v[j], the importance of w[j], a total of K items selected, the number is J1,J2,......,JK, then the sum is: v[j1]*w[j1]+v[j2]*w[j2]+ ... +v[jk]*w[jk]. (where * for multiplication sign) please help Jinming to design a shopping list that satisfies the requirements. Input format
The 1th line of the input file, which is two positive integers, is separated by a space:
Nm
where N (<32000) represents the total amount of money, M (<60) is the number of items you wish to purchase. )
From line 2nd to line m+1, line J gives basic data for items numbered J-1, with 3 non-negative integers per line
V P q
(where v indicates the price of the item (v<10000), p indicates the item's importance (), q indicates whether the item is a main part or an accessory. If q=0, indicates that the item is the main part, if q>0, indicates that the item is an attachment, q is the number of the main part of the output format
The output file has only a positive integer, which is the maximum value of the sum of the product of the price and the importance of the item not exceeding the total amount of money
(<200000).
In
1000 5
800 2 0
400 5 1
300 5 1
400 3 0
500 2 0
Out
2200
On the basis of 01 knapsack dynamic programming, the problem of main parts attachment is added. Even if an accessory is cost-effective, it may not be able to take it, because it has to be qualified to be taken after its main part has been taken.
The idea here is that each of the main items with a chain, the chain with its attachments, because no more than 2 attachments, so open two pairs.
The exploration is still in accordance with the 01 backpack mode, but at a certain price J, not only to see minus the current main item I the price of the remaining money comparison, but also to see minus the main pieces, minus one main and the first attachment, minus one main and the second attachment, minus the main and all the accessories of the case. That is, the main parts can be obtained under the premise of the review of its accessories.
#include <stdio.h> #include <algorithm> using namespace std;
int f1[65], f2[65], v[65], p[65], v1[65], v2[65], p1[65], p2[65], dp[32005];
int main () {int n, m, I, J, A, B, C;
scanf ("%d%d", &n, &m);
for (i = 1; I <= m; i++) {scanf ("%d%d%d", &a, &b, &c);
if (c = = 0) {V[i] = A;
P[i] = b;
} else {if (f1[c] = = 0) {F1[c] = i;
V1[i] = A;
P1[i] = b;
} else {F2[c] = i;
V2[i] = A;
P2[i] = b;
}}} int ans =-1; for (i = 1; l <= m; i++) for (j = n; j >= V[i]; j--) {Dp[j] = max (Dp[j], Dp[j-v[i]] + v[i] * p[
I]); if (J-v[i]-v1[f1[i]] >= 0) {Dp[j] = max (Dp[j], Dp[j-v[i]-v1[f1[i] [+ v[i] * P[i] + v1[f1[i]] * P1
[F1[i]]); } if (J-v[i]-v2[f2[i]] >= 0) {Dp[j] = max (Dp[j], Dp[j-v[i]-v2[f2[i]] + v[i] * P[i] + v2[f2[
I]] [p2[f2[i]]);
} if (J-v[i]-v1[f1[i]]-v2[f2[i]] >= 0) { DP[J] = max (Dp[j], Dp[j-v[i]-v1[f1[i]]-v2[f2[i []] + v[i] * P[i] + v1[f1[i] [p1[f1[i]] + v2[f2[i]] [p2[f2[i]]);
}} printf ("%d\n", Dp[n]);
return 0; }