hdu 5410 - CRB and His Birthday -- 完全背包+0-1背包

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 kof 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 TT, indicating the number of test cases. For each test case:
The first line contains two integers M and N .
Then N lines follow, ii-th line contains three space separated integers Wi , Aiand BiBi.
 
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 的money去给他买生日礼物 ,妈妈去的商店里有 N 种商品,每种商品的个数是无限多的,
    且每种商品的价格为 Wi ,已知,每买 x 个第 i 种商品,商店的店主会送 Ai*x+Bi个糖果,小明的妈妈想要得到最多的糖果,
    请问小明的妈妈该如何选择买商品,当然,如果不买第 i 个商品的话,店主不会送糖

分析:
  通过题目可以知道,每种商品的个数为 无限个,很像是完全背包问题,但是但是在 每次的选择中,如果买了某商品,就会送一个 Bi,而且是不论买多少该商品
  都只是送 1 个
  但如果买了 x 个该商品,会送 x*Ai 个 Ai
  【最近在学背包问题】:这是一个 0-1 背包与 完全背包组合的一道题目
   将该问题拆分为 0-1 和 完全背包来做。
  在 0-1 背包的时候,价值为 Ai+Bi
  在多重背包的时候, 价值为 Ai
  根据两种不同的情况来选择背包解体方案

代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <map>
#include <vector>
#include <stack>
#include <iostream>
#include <string>
#define Pair pair<int,int>
#define INF 0x3f3f3f3f
#define NINF 0xc0c0c0c0
#define maxn 10000
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;

int W[maxn],value[maxn];
int dp[maxn];
int main()
{
    int M,N;
    int t;scanf("%d",&t);while(t--){
        scanf("%d%d",&M,&N);
        int ai,bi,wi;
        for(int i = 1;i <= N;++i){
            scanf("%d%d%d",&wi,&ai,&bi);
            W[i] = wi;value[i] = ai+bi; //0-1背包问题
            W[i+N] = wi;value[i+N] = ai; //多重背包问题
        }
        mem(dp,0);
        for(int i = 1;i <= N;++i){
            for(int j = M;j >= W[i];j--){
                dp[j] = max(dp[j],dp[j-W[i]]+value[i]);
            }
        }
        for(int i = 1+N;i <= N+N;++i){
            for(int j = W[i];j <= M;j++){
                dp[j] = max(dp[j],dp[j-W[i]]+value[i]);
            }
        }
        printf("%d\n",dp[M]);
    }
    return 0;
}

 










posted @ 2017-04-20 15:44  渣渣技术狗  阅读(75)  评论(0)    收藏  举报