题目链接: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 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.

题目的大概意思是有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

转载于:https://www.cnblogs.com/arno-my-boke/p/4763998.html

HDU 5410 CRB and His Birthday相关推荐

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

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

  2. hdu 5410(背包问题变形)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5410 解题思路:令dp[i][j][0]表示前i种物品,共j钱,不买第i种物品所能买到的最大值.dp[ ...

  3. HDU - 5411 CRB and Puzzle 矩阵快速幂

    HDU - 5411 考虑直接dp会T, 用矩阵优化一下就好了. #include<bits/stdc++.h> #define LL long long #define LD long ...

  4. hdu 5411 CRB and Puzzle(矩阵快速幂)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5411 解题思路: 题目大意: 给定n个点 常数m 下面n行第i行第一个数字表示i点的出边数,后面给出 ...

  5. HDU 5411 CRB and Puzzle (2015年多校比赛第10场)

    1.题目描写叙述:点击打开链接 2.解题思路:本题实际是是已知一张无向图.问长度小于等于m的路径一共同拥有多少条. 能够通过建立转移矩阵利用矩阵高速幂解决.当中,转移矩阵就是输入时候的邻接矩阵,同一时 ...

  6. hdu 5411 CRB and Puzzle 矩阵高速幂

    链接 题解链接:http://www.cygmasot.com/index.php/2015/08/20/hdu_5411/ 给定n个点 常数m 以下n行第i行第一个数字表示i点的出边数.后面给出这些 ...

  7. hdu 5411 CRB and Puzzle

    Sample Input 1 3 2 1 2 1 3 0 Sample Output 6 Hint possible patterns are ∅, 1, 2, 3, 1→2, 2→3 解释样例: 第 ...

  8. 【矩阵快速幂】 HDU 5411 CRB and Puzzle 等比

    点击打开链接 题意显然是 求 A+A^2+A^3+....+A^m 这就是经典题目 矩阵乘法十种经典题目 递归解决 后半部分提取 A^(m/2) 该题再特判下 m==1的时候 #include < ...

  9. 小算法小心情:背包问题就是陪你看花开向阳

    一开始接触背包问题,总会有些困扰,无法完全理解,或者说不断地忘记所谓的公式,特神说分享,可以让许多人少走弯路,我觉得颇有道理,其实我想做的,只是努力让让算法变得看上去简单易懂一些,因为我也曾是个迷惘无 ...

最新文章

  1. 3168串口java_电子称串口读取数据(转)
  2. 《TCP/IP详解 卷1:协议》第4章 ARP:地址解析协议
  3. python调用动态库出现error193_切换到64位时使用Python Winerror 193
  4. 单点登录(SSO)—简介 1
  5. css 相对定位 ie7问题
  6. python调用opengl_Python运行OpenGL示例
  7. Concurrent HTTP connections in Node.js
  8. java外部类_Java里什么叫内部类什么叫外部类
  9. 强健程序员体魄————减脂原理
  10. 狗狗币暴涨暴跌?数据分析师带你走进它的前世今生!
  11. 运营九年,这款音乐手游宣布停服!将从应用商店下架...
  12. 【hortonworks/registries】Parameter Schema name is null
  13. 2008年9月三级网络技术考试试卷 参考答案1
  14. PHP学习笔记01——基础语法
  15. Python入门--字符串的分割操作,split,rsplit
  16. VMProtect修复导入表的插件
  17. Mac版 QQ防撤回插件
  18. swift 隐藏状态栏_Swift - 动态改变状态栏statusBar文字颜色(preferredStatusBarStyle无效问题)...
  19. sony xz2c android升9,坐稳放宽,索尼Xperia XZ2/XZ2 Compact安卓9 Pie已开始逐步推送
  20. Android 常用开源库总结

热门文章

  1. python爬取flash数据_爬取flash数据
  2. faster rcnn resnet_张航、李沐等人提出ResNet最强改进版:性能提高3%,参数不增
  3. 数值分析matlab实验报告,数值分析第一次作业matlab实验报告.doc
  4. mysql的储存原理_mysql储存原理
  5. 项目管理实践之版本控制工具SVN
  6. C语言:输入四个数a,b,c,d,将这四个数由小到大排序
  7. php数组array_filter,php数组array_filter()函数和array_slice()函数
  8. 图的存储结构之邻接表
  9. spark 持久化机制入门
  10. 信息存储服务公司Everlaw获810万美金注资