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. Hibernate学习六----------CRUD

    © 版权声明:本文为博主原创文章,转载请注明出处 实例 1.项目结构 2.pom.xml <project xmlns="http://maven.apache.org/POM/4.0 ...

  2. Android 开发资源

    0x00 Android 入门资料 a. Android 学习之路 b.<第一行代码> 介绍及购买链接:http://blog.csdn.net/guolin_blog/article/d ...

  3. centos部署Python环境

    在centos上部署Python之前,我们需要先配置开发环境. 1.安装Python依赖的开发工具包 gcc自然少不了,可以直接用“Development Tools”: yum grouplist ...

  4. Bootstrp--一个导航面板切换的实用例子

    <!--导航区开始--> <ul class="nav nav-tabs nav-stacked" role="tablist"> &l ...

  5. 宜人贷PaaS数据服务平台Genie:技术架构及功能

    上篇:架构及组件 一.数据平台的发展 1.1 背景介绍 随着数据时代的到来,数据量和数据复杂度的增加推动了数据工程领域的快速发展.为了满足各类数据获取/计算等需求,业内涌现出了诸多解决方案.但大部分方 ...

  6. mac环境下清理系统垃圾clearMyMac 3.9 破解版

    mac环境下清理系统垃圾clearMyMac 3 轻轻松松清理好几十G的垃圾文件 下载地址 链接: https://pan.baidu.com/s/1XZbZwzhgQCnzpvQDvyQrRA 密码 ...

  7. python学习(四)字符串学习

    #!/usr/bin/python # 这一节学习的是python中的字符串操作 # 字符串是在Python中作为序列存在的, 其他的序列有列表和元组 # 1. 序列的操作 S = 'Spam' # ...

  8. 【转】【Axure学习】之短信动态验证码+图片动态验证码

    感谢:努力拼搏的80后的<巧用Axure三步轻松搞定图片验证码>. 人人都是产品经理的<Axure 教程:实现倒计时获取验证码效果>

  9. vue-strap 修改Modal组件

    在用到vue-strap的Modal组件时,会有两个默认按钮,查看官方文档配置如下: 可以看到,ok-text和cancel-text都有一个默认值,在使用时即使不给这两个选项赋值,也会显示两个默认文 ...

  10. eclipse中三大利器

    eclipse中两大利器: 首先说下用eclipse开发工具.进行java代码,开发的时候,我们开发完成以后.需要测试.大部分我们用Junit测试工具.可是内部的代码覆盖率.和结构我们看的不是那么详细 ...