码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 5410 CRB and His Birthday

时间:2015-08-27 18:05:58      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5410

Problem Description
Today is CRB‘s birthday. His mom decided to buy many presents for her lovely son.
She went to the nearest shop with M Won(currency unit).
At the shop, there are N kinds of presents.
It costs Wi Won to buy one present of i-th kind. (So it costs k × Wi Won to buy k of them.)
But as the counter of the shop is her friend, the counter will give Ai × x + Bi candies if she buys x(x>0) presents of i-th kind.
She wants to receive maximum candies. Your task is to help her.
1 ≤ T ≤ 20
1 ≤ M ≤ 2000
1 ≤ N ≤ 1000
0 ≤ Ai, Bi ≤ 2000
1 ≤ Wi ≤ 2000
 

 

Input
There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. For each test case:
The first line contains two integers M and N.
Then N lines follow, i-th line contains three space separated integers WiAi and Bi.
 

 

Output
For each test case, output the maximum candies she can gain.
 

 

Sample Input
1 100 2 10 2 1 20 1 1
 

 

Sample Output
21
Hint
CRB‘s mom buys 10 presents of first kind, and receives 2 × 10 + 1 = 21 candies.
 
题目的大概意思是有M元钱,有N件物品,没件物品的花费是W[i],第i件物品买X件能得到A[i]*X+B[i]块糖,没件物品买的数量没有上限
问这M元钱最多能得到多少块糖。
问题分析:没种物品买第一件能得到A[i]+B[i]块糖,以后每买一件只能多得到A[i]块糖,也就是每种物品买第一件比买后面的得到的糖多,
              根据贪心的思想很容易想到要先每种物品买一件再买大于一件,这样就把问题分成了两个阶段,第一阶段每种物品最多只买一件,
              0/1背包,第二阶段,没种物品能买无限件,完全背包。
技术分享
 1 #include <iostream>
 2 #include <cstring>
 3 using namespace std;
 4 
 5 int ma(int a,int b)
 6 {
 7     if (a>b)
 8     return a;
 9     else
10     return b;
11 }
12 int main()
13 {
14     int ans[12000],v,a[1100],b[1100],c[1100],n,m,T;
15     cin>>T;
16     while (T--)
17     {
18           cin>>v>>m;
19           for (int i=1;i<=m;i++)
20           cin>>c[i]>>a[i]>>b[i];
21           memset(ans,0,sizeof(ans));
22           for (int i=1;i<=m;i++)//处理第一阶段,0/1背包 
23           {
24               for (int j=v;j>=c[i];j--)
25               {
26                   ans[j]=ma(ans[j],ans[j-c[i]]+a[i]+b[i]);              
27               }
28           }
29           for (int i=1;i<=m;i++)//处理第二阶段,完全背包。 
30           {
31               for (int j=c[i];j<=v;j++)
32               {
33                    ans[j]=ma(ans[j],ans[j-c[i]]+a[i]);
34               }
35           }
36           cout <<ans[v]<<endl;
37     }
38     return 0;
39 }   
View Code

 

HDU 5410 CRB and His Birthday

标签:

原文地址:http://www.cnblogs.com/arno-my-boke/p/4763998.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!