TOJ3540Consumer(有依赖的背包)
http://acm.tju.edu.cn/toj/showp3540.html3540. Consumer
Time Limit: 2.0 Seconds Memory Limit: 65536K
Total Runs: 136 Accepted Runs: 67
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 i-th 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
【题目大意】给若干组物品,每组物品都有一个箱子(箱子自身也有cost),然后就是物品的cost和value,要买某个物品必须也要买装这个物品的箱子,给一定钱数,问能获得的最大价值。
解题思路:对每个箱子的附件进行一次01背包,背包的容量是总花费-箱子的花费,得到的就是对应每个附件的最大价值;然后再对这个箱子进行一次01背包,箱子的价值dpbox[i-p]与不取这个箱子的价值dptotal[i]那个大取哪个,模模糊糊知道大体思路
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAX = + ;
int dptotal[MAX],dpbox[MAX];
int price,value,n,w,p,c,m;
int main()
{
while(scanf("%d%d", &n,&w) != EOF)
{
memset(dptotal, , sizeof(dptotal));
for(int i = ; i < n; i++)
{
scanf("%d%d",&p,&m);
memcpy(dpbox, dptotal, sizeof(dptotal));
for(int k = ; k <= m; k++)
{
scanf("%d%d",&price,&value); for(int j = w - p; j >= price; j--)
{
dpbox[j] = max(dpbox[j], dpbox[j - price] + value);
}
}
for(int k = w; k >= p; k--)
{
if(dptotal[k] < dpbox[k - p])
dptotal[k] = dpbox[k - p];
}
}
printf("%d\n",dptotal[w]);
}
return ;
}
TOJ3540Consumer(有依赖的背包)的更多相关文章
- RONOJ 6今明的预算方案(有依赖的背包)
题目描述 金明今天很开心,家里购置的新房就要领钥匙了,新房里有一间金明自己专用的很宽敞的房间.更让他高兴的是,妈妈昨天对他说:“你的房间需要购买哪些物品,怎么布置,你说了算,只要不超过N元钱就行”.今 ...
- C. Coin Troubles 有依赖的背包 + 完全背包变形
http://codeforces.com/problemset/problem/283/C 一开始的时候,看着样例不懂,为什么5 * a1 + a3不行呢?也是17啊 原来是,题目要求硬币数目a3 ...
- 有依赖的背包---P1064 金明的预算方案
P1064 金明的预算方案 solution 1 暴搜 70pt dfs (当前搜到了第几个物品,产生的总价值,剩下多少钱) 剪枝 1:如果剩下的钱数<0,直接return就好,没必要继续了 剪 ...
- AcWing 286. 选课 (树形依赖分组背包)打卡
有依赖的背包 首先依赖的概念,就是一个东西依附与一个东西之上,我们想买附品的话必须要把主品先买下来,这个可以先做下这道题 https://www.cnblogs.com/Lis-/p/11047466 ...
- CSU - 1580 NCPC2014 Outing(树形依赖+分组背包)
Outing Input Output Sample Input 4 4 1 2 3 4 Sample Output 4 分组背包: for 所有的组k for v=V..0 for 所有的i属于组k ...
- hdu 3449 Consumer (依赖01背包)
题目: 链接:pid=3449">点击打开链接 题意: 思路: dp[i][j]表示前i个箱子装j钱的材料可以得到的最大价值. 代码: #include<iostream> ...
- Luogu1064 金明的预算方案 (有依赖的背包)
枚举多个状态 #include <iostream> #include <cstdio> #include <cstring> #include <algor ...
- hdu4044 依赖背包变形 好题!
由于不是求最大的可拦截的HP值,而是要将最小值最大化,那么就需要分配每个子树用的钱数以达到最小值最大化 第一步解决如何分配钱使得结点u的子树中用了j元钱后可以拦截的HP最大,这就是变形的分组(依赖)背 ...
- HDU 1561 The more, The Better【树形DP/有依赖的分组背包】
ACboy很喜欢玩一种战略游戏,在一个地图上,有N座城堡,每座城堡都有一定的宝物,在每次游戏中ACboy允许攻克M个城堡并获得里面的宝物.但由于地理位置原因,有些城堡不能直接攻克,要攻克这些城堡必须先 ...
随机推荐
- jquery中的get和set
jquery中通过参数的个数来判断是get方法还是set方法: css: function(name, value ) { return value !== undefined ? jQuery.st ...
- Gradle多项目构建
1. 创建项目 首先创建项目,名称为 test: mkdir test && cd test gradle init 这时候的项目结构如下: ➜ test tree . ├── bui ...
- 【C#】窗体动画效果
通过调用API可以实现C#窗体的动画效果,主要调用user32.dll的行数AnimateWindow 1.函数申明 [System.Runtime.InteropServices.DllImport ...
- CentOS 6.5系统安装配置图解教程
说明: 截止目前CentOS 6.x最新版本为CentOS 6.5,下面介绍CentOS 6.5的具体安装配置过程 服务器相关设置如下: 操作系统:CentOS 6.5 64位 IP地址:192.16 ...
- listview向下滑动过程中背景色变成黑色和一些奇怪问题
ListView是一个经常要用到的android控件,现总结遇到过的一些美化的小细节. 1.listview在拖动的时候背景图片消失变成黑色背景,等到拖动完毕我们自己的背景图片才显示出来 这个问题是我 ...
- Linux常用指令---系统负载
查看linux系统负载: http://www.lupaworld.com/article-217011-1.html在Linux系统中,uptime.top等命令都会有系统平均负载load aver ...
- LeetCode:Jump Game I II
Jump Game Given an array of non-negative integers, you are initially positioned at the first index o ...
- MVC5 + EF6 + Bootstrap3 (8) HtmlHelper用法大全(上)
文章来源:Slark.NET-博客园 http://www.cnblogs.com/slark/p/mvc5-ef6-bs3-get-started-httphelper-part1.html 上一节 ...
- 使用jQuery加载html页面到指定的div
一.jQuery加载一个html页面到指定的div里 把a.html里面的某一部份的内容加载到b.html的一个div里.比如:加载a.html里面的<div id=“row"> ...
- 百度地图ip定位,不算bug的bug
做为一个入行不足两年的菜鸟,能在博客园写下第一篇博客,是需要多大的勇气啊.主要还是怕大神们喷啊.其次自己文笔实在太差了. 哈哈~还请各位大神,口下留情啊. 首先说下我的需求:一个需要城市分站的手机站. ...