HDU 3449 Consumer(有依赖背包)

Consumer

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

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


Source
2010 ACM-ICPC Multi-University Training Contest(2)——Host by BUPT
依赖背包。《背包九讲》的第七讲讲了这个。 
设dp[i][j]为前i个箱子使用金额j获得的最大价值。 
考虑每个箱子内部就是一个01背包,那么在枚举每一个箱子的时候,使用上一轮能满足本轮箱子费用的状态做一个01背包,然后本轮结束后更新dp数组。 
最后的ans就是dp[n][W] 
需要深刻理解一下这个依赖关系。

题意:每种类型的物品要一个箱子中,并且每个箱子都得花钱买,问最终卖得的物品的最大价值。

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
int dp[55][100010];
int n,t,nu,num;
int main()
{
    int i,j,k;
    while(scanf("%d%d",&n,&t)!=EOF)
    {
        memset(dp,0,sizeof(dp));
        for(i=1;i<=n;i++)
        {
            scanf("%d%d",&nu,&num);
            for(j=0;j<=nu;j++)///这个盒子的价格是q的时候,当前组下的所有比q容量小的背包都买不了
                dp[i][j]=-1;
            for(k=t;k>=nu;k--)///比q容量大的背包要根据上一组的背包来买,注意这里买盒子是不能获得相应的价值的
                dp[i][k]=dp[i-1][k-nu];
            for(j=0;j<num;j++)
            {
                int a,b;
                scanf("%d%d",&a,&b);
                for(k=t;k>=a;k--)
                {
                    if(dp[i][k-a]!=-1)
                        dp[i][k]=max(dp[i][k],dp[i][k-a]+b);
                }
            }
            for(j=0;j<=t;j++)///01
                dp[i][j]=max(dp[i-1][j],dp[i][j]);
        }
        printf("%d\n",dp[n][t]);
    }
    return 0;
}
posted @ 2017-11-18 21:28  南风古  阅读(118)  评论(0编辑  收藏  举报