依赖背包 事实上,这是一种树形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

最新文章

  1. z-stack协议uart分析(DMA)
  2. HDU 4941 Magical Forest(2014 Multi-University Training Contest 7)
  3. iOS阶段学习第一天笔记(Mac终端的操作)
  4. C# WebService动态调用
  5. js鲸鱼
  6. 聊聊IO多路复用之select、poll、epoll详解
  7. 推荐的PHP编码规范
  8. windows程序消息机制(Winform界面更新有关)
  9. Java实战之02Hibernate-03Session中的常用方法
  10. [OpenJudge] 平方和
  11. Android开发 - ActivityLifecycleCallbacks用法初探
  12. Effective C++笔记05:实现
  13. Java中获取路径的方法_自我分析
  14. 好公司、行业、领导?应届生应根据什么选offer?
  15. from sys import argv
  16. Flask web 开发出现错误:TypeError: Allowed methods have to be iterables of strings, for example: @app.route(..., methods=[&quot;POST&quot;])
  17. oracle 11g空表不能exp导出问题解决方案
  18. vscode sass live compiler
  19. [Tensorflow] Object Detection API - retrain mobileNet
  20. sqlmap的使用

热门文章

  1. 国内PaaS概述和EEPlat定位
  2. Hosting WCF Service
  3. C#访问Java的WebService添加SOAPHeader验证的问题
  4. 【C语言探索之旅】 第一部分第四课第一章:变量的世界之内存那档事
  5. Python学习笔记20:server先进
  6. QrcodeWithLogo
  7. 讲座:采用Store检查邮件(1)
  8. UiAutomator喷射事件的源代码分析
  9. Django架设blog步骤(转)
  10. [ACM] HDU 1227 Fast Food (经典Dp)