HDOJ 5410 CRB and His Birthday DP背包

先跑一遍01背包,再跑一遍多重背包

CRB and His Birthday

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

Total Submission(s): 327    Accepted Submission(s): 177

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

/* ***********************************************
Author        :CKboss
Created Time  :2015年08月21日 星期五 13时29分32秒
File Name     :HDOJ5410.cpp
************************************************ */

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map>

using namespace std;

const int maxn=1100;

int m,n;
int w[maxn],A[maxn],B[maxn];
int dp[maxn*2];

int main()
{
	//freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);

	int T_T;
	scanf("%d",&T_T);
	while(T_T--)
	{
		scanf("%d%d",&m,&n);

		for(int i=1;i<=n;i++) scanf("%d%d%d",w+i,A+i,B+i);

		memset(dp,0,sizeof(dp));

		for(int i=1;i<=n;i++)
		{
			for(int j=m;j>=w[i];j--)
			{
				dp[j]=max(dp[j],dp[j-w[i]]+A[i]+B[i]);
			}
		}

		for(int i=1;i<=n;i++)
		{
			for(int j=w[i];j<=m;j++)
			{
				dp[j]=max(dp[j],dp[j-w[i]]+A[i]);
			}
		}

		printf("%d\n",dp[m]);
	}

    return 0;
}

版权声明:来自: 码代码的猿猿的AC之路 http://blog.csdn.net/ck_boss

时间: 2024-10-27 02:49:04

HDOJ 5410 CRB and His Birthday DP背包的相关文章

背包DP HDOJ 5410 CRB and His Birthday

题目传送门 题意:有n个商店,有m金钱,一个商店买x件商品需要x*w[i]的金钱,得到a[i] * x + b[i]件商品(x > 0),问最多能买到多少件商品 01背包+完全背包:首先x == 1时,得到a[i] + b[i],若再买得到的是a[i],那么x == 1的情况用01背包思想,x > 1时就是在01的基础上的完全背包.背包dp没刷过专题,这么简单的题也做不出来:( /************************************************* Author

hdu 5410 CRB and His Birthday(混合背包)

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.

hdu 5410 CRB and His Birthday 01背包和完全背包

#include<stdio.h> #include<string.h> #include<vector> #include<queue> #include<algorithm> using namespace std; int main() { int _,i,j,m,n,k,a[1024],b[1024],w[1024],dp[2048]; scanf("%d\n",&_); while(_--) { scanf(

HDU 1011 Starship Troopers(树形dp+背包)

Starship Troopers Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 13109    Accepted Submission(s): 3562 Problem Description You, the leader of Starship Troopers, are sent to destroy a base of

POJ 3132 Sum of Different Primes DP背包

http://poj.org/problem?id=3132 题意: 给定n和k,问用恰好k个不同的质数来表示n的方案数. 分析: n和k都很小.反正就是个背包,选k个物品恰好填满n即可. 1 #include<cstdio> 2 #include<cstring> 3 using namespace std; 4 5 bool dp[1200][15]; 6 int ct[1200][15]; 7 int p[1200]; 8 bool a[1200]; 9 int n, k,

二叉苹果树(树型DP+背包)

二叉苹果树 有一棵苹果树,如果树枝有分叉,一定是分2叉(就是说没有只有1个儿子的结点).这棵树共有N个结点(叶子点或者树枝分叉点),编号为1-N,树根编号一定是1. 我们用一根树枝两端连接的结点的编号来描述一根树枝的位置.下面是一颗有4个树枝的树: 2   5 \  / 3  4 \  / 1 现在这颗树枝条太多了,需要剪枝.但是一些树枝上长有苹果. 给定需要保留的树枝数量,求出最多能留住多少苹果. 程序名:apple 输入格式: 第1行2个数,N和Q(1<=Q<= N,1<N<=

构造 HDOJ 5414 CRB and String

题目传送门 题意:给两个字符串s,t,可以在s字符串任意位置后面插入字符c(与前面的不同),问是否能够将s转换为t字符串 构造:首先lens > lent 或者 s[1] != t[1] 一定是No,然后t最前面相同字符长度的部分在s中要相同,否则不能插入,之后的部分只要相同的部分全部存在,不同的部分可以随便插 /************************************************* Author        :Running_Time* Created Time

hdu1011 树形dp背包

http://acm.hdu.edu.cn/showproblem.php?pid=1011 Problem Description You, the leader of Starship Troopers, are sent to destroy a base of the bugs. The base is built underground. It is actually a huge cavern, which consists of many rooms connected with

hdoj 2046 骨牌铺方格 【DP】+【斐波那契】

dp果然不是好学的... 第n个,即2*n时,可由第n-1个的竖直排列再加一个,和第n-2个中横着排两个 所以f(n) = 1×f(n-1) + 1×f(n-2): 骨牌铺方格 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 28412    Accepted Submission(s): 13771 Problem Descripti