hdu 5410 CRB and His Birthday 背包问题 2015 Multi-University Training Contest 10
2015-09-07 16:20
399 查看
CRB and His Birthday
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 852 Accepted Submission(s): 450
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 Wi, Ai 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 HintCRB's mom buys 10 presents of first kind, and receives 2 × 10 + 1 = 21 candies.
Author
KUT(DPRK)
Source
2015 Multi-University Training Contest 10
Recommend
wange2014 | We have carefully selected several similar problems for you: 5431 5430 5429 5428 5427
二十多天以前,就是在这场多校比赛中,我们队爆蛋了,今天终于报仇雪恨了
上次三个人五个小时想不出来的题目,我今天十几分钟就想出来了,哼。
首先,不能暴力,而且对付这种 有限容量,放占用容量的东西去求最大价值的问题 有固定的解决办法,那就是背包。
当时就是想不通,怎么保证购买了某种商品只加一个B,当时想到把买某一个商品,第一个价值为A+B(只有1个卖),
之后为A(有无限个卖),
但是死活认为不能保证只有买了第一个(价值A+B),才能买后面的(A)。
其实很简单,背包肯定是要用的,那我们就去更多的找定的规律,
首先,这个背包 与 物品选择的先后无关,不像之前有一道题。(那么不用考虑整体的贪心,即物品与物品之间的顺序)
那么对于某种物品,我们可以更多的向贪心(定)的方向去想。//对于其它题目 也可以试试对所有物品的贪心,找出规律
你会发现,第一件物品(A+B),和后面的(A)花费相同,但是价值不同。
我么可以试着,把(A+B)先加入递推考虑,再加入(A) ,既然花费一样,那么v的取值范围就应该一样(在这个范围内考虑dp[v]=max(dp[v-cost[x]],....)) ,v-cost[x]也一样。
如果A+B这个价值大的都不用买(没有更新dp值),那么与它同样花费 却比它 价值小的(A)就更不能更新dp了,
所以说(按照背包解法)如果A+B没有买,A就自然不会买
如果A+B买了,A可能买也可能不买。(由此保证符合实际情况)
对于某种物品
先进行01背包(A+B),再进行完全背包(A),这就是解法。
其实先进行完全背包(A),再进行01背包(A+B),也正确(其实不影响)。
const int INF =0x3f3f3f3f; const int maxn= 1000+10 ; const int maxm= 2000+10 ; //by yskysker123 int cost[maxn],val[maxn],add[maxn]; int dp[maxm],m,n; void zerop(int x) { int c=cost[x],va=val[x]+add[x]; for(int v=m;v>=c;v--) { dp[v]=max(dp[v],dp[v-c ]+va); } } void full(int x) { for(int v=cost[x];v<=m;v++) { dp[v]=max(dp[v],dp[v-cost[x]]+val[x]); } } int main() { int T;scanf("%d",&T); while(T--) { scanf("%d%d",&m,&n); for(int i=1;i<=n;i++) { scanf("%d%d%d",&cost[i],&val[i],&add[i]); } memset(dp,0,sizeof dp); for(int i=1;i<=n;i++) { zerop(i); full(i); } printf("%d\n",dp[m]); } return 0; }
#include<cstdio> #include<string> #include<cstring> #include<iostream> #include<cmath> #include<algorithm> #include<climits> #include<queue> #include<vector> #include<map> #include<sstream> #include<set> #include<stack> #include<utility> #pragma comment(linker, "/STACK:102400000,102400000") #define PI 3.1415926535897932384626 #define eps 1e-10 #define sqr(x) ((x)*(x)) #define FOR0(i,n) for(int i=0 ;i<(n) ;i++) #define FOR1(i,n) for(int i=1 ;i<=(n) ;i++) #define FORD(i,n) for(int i=(n) ;i>=0 ;i--) #define lson num<<1,le,mid #define rson num<<1|1,mid+1,ri #define MID int mid=(le+ri)>>1 #define zero(x)((x>0? x:-x)<1e-15) #define mk make_pair #define _f first #define _s second using namespace std; //const int INF= ; typedef long long ll; //const ll inf =1000000000000000;//1e15; //ifstream fin("input.txt"); //ofstream fout("output.txt"); //fin.close(); //fout.close(); //freopen("a.in","r",stdin); //freopen("a.out","w",stdout);
相关文章推荐
- Release Notes for Contrail Release 2.20
- 生产者消费者之 wait / notify
- tair集群配置
- nonatomic, retain,weak,strong用法详解
- ORA-29861: 域索引标记为 LOADING/FAILED/UNUSABLE
- RabbitMQ消息队列(一): Detailed Introduction 详细介绍(转)
- Light oj 1021 Painful Bases(状压dp)
- NetworkOnMainThreadException异常解决办法
- UVA 818 Cutting Chains
- WPF ItemContainerGenerator.ContainerFromItem返回Null
- fatal error LINK1123:failure during conversion to COFF:file invalid or corrupt
- 根文件系统挂载 http://blog.csdn.net/new_abc/article/details/7685681
- StarUML 5.0问题解决:Failed to open the model file. Invalid file format.
- [置顶] 《Linux启动过程分析》内核挂载根文件系统 http://blog.csdn.net/tankai19880619/article/details/12093239
- tools:context=".MainActivity的作用
- http://blog.csdn.net/renfufei/article/details/41647937
- 线程间的通信——wait()/notify()
- High scalability - Harvard notes.
- 51nod 1305:Pairwise Sum and Divide
- 51nod 1305:Pairwise Sum and Divide