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个城堡并获得里面的宝物.但由于地理位置原因,有些城堡不能直接攻克,要攻克这些城堡必须先 ...
随机推荐
- kvm虚拟化管理平台WebVirtMgr部署-完整记录(安装Windows虚拟机)-(4)
一.背景说明 在之前的篇章中,提到在webvirtmgr里安装linux系统的vm,下面说下安装windows系统虚拟机的操作记录: 由于KVM管理虚拟机的硬盘和网卡需要virtio驱动,linux ...
- 两种方式判断类的存在→className getAttribute
通过className获取 var p = document.getElementsByTagName('p'); for(var i = 0;i <p.length;i++){ if(p[i] ...
- project和task
projects和tasks是Gradle中最重要的两个概念 任何一个Gradle构建狗屎一个或多个projects的组成.每个project包括许多可构建组成部分 什么是 project ? 一个j ...
- RDLC系列之二 子报表
本文实现简单的子报表 一.效果图
- linux输入输出重定向
http://www.cnblogs.com/chengmo/archive/2010/10/20/1855805.html 在Linux下,当一个用户进程被创建的时候,系统会自动为该进程创建三个数据 ...
- MTK android 工程中如何修改照片详细信息中机型名
每一个项目的机型名都不相同,因此拍出来的照片需要更改详细信息中的机型名. 那么,具体在哪里修改照片详细信息机型名呢 路径信息:/ALPS.JB3.TDD.MP.V2_TD_xxx/mediatek/c ...
- web前端开发资源整理
webpack中文文档 http://webpackdoc.com/index.html 饿了么UI http://mint-ui.github.io/#!/zh-cn http://element. ...
- Winform调用百度地图接口
using System; using System.IO; using System.Collections.Generic; using System.ComponentModel; using ...
- LeetCode:Climbing Stairs(编程之美2.9-斐波那契数列)
题目链接 You are climbing a stair case. It takes n steps to reach to the top. Each time you can either c ...
- iOS开发系列--扩展--播放音乐库中的音乐
众所周知音乐是iOS的重要组成播放,无论是iPod.iTouch.iPhone还是iPad都可以在iTunes购买音乐或添加本地音乐到音乐 库中同步到你的iOS设备.在MediaPlayer.fram ...