hdoj 3449 Consumer 【依赖背包】



Consumer

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/65536 K (Java/Others)
Total Submission(s): 1751    Accepted Submission(s): 905


Problem Description
FJ is going to do some shopping, and before that, he needs some boxes to carry the different kinds of stuff he is going to buy. Each box is assigned to carry some specific kinds of stuff (that is to say, if he is going to buy one of these stuff, he has to buy the box beforehand). Each kind of stuff has its own value. Now FJ only has an amount of W dollars for shopping, he intends to get the highest value with the money.
 

Input
The first line will contain two integers, n (the number of boxes 1 <= n <= 50), w (the amount of money FJ has, 1 <= w <= 100000) Then n lines follow. Each line contains the following number pi (the price of the ith box 1<=pi<=1000), mi (1<=mi<=10 the number goods ith box can carry), and mi pairs of numbers, the price cj (1<=cj<=100), the value vj(1<=vj<=1000000)
 

Output
For each test case, output the maximum value FJ can get
 

Sample Input
      
      
3 800 300 2 30 50 25 80 600 1 50 130 400 3 40 70 30 40 35 60
 

Sample Output
      
      
210
 



题意:给定n个盒子以及每个盒子可以装的物品,买物品必须先买其对应的盒子。现在已知盒子的价格、所有物品的价格和价值,给你w元,问能够得到的最大价值。


思路:依赖背包,先把盒子及它所能装的物品看做一组。用dp[i]记录使用i元得到的最大价值。

对每一组(盒子价格为p,物品价格c, 价值v),先求一次0-1背包。Mval[j] = max(Mval[j], Mval[j-c]+v),则Mval[i]表示处理到当前组(需要把dp数组的值copy给Mval) 使用i元所能得到的最大价值(不涉及盒子的费用)。

下面的操作需要把当前组盒子的费用考虑到,也就是说对于我们当前更新的dp[],需要空出p的价格来购买盒子,这样更新 -> dp[j] = max(dp[j], dp[j-p])。


AC代码:



#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <vector>
#define INF 0x3f3f3f3f
#define eps 1e-8
#define MAXN 500000+10
#define MAXM 50000000
#define Ri(a) scanf("%d", &a)
#define Rl(a) scanf("%lld", &a)
#define Rs(a) scanf("%s", a)
#define Pi(a) printf("%d\n", (a))
#define Pl(a) printf("%lld\n", (a))
#define Ps(a) printf("%s\n", (a))
#define W(a) while(a--)
#define CLR(a, b) memset(a, (b), sizeof(a))
#define MOD 9901
#define LL long long
using namespace std;
LL Mval[MAXN];
LL dp[MAXN];
int main()
{
    int n, w;
    while(scanf("%d%d", &n, &w) != EOF)
    {
        CLR(dp, 0);
        for(int i = 1; i <= n; i++)
        {
            int p, num;
            Ri(p); Ri(num);
            memcpy(Mval, dp, sizeof(dp));
            for(int j = 1; j <= num; j++)
            {
                int c; LL v;
                Ri(c); Rl(v);
                for(int k = w-p; k >= c; k--)
                    Mval[k] = max(Mval[k], Mval[k-c]+v);
            }
            for(int j = w; j >= p; j--)
                dp[j] = max(dp[j], Mval[j-p]);
        }
        Pl(dp[w]);
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值