CRB and His Birthday

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 1655    Accepted Submission(s): 782

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种,买商品的时候会附加赠送糖果,并且给出了各个商品需要的话费wi元,还有关于赠送糖果量的相关系数ai和bi。随着该物品的购买量x会赠送ai*x+bi个糖果。问,用m元如何购买商品能够获得的最多糖果数目。

解题思路:乍一看就是个完全背包,但是有额外增加的条件。随着购买量增加的赠送的糖果量。bi后面没有涉及到x。所以确定第一个的赠送量就是ai+bi。后面的赠送购买赠送的糖过量就和bi没有关系了,就变成了一个完全背包。刚开始的明显可以通过01背包来解决了。

源代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<stack>
#include<queue>
#include<vector>
#include<deque>
#include<map>
#include<set>
#include<algorithm>
#include<string>
#include<iomanip>
#include<cstdlib>
#include<cmath>
#include<sstream>
#include<ctime>
using namespace std; int w[1005];
int dp[2005];
int a[1005];
int b[1005]; int main()
{
int t;
int m,n;//m钱款,n代表商品种类数
int i,j;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&m,&n);
memset(w,0,sizeof(w));
memset(dp,0,sizeof(dp));
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
for(i = 0; i < n; i++)
{
scanf("%d%d%d",&w[i],&a[i],&b[i]);
}
for(i = 0; i < n; i++)
{
//先跑一次01背包,这样就不用再考虑bi,因为买与不买都会赠送bi糖果,
//0次或者1次考虑完之后,就剩下多次的,再跑完全背包
for(j = m; j >= w[i]; j--)
{
dp[j]=max(dp[j],dp[j-w[i]]+a[i]+b[i]);
}
for(j = w[i]; j <= m; j++)
{
dp[j]=max(dp[j],dp[j-w[i]]+a[i]);
}
}
printf("%d\n",dp[m]);
}
return 0;
}

HDU5410--01背包+完全背包的更多相关文章

  1. luogu 4377 Talent show 01分数规划+背包dp

    01分数规划+背包dp 将分式下面的部分向右边挪过去,通过二分答案验证, 注意二分答案中如果验证的mid是int那么l=mid+1,r=mid-1,double类型中r=mid,l=mid; 背包dp ...

  2. 01二维背包——poj2576

    /* 要求把a数组分成两个集合,两个集合人数最多差1,并且元素之和的差尽可能小 那只要把所有可行的列出来即可 01二维背包,即体积是个二维数据,那么我们的背包状态也应该设为二维 dp[j][k]设为 ...

  3. HDU 3591 The trouble of Xiaoqian(多重背包+全然背包)

    HDU 3591 The trouble of Xiaoqian(多重背包+全然背包) pid=3591">http://acm.hdu.edu.cn/showproblem.php? ...

  4. 背包!背包!HDU 2602 Bone Collector + HDU 1114 Piggy-Bank + HDU 2191 512

    http://acm.hdu.edu.cn/showproblem.php?pid=2602 第一题 01背包问题 http://acm.hdu.edu.cn/showproblem.php?pid= ...

  5. POJ 3260 The Fewest Coins(多重背包+全然背包)

    POJ 3260 The Fewest Coins(多重背包+全然背包) http://poj.org/problem?id=3260 题意: John要去买价值为m的商品. 如今的货币系统有n种货币 ...

  6. dp--01背包,完全背包,多重背包

    背包问题 以下代码 n是物品个数,m是背包容积 物品价值和重量int v[maxn],w[maxn]; 01背包 模板 for(int i = 0; i < n; i++) { for(int ...

  7. POJ 3260 多重背包+完全背包

    前几天刚回到家却发现家里没网线 && 路由器都被带走了,无奈之下只好铤而走险尝试蹭隔壁家的WiFi,不试不知道,一试吓一跳,用个手机软件简简单单就连上了,然后在浏览器输入192.168 ...

  8. 【poj3260-最少找零】多重背包+完全背包

    多重背包+完全背包. 买家:多重背包:售货员:完全背包: 开两个数组,分别计算出买家,售货员每个面额的最少张数. 最重要的是上界的处理:上界为maxw*maxw+m(maxw最大面额的纸币). (网上 ...

  9. B 维背包+完全背包 Hdu2159

    <span style="color:#3333ff;">/* ---------------------------------------------------- ...

  10. bzoj5281/luogu4377 Talent Show (01分数规划+背包dp)

    就是01分数规划的思路,只不过当把w[i]-r*t[i]>0的选完以后如果w值还没达到要求,那就再01背包dp一下就好了(dp时w值>W的时候就存在W里就不会爆内存了). (跑得很慢..大 ...

随机推荐

  1. SQL数据查询语句(一)

    本文所用数据库为db_Test,数据表为Employee 一.SELECT语句基本结构 语句语法简单归纳为: SELECT select_list [INTO new_table_name] [FRO ...

  2. Coredata中的多线程

    =================== 疑问: 1.coredata是什么?结构 2.如果在简单的demo中,我们可以在主线程中使用coredata.但是如果在真正的大项目中,这样可行么? 3.假设都 ...

  3. Redhat Linux 自动修改密码

    bash下使用echo+passwd命令修改密码的方法.方法一:echo "Password" |passwd username --stdin方法二:(echo "Pa ...

  4. D - MUH and Cube Walls

    D. MUH and Cube Walls   Polar bears Menshykov and Uslada from the zoo of St. Petersburg and elephant ...

  5. SharePoint Server 2013 安装篇 - 如何解决无法找到 .net 4.5 的问题

    SharePoint Server 2013 在安装前,是不能安装 VS 等会自动安装 .net 4.5.x 以上版本的 .net Framework 的软件的.因为安装了 .net Framewor ...

  6. python 进程池(multiprocessing.Pool)和线程池(threadpool.ThreadPool)的区别与实例

    一般我们是通过动态创建子进程(或子线程)来实现并发服务器的,但是会存在这样一些缺点: 1.动态创建进程(或线程)比较耗费时间,这将导致较慢的服务器响应.  2.动态创建的子进程通常只用来为一个客户服务 ...

  7. 隐藏input的光标

    https://segmentfault.com/q/1010000000684888 https://wap.didialift.com/beatles/campaign/driver/activi ...

  8. ligerUI---ligerGrid中treegrid(表格树)的使用

    写在前面: 表格树是在普通ligerGrid的基础上,做了一点改变,使数据以表格树的形式显示出来,适用于有级别的数据比如菜单(有父菜单,父菜单下面有子菜单).表格树的显示有两种方法,可以根据自己的项目 ...

  9. Ubuntu16 远程连接MySQL

    1.进入MySQL配置目录允许其他IP可以链接 vi /etc/mysql/mysql.conf.d/mysqld.cnf 吧下面这行注释掉 #bind-address = 127.0.0.1 2.远 ...

  10. Scrum Meeting Alpha - 7

    Scrum Meeting Alpha - 7 NewTeam 2017/11/1 地点:新主楼F座二楼 任务反馈 团队成员 完成任务 计划任务 安万贺 登录部分的API仍存在问题 完成登陆部分的AP ...