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.

题目大意像一个背包问题,物品可以放无穷次,但是增益是Ai×次数+Bi。

关键是多了B,不然就是一个普通的背包,能处理掉B,这题就能做。

于是考虑了这样一个状态p[flag][j]:

flag为真表示放过第i个物品的最优,flag为假表示不放第i个物品的最优。

j就是背包容量。

于是p[1][j] = max(p[0][j-w[i]]+a[i]+b[i], p[1][j-w[i]]+a[i]);

然后到i+1时,初始状态是都没有放过i+1物品的,所以需要初始化p[0][j]:

p[0][i] = max(p[0][i], p[1][i]);

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <algorithm>
#define LL long long using namespace std; const int maxN = ;
int n, m, p[][];
int w[maxN], a[maxN], b[maxN]; void input()
{
scanf("%d%d", &m, &n);
for (int i = ; i < n; ++i)
scanf("%d%d%d", &w[i], &a[i], &b[i]);
memset(p, , sizeof(p));
} void work()
{
for (int i = ; i < n; ++i)
{
for (int j = w[i]; j <= m; ++j)
p[][j] = max(p[][j-w[i]]+a[i]+b[i], p[][j-w[i]]+a[i]);
for (int j = ; j <= m; ++j)
p[][j] = max(p[][j], p[][j]);
}
int ans = ;
for (int i = ; i <= m; ++i)
{
ans = max(ans, p[][i]);
ans = max(ans, p[][i]);
}
printf("%d\n", ans);
} int main()
{
//freopen("test.in", "r", stdin);
int T;
scanf("%d", &T);
for (int times = ; times < T; ++times)
{
input();
work();
}
return ;
}

ACM学习历程—HDU5410 CRB and His Birthday(动态规划)的更多相关文章

  1. ACM学习历程—HDU5407 CRB and Candies(数论)

    Problem Description CRB has N different candies. He is going to eat K candies.He wonders how many co ...

  2. ACM学习历程—HDU 5534 Partial Tree(动态规划)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5534 题目大意是给了n个结点,让后让构成一个树,假设每个节点的度为r1, r2, ...rn,求f(x ...

  3. ACM学习历程—Hihocoder 1290 Demo Day(动态规划)

    http://hihocoder.com/problemset/problem/1290 这题是这次微软笔试的第三题,过的人比第一题少一点,这题一眼看过去就是动态规划,不过转移方程貌似不是很简单,调试 ...

  4. 完成了C++作业,本博客现在开始全面记录acm学习历程,真正的acm之路,现在开始

    以下以目前遇到题目开始记录,按发布时间排序 ACM之递推递归 ACM之数学题 拓扑排序 ACM之最短路径做题笔记与记录 STL学习笔记不(定期更新) 八皇后问题解题报告

  5. ACM学习历程—HDU 5512 Pagodas(数学)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5512 学习菊苣的博客,只粘链接,不粘题目描述了. 题目大意就是给了初始的集合{a, b},然后取集合里 ...

  6. ACM学习历程—HDU5521 Meeting(图论)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5521 学习菊苣的博客,只粘链接,不粘题目描述了. 题目大意就是一个人从1开始走,一个人从n开始走.让最 ...

  7. ACM学习历程—HDU2476 String painter(动态规划)

    http://acm.hdu.edu.cn/showproblem.php?pid=2476 题目大意是给定一个起始串和一个目标串,然后每次可以将某一段区间染成一种字符,问从起始串到目标串最少需要染多 ...

  8. ACM学习历程—HDU5700 区间交(树状数组 && 前缀和 && 排序)

    http://acm.hdu.edu.cn/showproblem.php?pid=5700 这是这次百度之星初赛2B的第五题.省赛回来看了一下,有这样一个思路:对于所有的区间排序,按左值排序. 然后 ...

  9. ACM学习历程—HDU5701 中位数计数(中位数 && 计数排序)

    http://acm.hdu.edu.cn/showproblem.php?pid=5701 这是这次百度之星初赛2B的第六题.之前白山云做过类似的题,省赛完回来,我看了一下大概就有这样的思路:首先枚 ...

随机推荐

  1. (webstorm的css编写插件)Emmet:HTML/CSS代码快速编写神器

    Emmet的前身是大名鼎鼎的Zen coding,如果你从事Web前端开发的话,对该插件一定不会陌生.它使用仿CSS选择器的语法来生成代码,大大提高了HTML/CSS代码编写的速度,比如下面的演示: ...

  2. PHPExcel简易使用教程

    在企业里使用PHP进行开发,不可避免总会遇到读/写Excel的需求,遇到这种需求,一般使用PHPExcel类库进行开发. PHPExcel现在最新版本是1.8.0,最低需要PHP5.2版本,支持读取x ...

  3. 03 redis之string类型命令解析

    Redis字符串类型的操作 set key value [ex 秒数] / [px 毫秒数] [nx] /[xx] 如: set a 1 ex 10 , 10秒有效 Set a 1 px 9000 , ...

  4. 初识ASP.net-牛腩新闻公布系统

           在做牛腩新闻公布的系统的时候,总有一种感觉就是:我仍然在敲机房收费系统,唯一不同的一点.就是敲机房收费的时候,用户界面是是自己手动画界面.而,在牛腩新闻公布系统中,用户界面,却是须要自己 ...

  5. IOS --支付宝SDK 分解讲解

    开发在手机端的时候(客户端),我们主要负责客户端(手机端)的开发,所以那些繁琐的到支付宝官网填写商户信息可以留给后台去弄,后台只要把: 1回调地址, 2app的ID, 3商户的私钥(privateKe ...

  6. python 基础 2.8 python练习题

    python 练习题:   #/usr/bin/python #coding=utf-8 #@Time   :2017/10/26 9:38 #@Auther :liuzhenchuan #@File ...

  7. Spanner: Google’s Globally-Distributed Database

    https://research.google.com/archive/spanner.html Spanner is Google’s scalable, multi-version, global ...

  8. Swift 学习笔记 (函数)

    函数 函数是一个独立的代码块,用来执行特定的任务.Swift中的函数与Object-C中的函数一样,但是声明与书写的方式不太一样,现在我们就通过几个例子介绍一下Swift中的函数.简单的来说,他与JS ...

  9. 百度小程序转换微信小程序

    Python脚本,一键转换Github地址:https://github.com/DWmelon/py-transfer-BdToWx 运行条件 具备Python环境,可在命令行中使用Python命令 ...

  10. phpPHP创建创建jpg格式图片以及压缩图片(转)

    其实是因为一些业务上的需求,所以需要对用户上传后的图片进行压缩,因为上传的图片比较大,显示的时候加载起来如果网速不给力的话就很吃力了,而且大图片也浪费空间,于是找了一下相关的资源,主要方法在开源中国上 ...