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攻略(3)——数据的存储与检索

    要点目录: I.保存数据 II.打开文件   III.创建并写入文件 IV.关闭文件 V.读文件 VI.给文件加锁 VII.删除文件 VIII.其他有用的文件操作函数 IX.数据库管理系统 1.保存数 ...

  2. leetcode98 Validate Binary Search Tree

    题目: Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is define ...

  3. MAT之prim算法

    prim算法 边赋以权值的图称为网或带权图,带权图的生成树也是带权的,生成树T各边的权值总和称为该树的权. 最小生成树(MST):权值最小的生成树. 生成树和最小生成树的应用:要连通n个城市需要n-1 ...

  4. [原创]java WEB学习笔记57:Struts2学习之路---ActionSupport类的说明

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

  5. sqlserver 存储过程 以及统计整个数据库数据

    drop proc test 删除存储过程 go  用于在 SSMS 和 SQLCMD 中将其之前的 T-SQL 语句作为一个批处理提交给 SQL Server 实例.GO 不是 T-SQL 语句,只 ...

  6. C++之路进阶——hdu2222(Keywords Search)

    /*Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) ...

  7. ajax基本用法

    ajax能做到无刷新数据交互,给用户体验带来好处的同时也减小了服务器的压力,所以运用ajax能使网站性能更强劲.更吸引用户. 大型网站少不了注册页面,而大多数情况下我们不想让用户有相同的注册ID,所以 ...

  8. JQuery书写Ajax的几种方式?

    1 $.ajax({ type: "Post", //请求方式 ("POST" 或 "GET"), 默认为 "GET" ...

  9. Oracle游标整理二

    1.概念     游标是指向SQL处理的内存区的句柄或指针.当使用一个PL/SQL块来执行DML语句或只返回一行结果的SELECT语句时,系统将自动创建一个隐式游标.如果SQL语句返回多个结果,就必须 ...

  10. session_start()一些问题

    session问题集锦 对于PHP的session功能,始终找不到合适的答案,尤其是一些错误,还有一些没有错误的结果,最可怕的就是后者,一直为许多的初学者为难.就连有些老手,有时都被搞得莫名其妙.本文 ...