hdu 5410 CRB and His Birthday 背包问题 2015 Multi-University Training Contest 10 ★★★

CRB and His Birthday

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 852    Accepted Submission(s): 450


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   |   We have carefully selected several similar problems for you:   5431  5430  5429  5428  5427 


二十多天以前,就是在这场多校比赛中,我们队爆蛋了,今天终于报仇雪恨了奋斗
上次三个人五个小时想不出来的题目,我今天十几分钟就想出来了,哼。

首先,不能暴力,而且对付这种 有限容量,放占用容量的东西去求最大价值的问题 有固定的解决办法,那就是背包。
当时就是想不通,怎么保证购买了某种商品只加一个B,当时想到把买某一个商品,第一个价值为A+B(只有1个卖),
之后为A(有无限个卖),
但是死活认为不能保证只有买了第一个(价值A+B),才能买后面的(A)。
其实很简单,背包肯定是要用的,那我们就去更多的找定的规律,

首先,这个背包 与 物品选择的先后无关,不像之前有一道题。(那么不用考虑整体的贪心,即物品与物品之间的顺序)
那么对于某种物品,我们可以更多的向贪心(定)的方向去想。//对于其它题目  也可以试试对所有物品的贪心,找出规律
你会发现,第一件物品(A+B),和后面的(A)花费相同,但是价值不同。
我么可以试着,把(A+B)先加入递推考虑,再加入(A) ,既然花费一样,那么v的取值范围就应该一样(在这个范围内考虑dp[v]=max(dp[v-cost[x]],....))  ,v-cost[x]也一样。
如果A+B这个价值大的都不用买(没有更新dp值),那么与它同样花费  却比它 价值小的(A)就更不能更新dp了,
所以说(按照背包解法)如果A+B没有买,A就自然不会买
如果A+B买了,A可能买也可能不买。(由此保证符合实际情况)

对于某种物品
先进行01背包(A+B),再进行完全背包(A),这就是解法。
其实先进行 完全背包(A),再进行01背包(A+B),也正确(其实不影响)。


const int INF =0x3f3f3f3f;
const int maxn= 1000+10    ;
const int maxm= 2000+10   ;
//by yskysker123
int cost[maxn],val[maxn],add[maxn];
int dp[maxm],m,n;


void zerop(int x)
{
    int c=cost[x],va=val[x]+add[x];
    for(int v=m;v>=c;v--)
    {
        dp[v]=max(dp[v],dp[v-c ]+va);
    }
}

void full(int x)
{
    for(int v=cost[x];v<=m;v++)
    {
        dp[v]=max(dp[v],dp[v-cost[x]]+val[x]);
    }
}

int main()
{
    int T;scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&m,&n);
        for(int i=1;i<=n;i++)
        {
            scanf("%d%d%d",&cost[i],&val[i],&add[i]);
        }
        memset(dp,0,sizeof dp);
        for(int i=1;i<=n;i++)
        {
            zerop(i);
            full(i);
        }
        printf("%d\n",dp[m]);


    }


    return 0;
}



 

#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<climits>
#include<queue>
#include<vector>
#include<map>
#include<sstream>
#include<set>
#include<stack>
#include<utility>
#pragma comment(linker, "/STACK:102400000,102400000")
#define PI 3.1415926535897932384626
#define eps 1e-10
#define sqr(x) ((x)*(x))
#define FOR0(i,n)  for(int i=0 ;i<(n) ;i++)
#define FOR1(i,n)  for(int i=1 ;i<=(n) ;i++)
#define FORD(i,n)  for(int i=(n) ;i>=0 ;i--)
#define  lson   num<<1,le,mid
#define rson    num<<1|1,mid+1,ri
#define MID   int mid=(le+ri)>>1
#define zero(x)((x>0? x:-x)<1e-15)
#define mk    make_pair
#define _f     first
#define _s     second

using namespace std;
//const int INF=    ;
typedef long long ll;
//const ll inf =1000000000000000;//1e15;
//ifstream fin("input.txt");
//ofstream fout("output.txt");
//fin.close();
//fout.close();
//freopen("a.in","r",stdin);
//freopen("a.out","w",stdout);

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值