题目链接: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+;
const int INF=0x3f3f3f3f; int dp[maxn];
int temp[maxn]; int main()
{
int N,P;
while(scanf("%d%d",&N,&P)==)
{
memset(dp,,sizeof(dp));
for(int i=; i<=N; i++)
{
int p,m;
scanf("%d%d",&p,&m);
memset(temp,-,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]!=-)
temp[k]=max(temp[k],temp[k-a]+b);
}
for(int k=; k<=P; k++)
dp[k]=max(dp[k],temp[k]);
}
printf("%d\n",dp[P]);
}
return ;
}

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

hdu 3449 有依赖性的01背包的更多相关文章

  1. hdu 3449 Consumer (依赖01背包)

    题目: 链接:pid=3449">点击打开链接 题意: 思路: dp[i][j]表示前i个箱子装j钱的材料可以得到的最大价值. 代码: #include<iostream> ...

  2. HDu 3449 (有依赖的01背包) Consumer

    题意: 有n件物品,对应有不同的价格和价值,这是典型的01背包.但现在有了一个限制,要买物品先买能装这件物品的特定的盒子,盒子的价值为0 代码理解得还不是太好,感觉这是一个“二重”的01背包.首先假设 ...

  3. hdu 3449 (有依赖的01背包)

    依赖背包 事实上,这是一种树形DP,其特点是每个父节点都需要对它的各个儿子的属性进行一次DP以求得自己的相关属性. fj打算去买一些东西,在那之前,他需要一些盒子去装他打算要买的不同的物品.每一个盒子 ...

  4. HDU 5234 Happy birthday --- 三维01背包

    HDU 5234 题目大意:给定n,m,k,以及n*m(n行m列)个数,k为背包容量,从(1,1)开始只能往下走或往右走,求到达(m,n)时能获得的最大价值 解题思路:dp[i][j][k]表示在位置 ...

  5. HDOJ(HDU).3466 Dividing coins ( DP 01背包 无后效性的理解)

    HDOJ(HDU).3466 Dividing coins ( DP 01背包 无后效性的理解) 题意分析 要先排序,在做01背包,否则不满足无后效性,为什么呢? 等我理解了再补上. 代码总览 #in ...

  6. HDOJ(HDU).2546 饭卡(DP 01背包)

    HDOJ(HDU).2546 饭卡(DP 01背包) 题意分析 首先要对钱数小于5的时候特别处理,直接输出0.若钱数大于5,所有菜按价格排序,背包容量为钱数-5,对除去价格最贵的所有菜做01背包.因为 ...

  7. HDOJ(HDU).2602 Bone Collector (DP 01背包)

    HDOJ(HDU).2602 Bone Collector (DP 01背包) 题意分析 01背包的裸题 #include <iostream> #include <cstdio&g ...

  8. HDU 1864 最大报销额 0-1背包

    HDU 1864 最大报销额 0-1背包 题意 现有一笔经费可以报销一定额度的发票.允许报销的发票类型包括买图书(A类).文具(B类).差旅(C类),要求每张发票的总额不得超过1000元,每张发票上, ...

  9. hdu 2639 第k大01背包

    求每个状态里的k优解,然后合并 /* HDU 2639 求01背包的第k大解. 合并两个有序序列 */ #include<stdio.h> #include<iostream> ...

随机推荐

  1. vs2010调试程序出现“Cannot find or open the PDB file”

    项目中源程序编写好以后, (一个简单的小程序) #include int main(void) { int age; int day; age = 24; printf("tom is %d ...

  2. codeforces 499A.Inna and Pink Pony 解题报告

    题目链接:http://codeforces.com/problemset/problem/499/A 题目意思:有两种按钮:1.如果当前观看的时间是 t,player 可以自动处理下一分钟,姑且理解 ...

  3. 类中实现 Dispose And Finalize

    1.Dispose方法中,应该使用GC.SuppressFinalize防止GC调用Finalize方法,因为显示调用Dispose比较好. 2.Disposed字段保证了两次调用Dispose方法不 ...

  4. supersr--addSubview和 insertSubView 区别

    A addSubview B  是将B直接覆盖在A的最上层  例子: [self.view addSubview:scrollView]; A insertSubView B AtIndex:2 是将 ...

  5. 解决eclipse manven项目添加不了maven dependencis

    <classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER"& ...

  6. hdu 2087剪花布条

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2087 思路:正常KMP求解aaaaaa  aa得到的结果是6,这题是3.仅仅改一点代码就行 当匹配完之 ...

  7. 利用drozer进行Android渗透测试

    一.安装与启动 1. 安装 第一步:从 http://mwr.to/drozer 下载Drozer (Windows Installer) 第二步:在 Android 设备中安装 agent.apk ...

  8. Jmeter 函数

    一._csvRead 函数 _cvsRead函数是从外部读取参数,csvRead函数可以从一个文件中读取多个参数. 步骤: 1.先新建一个文件,例如c.txt,里面的数据存放为 web@qq.com, ...

  9. Linux安装mysql最新版本纪要

    http://blog.csdn.net/frt007/article/details/50184143 http://blog.csdn.net/wb96a1007/article/details/ ...

  10. 攻城狮在路上(叁)Linux(十九)--- 磁盘分区

    磁盘分区使用fdisk命令,该命令仅root有使用权限. 磁盘分区的一般步骤: 1.首先用df命令查看设备名 df -h; 2.fdisk /dev/sda; <== 按m显示help d:删除 ...