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): 1316    Accepted Submission(s): 640


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.
题意:有n块钱和m种商品,每种商品可以买无数次,总共可以的得到Ai*买的次数+Bi的糖果

问你最多可以得到多少糖果

思路:注意此题顶多只能O(n²)的复杂度,拆成二进制啊什么的就不要想了。

本题有两种解法,先说第一种。我们假设

dp[i][j][0]表示到第i种商品,花了j块钱,并且不买第i种商品所能得到的最多糖果数

dp[i][j][1]表示到第i种商品,花了j块钱,并且买第i种商品所能得到的最多糖果数

那么很容易可以得到转移方程

dp[i][j][0]=max(dp[i-1][j][0],dp[i-1][j][1])

dp[i][j][1]=max(dp[i][j-w][1]+ai,dp[i][j-w][0]+ai+bi)

这样此问题就变成了一个类似完全背包的问题。

第二种解法是把问题转换为混合背包,在网上看到这种思路时真的惊到了= = 直接把一个看似很复杂的问题转换为模板题了!

这个方法是这样子的,对于一种物品,我们把它分解成两种物品,两种物品的费用相同,但是第一种物品的价值是ai+bi,第二种物品ai

并且我们保证第一种物品最多只能取一件(01背包),第二种物品无数件(完全背包)

有人可能会问,如何保证取第二种物品的时候第一种一定取了呢?

我们可以想,同样一件物品,费用都相同但是第一种物品的价值更高,我们会去取第二件物品的话,第一件难道不是必然已经取了嘛?

代码1:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define N 1010
int dp[N][2*N][2];
struct P
{
    int w,a,b;
} p[N];
int main()
{
    int T,n,m;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d %d",&n,&m);
        for(int i=1;i<=m;i++)
            scanf("%d %d %d",&p[i].w,&p[i].a,&p[i].b);
        memset(dp,0,sizeof(dp));
        for(int i=1;i<=m;i++)
        {
            for(int j=0;j<=n;j++)
                    dp[i][j][0]=max(dp[i-1][j][0],dp[i-1][j][1]);
            for(int j=p[i].w;j<=n;j++)
                dp[i][j][1]=max(dp[i][j-p[i].w][1]+p[i].a,dp[i][j-p[i].w][0]+p[i].a+p[i].b);
        }
        int maxn=-1;
        for(int i=0;i<=n;i++)
        {
            maxn=max(maxn,dp[m][i][0]);
            maxn=max(maxn,dp[m][i][1]);
        }
        printf("%d\n",maxn);
    }
    return 0;
}

代码二:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define N 1010
int dp[2*N];
struct P
{
    int w,v;
} p1[N],p2[N];
int main()
{
    int T,n,m;
    int w,a,b;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d %d",&n,&m);
        for(int i=1; i<=m; i++)
        {
            scanf("%d %d %d",&w,&a,&b);
            p1[i].w=w;
            p1[i].v=a+b;
            p2[i].w=w;
            p2[i].v=a;
        }
        memset(dp,0,sizeof(dp));
        for(int i=1; i<=m; i++)
            for(int j=n;j>=p1[i].w;j--)
            dp[j]=max(dp[j],dp[j-p1[i].w]+p1[i].v);
        for(int i=1; i<=m; i++)
            for(int j=p2[i].w;j<=n;j++)
            dp[j]=max(dp[j],dp[j-p2[i].w]+p2[i].v);
        int maxn=-1;
        for(int i=1;i<=n;i++)
            maxn=max(maxn,dp[i]);
        printf("%d\n",maxn);
    }
    return 0;
}




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值