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(有依赖的背包)的更多相关文章

  1. RONOJ 6今明的预算方案(有依赖的背包)

    题目描述 金明今天很开心,家里购置的新房就要领钥匙了,新房里有一间金明自己专用的很宽敞的房间.更让他高兴的是,妈妈昨天对他说:“你的房间需要购买哪些物品,怎么布置,你说了算,只要不超过N元钱就行”.今 ...

  2. C. Coin Troubles 有依赖的背包 + 完全背包变形

    http://codeforces.com/problemset/problem/283/C 一开始的时候,看着样例不懂,为什么5 * a1 + a3不行呢?也是17啊 原来是,题目要求硬币数目a3 ...

  3. 有依赖的背包---P1064 金明的预算方案

    P1064 金明的预算方案 solution 1 暴搜 70pt dfs (当前搜到了第几个物品,产生的总价值,剩下多少钱) 剪枝 1:如果剩下的钱数<0,直接return就好,没必要继续了 剪 ...

  4. AcWing 286. 选课 (树形依赖分组背包)打卡

    有依赖的背包 首先依赖的概念,就是一个东西依附与一个东西之上,我们想买附品的话必须要把主品先买下来,这个可以先做下这道题 https://www.cnblogs.com/Lis-/p/11047466 ...

  5. 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 ...

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

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

  7. Luogu1064 金明的预算方案 (有依赖的背包)

    枚举多个状态 #include <iostream> #include <cstdio> #include <cstring> #include <algor ...

  8. hdu4044 依赖背包变形 好题!

    由于不是求最大的可拦截的HP值,而是要将最小值最大化,那么就需要分配每个子树用的钱数以达到最小值最大化 第一步解决如何分配钱使得结点u的子树中用了j元钱后可以拦截的HP最大,这就是变形的分组(依赖)背 ...

  9. HDU 1561 The more, The Better【树形DP/有依赖的分组背包】

    ACboy很喜欢玩一种战略游戏,在一个地图上,有N座城堡,每座城堡都有一定的宝物,在每次游戏中ACboy允许攻克M个城堡并获得里面的宝物.但由于地理位置原因,有些城堡不能直接攻克,要攻克这些城堡必须先 ...

随机推荐

  1. vuejs使用技巧

    组件创建自定义标签时,自定义的标签不要用驼峰写法,vue否者报错,例如<my-template></my-template>合法或者去掉中间的斜杠全部小写,只要出现大写字母就会 ...

  2. JavaScript基于时间的动画算法

    转自:https://segmentfault.com/a/1190000002416071 前言 前段时间无聊或有聊地做了几个移动端的HTML5游戏.放在不同的移动端平台上进行测试后有了诡异的发现, ...

  3. 呵呵!手把手带你在 IIS 上运行 Python(转)

    原文:http://blog.csdn.net/yangzhencheng_001/article/details/40342449 公司的网站让我头痛死了.在众多前辈高手的带领下,一大堆的 CMD ...

  4. Apache Thrift

    Baidu Thrift  Google Thrift Apache Thrift - 可伸缩的跨语言服务开发框架

  5. Linux 网络编程六(socket通信UDP版)

    //udp接收消息 #include <stdio.h> #include <stdlib.h> #include <string.h> #include < ...

  6. no.5.print sum

    #-*-coding=utf-8-*- for a in range(1,50,1): for b in range(1,50,1): for c in range(1,50,1): if a+b+c ...

  7. R树空间索引

    R树在数据库等领域做出的功绩是非常显著的.它很好的解决了在高维空间搜索等问题.举个R树在现实领域中能够解决的例子吧:查找20英里以内所有的餐厅.如果没有R树你会怎么解决?一般情况下我们会把餐厅的坐标( ...

  8. Code First开发系列之管理并发和事务(转)

    转自:http://www.cnblogs.com/farb/p/ConcurrencyAndTransctionManagement.html 返回<8天掌握EF的Code First开发&g ...

  9. JS 之性能优化(2)

    继续上一篇的JS性能优化之后,下面接着讲关于前端性能优化的内容.如果有不对的地方欢迎纠正. 1.避免过多的重排与重绘操作. 尽量将DOM中的多个读操作放一起,中间不要插入写的操作,因为写操作会导致浏览 ...

  10. 《图解tcp/ip》读书笔记(二)

    <图解tcp/ip>读书笔记(二) 本周主要阅读的是本书的第三章--数据链路. 当然了,从某些角度讲,我认为这一章就是计算机网络的最基本的内容之一.整章讲述了数据链路层的作用和相关技术,主 ...