题目链接

http://acm.split.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.

 
Author
KUT(DPRK)
 
Source
 
Recommend
wange2014
 
题意:输入M,N,分别表示总的钱数和物品种数,接下来输入N行,每行3个数,单价、买一件送的糖数 、买一次送的糖数   求最多能得到多少糖?
 
思路:01背包,dp[i]表示i钱下能得到最多的糖数,vis[i][j]表示i钱下得到最多糖时,是否买j物品,状态转移方程dp[i]=dp[i-kind[j][0]]+kind[j][1]+(vis[i-kind[j][0]][j]==0) * kind[j][2];
 
代码如下:
 
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <cstdio>
  4. #include <cstring>
  5. #include <cmath>
  6. #define eps 1e-8
  7. #define maxn 105
  8. #define inf 0x3f3f3f3f3f3f3f3f
  9. #define IN freopen("in.txt","r",stdin);
  10. using namespace std;
  11. int dp[];
  12. int vis[][];
  13. int kind[][];
  14.  
  15. int main()
  16. {
  17. int T;
  18. int M,N;
  19. cin>>T;
  20. while(T--)
  21. {
  22. scanf("%d%d",&M,&N);
  23. for(int i=;i<N;i++)
  24. scanf("%d%d%d",&kind[i][],&kind[i][],&kind[i][]);
  25. memset(dp,,sizeof(dp));
  26. memset(vis,,sizeof(vis));
  27.  
  28. for(int i=;i<=M;i++)
  29. {
  30. int flag=-;
  31. for(int j=;j<N;j++)
  32. {
  33. if(i<kind[j][]) continue;
  34. int s=dp[i-kind[j][]]+kind[j][];
  35. if(!vis[i-kind[j][]][j]) s+=kind[j][];
  36. if(dp[i]<s)
  37. {
  38. dp[i]=s;
  39. flag=j;
  40. }
  41. }
  42. if(flag>=)
  43. {
  44. for(int j=;j<N;j++)
  45. {
  46. vis[i][j]=vis[i-kind[flag][]][j];
  47. }
  48. vis[i][flag]++;
  49. }
  50. }
  51. int tmp=;
  52. for(int i=;i<=M;i++)
  53. tmp=max(tmp,dp[i]);
  54. printf("%d\n",tmp);
  55. }
  56. return ;
  57. }

2015暑假多校联合---CRB and His Birthday(01背包)的更多相关文章

  1. 2015暑假多校联合---Zero Escape(变化的01背包)

    题目链接 http://acm.hust.edu.cn/vjudge/contest/130883#problem/C Problem Description Zero Escape, is a vi ...

  2. 2015暑假多校联合---Expression(区间DP)

    题目链接 http://acm.split.hdu.edu.cn/showproblem.php?pid=5396 Problem Description Teacher Mai has n numb ...

  3. 2015暑假多校联合---Mahjong tree(树上DP 、深搜)

    题目链接 http://acm.split.hdu.edu.cn/showproblem.php?pid=5379 Problem Description Little sun is an artis ...

  4. 2015暑假多校联合---Cake(深搜)

    题目链接:HDU 5355 http://acm.split.hdu.edu.cn/showproblem.php?pid=5355 Problem Description There are m s ...

  5. 2015暑假多校联合---Friends(dfs枚举)

    原题链接 Problem Description There are n people and m pairs of friends. For every pair of friends, they ...

  6. 2015暑假多校联合---Assignment(优先队列)

    原题链接 Problem Description Tom owns a company and he is the boss. There are n staffs which are numbere ...

  7. 2015暑假多校联合---Problem Killer(暴力)

    原题链接 Problem Description You are a "Problem Killer", you want to solve many problems. Now ...

  8. 2016暑假多校联合---Rikka with Sequence (线段树)

    2016暑假多校联合---Rikka with Sequence (线段树) Problem Description As we know, Rikka is poor at math. Yuta i ...

  9. 2016暑假多校联合---Windows 10

    2016暑假多校联合---Windows 10(HDU:5802) Problem Description Long long ago, there was an old monk living on ...

随机推荐

  1. 《JAVA 从入门到精通》 - 正式走向JAVA项目开发的路

    以前很多时候会开玩笑,说什么,三天学会PHP,七天精通Nodejs,xx天学会xx ... 一般来说,这样子说的多半都带有一点讽刺的意味,我也基本上从不相信什么快速入门.我以前在学校的时候自觉过很多门 ...

  2. iOS-国家气象局-天气预报接口等常用接口

    接口地址: http://www.weather.com.cn/data/sk/101010100.html http://www.weather.com.cn/data/cityinfo/10101 ...

  3. 防止SQL注入的,网站安全的一些常用解决方案

    --------------------------------------------------------过滤URL中的一些特殊字符,动态SQL语句使用PrepareStatement.. -- ...

  4. mysql创建数据库指定编码uft8

    mysql创建数据库指定编码uft8 CREATE DATABASE IF NOT EXISTS my_db default character set utf8 COLLATE utf8_gener ...

  5. Constraint1:主键约束,唯一性约束和唯一索引

    1,主键约束创建索引 作为Primay Key的列是唯一的,非空的,Sql Server在创建主键约束时,自动为主键列创建一个唯一索引,并且索引列不允许为null. create table dbo. ...

  6. java中有关线程的题目

    1,看一下下面程序错误发生在哪一行! class Test implements Runnable{ public void run(Thread t){ } } 2,输出结果是什么? class T ...

  7. maven -- 学习笔记(四)实现在Eclipse用maven搭建springmvc项目(附构建步骤和详细实现代码)

    Learn from:http://www.cnblogs.com/fangjins/archive/2012/05/06/2485459.html,感谢楼主的分享,才有下面的这篇学习小结 一.环境准 ...

  8. SharePoint Server 2013开发之旅(一):新的开发平台和典型开发场景介绍

    我终于开始写这个系列文章,实际上确实有一段时间没有动笔了.最近重新安装了一套SharePoint Server 2013的环境,计划利用工作之余的时间为大家写一点新的东西. SharePoint Se ...

  9. c#连接mysql环境配置

    写.net的时候一直用的都是sql sever,mysql小 有命令行方便就想试了一下,网上搜很久,下载很多配置文件都不成功.昨晚上搞到两点多,冒着生命危险. 后来终于在一个网站上找到这个 MySQL ...

  10. Struts2整合Hibernate3实现用户登录功能

    所用技术:struts2 ,hibernate,jsp,mysql 本DEMO仅仅实现用户登录功能,采用MVC思想,自己也觉得相对是比较简单,比较容易理解数据流向的一个例子,通过整合这个过程,能够清晰 ...