Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=5410
Problem Descriptiontoday is CRB ' s birthday. His mom decided-buy many presents for her lovely son.
She went to the nearest shop withMWon (currency unit).
At the shop, there isNKinds of presents.
It costsWi Won to buy one present ofI-th kind. (So it costskXWi Won to buykof them.)
But as the counter of the shop was her friend, the counter would giveAi x x + Bi Candies if she buysx(x>0) presents ofI-th kind.
She wants to receive maximum candies. Your task is for help.
1≤T≤20
1≤M≤2000
1≤N≤1000
0≤Ai, Bi ≤2000
1≤Wi ≤2000
Inputthere is multiple test cases. The first line of input contains an integerT, indicating the number of test cases. For each test case:
The first line contains integersMandN.
ThenNLines follow,I-th line contains three space separated integersWi ,Ai and Bi.
Outputfor each test case, output the maximum candies she can gain.
Sample Input1100 210 2 120 1 1
Sample Output21
HintCRB ' s mom buys presents of first kind, and receives 2x10 + 1 = candies. The approximate meaning of the topic is that there is M yuan, there are n items, the cost of no item is w[i], the article I items buy X can get a[i]*x+b[i] block sugar, no item to buy the number of No limit ask this m yuan can get the maximum amount of sugar. Problem analysis: No kind of goods to buy the first can get a[i]+b[i] block sugar, after each buy one can only get a[i] block sugar, that is, each item buys the first more than buy the back of the sugar, according to the thought of greed is easy to think of each item to buy a purchase more than a piece, so that the problem into two orders Paragraph, the first stage of each item up to buy only one, 0/1 backpack, the second stage, no kind of items can buy unlimited pieces, complete backpack.
1#include <iostream>2#include <cstring>3 using namespacestd;4 5 intMaintAintb)6 {7 if(a>b)8 returnA;9 ElseTen returnb; One } A intMain () - { - intans[12000],v,a[1100],b[1100],c[1100],n,m,t; theCin>>T; - while(t--) - { -Cin>>v>>m; + for(intI=1; i<=m;i++) -Cin>>c[i]>>a[i]>>B[i]; +memset (ans,0,sizeof(ans)); A for(intI=1; i<=m;i++)//handling the first stage, 0/1 backpacks at { - for(intj=v;j>=c[i];j--) - { -Ans[j]=ma (ans[j],ans[j-c[i]]+a[i]+B[i]); - } - } in for(intI=1; i<=m;i++)//Process The second stage, complete the backpack. - { to for(intj=c[i];j<=v;j++) + { -Ans[j]=ma (ans[j],ans[j-c[i]]+a[i]); the } * } $cout <<ans[v]<<Endl;Panax Notoginseng } - return 0; the}
View Code
HDU 5410 CRB and his Birthday