The budget proposal of Jinming
"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. So, he set an important degree of each item, divided into 5, and so on: With an integer 1~5, the 5th is 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 * is multiplication sign)
Please help Jinming to design a shopping list that satisfies the requirements.
Separated:
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 (1~5), q Indicates whether the item is a main part 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 product)
"Output File"
The output file Budget.out has only a positive integer, which is the maximum value (<200000) of the sum of the price and the importance of the item that does not exceed the total amount of money.
"Input Sample"
1000 5
800 2 0
400 5 1
300 5 1
400 3 0
500 2 0
"Output Example"
2200
source code: #include<cstdio>intM,n,s (0), f[32000]={0};structt{intx, y;} i[ -][ -];intMain () {scanf ("%d%d",&n,&m); for(intA=1; a<=m;a++) { intT1,t2,t3; scanf ("%d%d%d",&t1,&t2,&T3); if(!T3) {i[++s][0].x=1; i[s][0].y=A; i[s][1].x=T1; i[s][1].y=t1*T2; } Else for(intb=1; b<=s;b++) if(t3==i[b][0].y) {intt=i[b][0].x; for(intC=1; c<=t;c++) {i[b][0].x++; i[b][i[b][0].x].x=i[b][c].x+T1; i[b][i[b][0].x].y=i[b][c].y+t1*T2; } Break; } } //preprocess all cases. for(intA=1; a<=s;a++) for(intB=n;b>0; b--) for(intC=1; c<=i[a][c].x;c++) if(b>=i[a][c].x)//Note that an array with a negative number should be prevented from appearing. f[b]=f[b]>f[b-i[a][c].x]+i[a][c].y?f[b]:f[b-i[a][c].x]+i[a][c].y;//Group backpack. printf"%d", F[n]); return 0;}
The budget proposal of Jinming