码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 3449 有依赖性的01背包

时间:2016-11-07 19:35:10      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:size   get   sign   pst   test   tput   end   http   nbsp   

题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=3449

 

                           Consumer

 

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


 

大意:在购买一类物品之前,必须买另一种物品。

本题是购买袋子,有体积,但是没有价值。

AC代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
#define ll long long
const int maxn=1e5+5;
const int INF=0x3f3f3f3f;

int dp[maxn];
int temp[maxn];

int main()
{
    int N,P;
    while(scanf("%d%d",&N,&P)==2)
    {
        memset(dp,0,sizeof(dp));
        for(int i=1; i<=N; i++)
        {
            int p,m;
            scanf("%d%d",&p,&m);
            memset(temp,-1,sizeof(temp));
            for(int k=p; k<=P; k++)
                temp[k]=dp[k-p];
            while(m--)
            {
                int a,b;
                scanf("%d%d",&a,&b);
                for(int k=P; k>=a; k--)
                    if(temp[k-a]!=-1)
                        temp[k]=max(temp[k],temp[k-a]+b);
            }
            for(int k=1; k<=P; k++)
                dp[k]=max(dp[k],temp[k]);
        }
        printf("%d\n",dp[P]);
    }
    return 0;
}

一个写的好几种方法的博客:http://www.cnblogs.com/wuyiqi/archive/2011/11/26/2264283.html

hdu 3449 有依赖性的01背包

标签:size   get   sign   pst   test   tput   end   http   nbsp   

原文地址:http://www.cnblogs.com/a-clown/p/6040268.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!