CRB and His Birthday

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 357 Accepted Submission(s): 191

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

Hint

CRB’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

题意:今天是CRB的生日,他的妈妈去商店给他买礼物,由于收银员是他妈妈的好朋友,所以收银员会按照不同礼 物的件数x赠与CRB的妈妈(a*x+b)块糖果。CRB的妈妈总共带了w元钱,总共有n种礼物。t组输入,每组先输入w 和n,接下来n行每行是该种礼物需要花费的价格和对应的a与b。求CRB的妈妈最多能获得多少糖果。

思路:因为每件物品都是无穷的所以是完全背包,但是只有在买第一件物品是多加b,所以将物品分成两部分,一部分是只有一件物品,进行01背包,一部分进行完全背包

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <map>
#include <algorithm>
#define INF 0x3f3f3f3f
using namespace std; typedef unsigned long long LL; int Dp[2100]; int n,m; int main()
{
int T;
int w,a,b;
scanf("%d",&T);
while(T--)
{
scanf("%d %d",&m,&n);
memset(Dp,0,sizeof(Dp));
for(int i=1;i<=n;i++)
{
scanf("%d %d %d",&w,&a,&b);
for(int j=m;j>=w;j--)//01背包
{
Dp[j]=max(Dp[j],Dp[j-w]+a+b);
}
for(int j=w;j<=m;j++)//完全背包
{
Dp[j]=max(Dp[j],Dp[j-w]+a);
}
}
printf("%d\n",Dp[m]);
}
return 0;
}

CRB and His Birthday(背包)的更多相关文章

  1. 2015暑假多校联合---CRB and His Birthday(01背包)

    题目链接 http://acm.split.hdu.edu.cn/showproblem.php?pid=5410 Problem Description Today is CRB's birthda ...

  2. HDU 5410 CRB and His Birthday(完全背包变形)

    CRB and His Birthday Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Oth ...

  3. hdu 5410 CRB and His Birthday(混合背包)

    Problem Description Today is CRB's birthday. His mom decided to buy many presents for her lovely son ...

  4. HDU 5410 CRB and His Birthday ——(完全背包变形)

    对于每个物品,如果购买,价值为A[i]*x+B[i]的背包问题. 先写了一发是WA的= =.代码如下: #include <stdio.h> #include <algorithm& ...

  5. 混合背包 hdu5410 CRB and His Birthday

    传送门:点击打开链接 题意:你有M块钱,如今有N件商品 第i件商品要Wi块,假设你购买x个这种商品.你将得到Ai*x+Bi个糖果 问能得到的最多的糖果数 思路:很好的一道01背包和全然背包结合的题目 ...

  6. HDU 5410 CRB and His Birthday (01背包,完全背包,混合)

    题意:有n种商品,每种商品中有a个糖果,如果买这种商品就送多b个糖果,只有第一次买的时候才送.现在有m元,最多能买多少糖果? 思路:第一次买一种商品时有送糖果,对这一次进行一次01背包,也就是只能买一 ...

  7. 背包DP HDOJ 5410 CRB and His Birthday

    题目传送门 题意:有n个商店,有m金钱,一个商店买x件商品需要x*w[i]的金钱,得到a[i] * x + b[i]件商品(x > 0),问最多能买到多少件商品 01背包+完全背包:首先x == ...

  8. hdu 5410 CRB and His Birthday 01背包和全然背包

    #include<stdio.h> #include<string.h> #include<vector> #include<queue> #inclu ...

  9. HDU 5410(2015多校10)-CRB and His Birthday(全然背包)

    题目地址:HDU 5410 题意:有M元钱,N种礼物,若第i种礼物买x件的话.会有Ai*x+Bi颗糖果,现给出每种礼物的单位价格.Ai值与Bi值.问最多能拿到多少颗糖果. 思路:全然背包问题. dp[ ...

随机推荐

  1. php实用类

    <?php class DBDA { public $host="localhost";//服务器地址 public $uid="root";//用户名 ...

  2. [原创]java WEB学习笔记71:Struts2 学习之路-- struts2常见的内建验证程序及注意点,短路验证,非字段验证,错误消息的重用

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  3. SQL 面向对象(委托)

    委托:也称为代理,事件也是一种委托:定义在类的最外面 1.定义委托关键字:delegate函数签名:签名和函数保持一致定义委托的时候要根据函数来定义public delegate int First( ...

  4. poj: 1005

    简单题 #include <iostream> #include <stdio.h> #include <string.h> #include <stack& ...

  5. ArrayList和LinkList区别

    ArrayList和LinkList区别 前者是数组的数据结构,后者是链表的数据结构 前者应用于排序和查找,后者应用于插入删除

  6. JSP数据交互习题错误总结

    1:如果注册完页面有中文字符需要在提交后的页面显示注册信息,切记先把接受到的request的编码方式改为中文:request.setCharacterEncoding("utf-8" ...

  7. 深度解析 Java 内存原型

    一.Java 虚拟机内存原型 寄存器:我们在程序中无法控制. 栈:存放基本类型的数据和对象的引用,但对象本身不存放在栈中,而是存放在 堆中. 堆:存放用 new 产生的数据. 静态域:存放在对象中用  ...

  8. 13---Net基础加强

    更新中,敬请期待............ 作业-复习 程序集1 程序集2 反射1 反射2 反射3 反射4-记事本插件1 反射5-记事本插件2

  9. COM编程之五 动静态链接

    [1]静态链接 静态链接是指由链接器在链接时将库的内容加入到可执行程序中的做法. 链接器是一个独立程序,将一个或多个库或目标文件(先前由编译器或汇编器生成)链接到一块生成可执行程序. 函数和数据被编译 ...

  10. linux mmap 内存映射【转】

    转自:http://blog.csdn.net/xyyangkun/article/details/7830313 [-] mmap vs readwritelseek mmap vs malloc ...