依赖背包 事实上,这是一种树形DP,其特点是每个父节点都需要对它的各个儿子的属性进行一次DP以求得自己的相关属性。

fj打算去买一些东西,在那之前,他需要一些盒子去装他打算要买的不同的物品。每一个盒子有特定要装的东西(就是说如果他要买这些东西里的一个,他不得不先买一个盒子)。每一种物品都有自己的价值,现在FJ只有W元去购物,他打算用这些钱买价值最高的东西。

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件物品,对应有不同的价格和价值,这是典型的01背包。但现在有了一个限制,要买物品先买能装这件物品的特定的盒子,盒子的价值为0

代码理解得还不是太好,感觉这是一个“二重”的01背包。首先假设先买第i个盒子,对每个盒子里的物品进行一次01背包;然后对盒子再进行一次01背包,决策到底要不要买这个盒子

dp[i][j]表示前i个盒子有j元钱能获得的最大价值,则所求就是dp[n][total]

因为物品对盒子有了“依赖”,所以要先对dp赋值为-1,表示买不到盒子就更不可能装物品

代码:

 #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <iostream>
using namespace std; #define N 100010
int dp[][N]; int main()
{
int n,total;
while(~scanf("%d%d",&n,&total)){
memset(dp,-,sizeof(dp)); //有依赖关系,要赋初值-1
memset(dp[],,sizeof(dp[]));
for(int i=;i<=n;i++){
int box,m;
scanf("%d%d",&box,&m);
for(int j=box;j<=total;j++)
dp[i][j]=dp[i-][j-box];//先让i层买盒子,因为盒子没有价值,
//所以直接等于上一层的花费+盒子钱
for(int j=;j<m;j++){//在已花费盒子钱的基础上,此时再对dp[i]层做01背包,
//即i层一个盒子多种物品的最大价值
int c,w;
scanf("%d%d",&c,&w);
for(int k=total;k>=c;k--){
if(dp[i][k-c]!=-)//注意依赖背包有不可能的情况,这里即k买不到盒子和这个物品,
//不能装物品
dp[i][k]=max(dp[i][k],dp[i][k-c]+w);// 这里不能dp[i][k]=max(dp[i][j],dp[i][k-box-c]+w)
//因为已经买过盒子了,这个表达式代表一个盒子基础上一个物品带一个盒子
}
}
for(int j=;j<=total;j++)//决策是否买第i个盒子
dp[i][j]=max(dp[i][j],dp[i-][j]);//不要忘了考虑不选当前组的情况(不是必选)
}
printf("%d\n",dp[n][total]);
}
return ;
}

参考:http://blog.csdn.net/yan_____/article/details/8539745

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

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

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

  2. hdu 3449 有依赖性的01背包

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=3449  Consumer Description FJ is going to do so ...

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

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

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

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

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

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

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

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

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

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

  8. HDU 1011 Starship Troopers【树形DP/有依赖的01背包】

    You, the leader of Starship Troopers, are sent to destroy a base of the bugs. The base is built unde ...

  9. hdu 2639 第k大01背包

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

随机推荐

  1. Hadoop-2.2.0中文文档—— MapReduce下一代- 可插入的 Shuffle 和 Sort

    简单介绍 可插入的 shuffle 和 sort 功能,同意在shuffle 和 sort 逻辑中用可选择的实现类替换.这个情况的样例是:用一个不是HTTP的应用协议,如RDMA来 shuffle 从 ...

  2. UITabBarController详细说明(简介和设置)

    [objc] view plaincopy - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions ...

  3. TabHost+RadioGroup搭建基础布局

    xml的形势如下: <tabhost> <linearlayout vertival> <framlayout weight=1/> <tabwidget g ...

  4. 采用Sambaserver由win平台,linux平台上传文件

    1.构造yum [root@db /]# cd /etc/yum.repos.d/ [root@db yum.repos.d]# vi yum.repo --改动光盘挂载位置,enabled设置为启动 ...

  5. 基本介绍LINUX远程PC软件:PUTTY、SecureCRT、X-Manager

    ***********************************************声明************************************************ 原创 ...

  6. Android - match_parent 和 fill_parent差异

    Android - match_parent 和 fill_parent差异 本文地址: http://blog.csdn.net/caroline_wendy match_parent 和 fill ...

  7. iPhone发展【一】从HelloWorld开始

    转载请注明出处.原文网址:http://blog.csdn.net/m_changgong/article/details/8013553 作者:张燕广 从经典的HelloWorld開始踏入iPhon ...

  8. UVa10000_Longest Paths(最短路SPFA)

    解题报告 求最长路. 用SPFA求最长路,初始化图为零,dis数组也为零 #include <iostream> #include <cstdio> #include < ...

  9. JAVA设计模式(08):结构化-飞锤(Flyweight)

    当前咱们国家正在大力倡导构建和谐社会,当中一个非常重要的组成部分就是建设资源节约型社会,"浪费可耻,节俭光荣". 在软件系统中,有时候也会存在资源浪费的情况,比如在计算机内存中存储 ...

  10. LeetCode :: Convert Sorted Array (link list) to Binary Search Tree [tree]

    1.Given an array where elements are sorted in ascending order, convert it to a height balanced BST. ...