Problem 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 |
Attachment |
Computer |
Printers, scanners |
Bookcase |
Books |
Desk |
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 j_1,j_2,......,j_k, then the sum is:
v[j_1]*w[j_1]+v[j_2]*w[j_2]+. +v[j_k]*w[j_k]. (where * is multiplication sign)
Please help Jinming to design a shopping list that satisfies the requirements. Input format input File The 1th line of budget.in, two positive integers, separated by a space: N m (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 the basic data of the item numbered j-1, with 3 non-negative integer v p q per line (where v represents the price of the item (v<10000), p indicates the item's importance (a), q indicates whether the item is a main piece or an attachment. If q=0, indicates that the item is a main piece, if q>0, indicates that the item is an attachment, q is the number of the main part of the output format output file budget.out only a positive integer, the maximum value of the sum of the price and the importance of the item not exceeding the total amount of money (<200000). Sample input 1000 5800 2 0400 5 1300 5 1400 3 0500 2 0 Sample Output 2200 idea: You can see the variant of the 01 backpack, the price is equivalent to weight, the price multiplier importance is equal to the value and then roughly divided into the following five cases: (1 (2) Select main Parts (3) Select main parts plus the first attachment (4) Select main parts plus the second attachment (5) Select the main parts plus two attachments there are attachments there is no attachment is 0~2 a hypothesis all have attachments, if not, is 0*0, the results have no impact, more judgment several times will not take too long time
1#include <stdio.h>2#include <stdlib.h>3 4 #defineMax (A, B) (a>b)? (a):(B)5 #defineM 616 #defineN 330007 8 voidInputint[][3] ,int[][3] ,int );9 intMaxmoney (int[][3] ,int[][3] ,int,int );Ten One intMainvoid){ A intN, M; -scanf"%d%d", &n, &m); - the intv[m][3] = {0}, p[m][3] = {0} ; - - input (V, p, m); - +printf"%d\n", Maxmoney (V, p, M, N)); - return 0; + } A at intMaxmoney (intv[][3] ,intp[][3] ,intMintN) { - Static intDp[m][n] = {0} ; - intI, J, K; - for(i =1; I <= m; i + + ){ - for(j =1; J <= N; J + + ){ - if(j-v[i][0] >=0 ){ inDP[I][J] = Max (dp[i-1][J], dp[i-1][j-v[i][0]] + v[i][0] * p[i][0] ); - //main Part No. No. 0 to if(j-v[i][0]-v[i][1] >=0 ){ +DP[I][J] = Max (Dp[i][j], dp[i-1][j-v[i][0]-v[i][1]] + v[i][0] * p[i][0] + v[i][1] * p[i][1] ); - //Select Accessory No. 1th the } * if(j-v[i][0]-v[i][2] >=0 ){ $DP[I][J] = Max (Dp[i][j], dp[i-1][j-v[i][0]-v[i][2]] + v[i][0] * p[i][0] + v[i][2] * p[i][2] );Panax Notoginseng //Select Accessory No. 2nd - } the if(j-v[i][0]-v[i][1]-v[i][2] >=0 ){ +DP[I][J] = Max (Dp[i][j], dp[i-1][j-v[i][0]-v[i][1]-v[i][2]] + v[i][0] * p[i][0] + v[i][1] * p[i][1] + v[i][2] * p[i][2] ); A //Select Accessories 1th and 2nd the } +}Else { -DP[I][J] = dp[i-1][j]; $ } $ } - } - returnDp[m][n]; the } - Wuyi voidInputintv[][3] ,intp[][3] ,intN) { the intI, TV, TP, TQ; - for(i =1; I <= N; i + + ){ Wuscanf"%d%d%d", &tv, &TP, &TQ); - if(TQ = =0 ){ Aboutv[i][0] = TV, p[i][0] =TP; $ //fill the main part No. No. 0 -}Else if(v[TQ] [1] ==0 ){ -v[TQ [1] = TV, p[TQ [1] =TP; - //Fill Attachment No. 1th A}Else { +v[TQ [2] = TV, p[TQ [2] =TP; the //Fill Attachment No. 2nd - } $ } the}
Algorithm to improve the budget plan of jinming