HDU 5410 CRB and His Birthday(DP)

CRB and His Birthday

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

Total Submission(s): 83    Accepted Submission(s): 45

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

点击打开链接

#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

using namespace std;

const int N = 2010;

int dp[N];
int p[N],w[N],k[N];
int n,m;
int flag[N][N];

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&m);
        for(int i=0; i<m; i++)
        {
            scanf("%d%d%d",&p[i],&w[i],&k[i]);
        }
        int maxx = 0;
        memset(flag,0,sizeof(flag));
        memset(dp,0,sizeof(dp));
        for(int i=0; i<m; i++)
        {
            for(int j=n; j>=p[i]; j--)
            {
                dp[j] = max(dp[j],dp[j-p[i]]+w[i]+k[i]);
                maxx = max(dp[j],maxx);
            }
        }
        for(int i=0;i<m;i++){
            for(int j=p[i];j<=n;j++){
                dp[j] = max(dp[j],dp[j-p[i]]+w[i]);
                maxx = max(dp[j],maxx);
            }
        }
        printf("%d\n",maxx);
    }

    return 0;
}

版权声明:本文为博主原创文章,如有特殊需要请与博主联系 QQ : 793977586。

时间: 2024-08-02 07:01:26

HDU 5410 CRB and His Birthday(DP)的相关文章

hdu 5410 CRB and His Birthday(动态规划)

题目: CRB and His Birthday Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 748    Accepted Submission(s): 395 Problem Description Today is CRB's birthday. His mom decided to buy many presents for

hdu 1024 Max Sum Plus Plus(DP)

转移方程dp[i][j]=Max(dp[i][j-1]+a[j],max(dp[i-1][k] ) + a[j] ) 0<k<j 此链接中有详解点击打开链接 #include<stdio.h> #include<algorithm> #include<iostream> using namespace std; #define MAXN 1000000 #define INF 0x7fffffff int dp[MAXN+10]; int mmax[MAXN

HDU 4669 Mutiples on a circle(DP)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4669 题意:给出一个长度为n的数字环A和数字m.问有多少子串(连续)使得这些子串的数字拼在一起是m的倍数? 思路:首先计算A[1]和A[n]不在一起的.这个简单,只要记录f[i][j]表示到第i个数字余数为j的个数即可,然后: 接着计算a[1]和a[n]在一起的情况. 我们首先预处理end[i]表示数字子串A[i,n]连在一起对m的余数.然后枚举i从[1,n-1],那么数字设数字串[1,i]的长度为

【HDU】 1160 FatMouse&#39;s Speed (DP)

一开始写的dfs进行记忆化结果不知道怎么进行路径的记录...改成循环就好了 dp[i] = max(dp[j]) + 1 , weight[j] < weight[j] && speed[j] > speed[i] 一开始进行一次排序使得重量递增,这样只需要考虑速度就好了 #include<cstdio> #include<algorithm> using namespace std; const int maxn = 10005; struct Mou

HDU 1231:最大连续子序列(DP)

最大连续子序列 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 18461    Accepted Submission(s): 8202 Problem Description 给定K个整数的序列{ N1, N2, ..., NK },其任意连续子序列可表示为{ Ni, Ni+1, ..., Nj },其中 1 <= i <= j

HDU 1712 ACboy needs your help(DP)

Problem Description ACboy has N courses this term, and he plans to spend at most M days on study.Of course,the profit he will gain from different course depending on the days he spend on it.How to arrange the M days for the N courses to maximize the

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

对于每个物品,如果购买,价值为A[i]*x+B[i]的背包问题. 先写了一发是WA的= =.代码如下: 1 #include <stdio.h> 2 #include <algorithm> 3 #include <string.h> 4 #include <set> 5 using namespace std; 6 typedef pair<int,int> pii; 7 8 pii dp[2005]; 9 int w[1005],A[1005

HDU 5410 CRB and His Birthday (01背包,完全背包,混合)

题意:有n种商品,每种商品中有a个糖果,如果买这种商品就送多b个糖果,只有第一次买的时候才送.现在有m元,最多能买多少糖果? 思路:第一次买一种商品时有送糖果,对这一次进行一次01背包,也就是只能买一次.然后对这种商品来一次完全背包,此时不送糖果,也可以多买. 1 #include <bits/stdc++.h> 2 #define pii pair<int,int> 3 #define INF 0x7f7f7f7f 4 #define LL long long 5 using n

HDU 5410 CRB and His Birthday (2015年多校比赛第10场)

1.题目描述:点击打开链接 2.解题思路:本题是完全背包问题的一种变形.根据题意描述,每种物品的价值随着A[i]是线性变化的,但是并不随着B[i]线性变化,B[i]仅仅是在第一次挑选第i件物品是才算入,其他时候均不算入.因此,这里的状态要比普通的完全背包增加一个维度:是否是第一次选第i件物品,即用(i,j,flag)表示当前背包容量为j时,是否为第一次选第i件物品时的最大价值.那么不难得到如下状态转移方程: dp(i+1,j,0)=max{dp(i,j,0),dp(i,j,1)}; dp(i+1