背包系列 hdu3449 有依赖背包
这道题真正困扰了笔者3,4天,冥思苦想几日无果之后,只能去找大牛的解法。结合网上的大牛解法与自己的理解,笔者终于解决了这个坑了,在此小庆幸一下。
原题如下:
Consumer
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/65536 K (Java/Others)
Total Submission(s): 2154 Accepted Submission(s): 1157 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
AC代码如下:其中dp使用了滚动数组。
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int dp[][];
int main()
{
int n,w;
while(~scanf("%d%d",&n,&w))
{
memset(dp,,sizeof(dp));
for(int i=;i<n;++i)
{
int pi,mi;
scanf("%d%d",&pi,&mi);
//这里做了个假设,假设dp[1][j]组必须加本轮的箱子
//得到结果后和上一轮的结果比
for(int j=;j<=w-pi;++j)
dp[][j+pi] = dp[][j];
for(int j=;j<mi;++j)
{
int cj,wj;
scanf("%d%d",&cj,&wj);
//当v-cj小于pi,表示连箱子都买不起
for(int v=w;v-cj>=pi;--v)
{
//01背包
dp[][v] = max(dp[][v],dp[][v-cj]+wj);
}
}
for(int v=w;v>=;--v)
{
//跟上一轮比,得出v的最佳答案。
dp[][v] = max(dp[][v],dp[][v]);
//滚动数组
dp[][v] = dp[][v];
} } printf("%d\n",dp[][w]);
}
return ;
}
背包系列 hdu3449 有依赖背包的更多相关文章
- 背包系列 hdu 3535 分组背包
题意: 有n组工作,现在有T分钟时间去做一些工作.每组工作里有m个工作,并且类型为s,s类型可以为0,1,2,分别表示至少选择该组工作的一项,至多选择该工作的一项,不限制选择.每个工作有ci,gi两个 ...
- Gym - 100502G Outing (强连通缩点+树形依赖背包)
题目链接 问题:有n个人,最多选k个,如果选了某个人就必须选他指定的另一个人,问最多能选多少个人. 将每个人所指定的人向他连一条单向边,则每一个点都有唯一的前驱,形成的图是个基环树森林,在同一个强连通 ...
- RNQOJ [stupid]愚蠢的矿工(树形依赖背包)
题意 题目链接 Sol 树形依赖背包板子题 树形依赖背包大概就是说:对于一个点,只有选了它的父亲才能选自身 把dfs序建出来,倒过来考虑 设\(f[i][j]\)表示从第\(i\)个节点往后背包体积为 ...
- 【HDU 4276】The Ghost Blows Light(树形DP,依赖背包)
The Ghost Blows Light Problem Description My name is Hu Bayi, robing an ancient tomb in Tibet. The t ...
- hdu 1561 The more, The Better (依赖背包 树形dp)
题目: 链接:点击打开链接 题意: 非常明显的依赖背包. 思路: dp[i][j]表示以i为根结点时攻击j个城堡得到的最大值.(以i为根的子树选择j个点所能达到的最优值) dp[root][j] = ...
- hdoj1010Starship Troopers (树dp,依赖背包)
称号:hdoj1010Starship Troopers 题意:有一个军队n个人要占据m个城市,每一个城市有cap的驻扎兵力和val的珠宝,并且这m个城市的占率先后具有依赖关系,军队的每一个人能够打败 ...
- 依赖背包——cf855C好题
比较裸的依赖背包,但是想状态还是想了好久 转移时由于边界问题,虽然可以倒序转移,但当容量为0|1的时候,由于有初始值的存在 很难再原dp数组上进行修改,所以额外用tmp数组来保存修改后的值 #incl ...
- cf581F 依赖背包+临时数组 好题
这题得加个临时数组才能做.. /* 给定一棵树,树节点可以染黑白,要求叶子节点黑白平分 称连接黑白点的边为杂边,求使得杂边最少的染色方 那么设dp[i][j][0|1]表示i子树中有j个叶子节点,i染 ...
- poj1155 依赖背包
/* 依赖背包 dp[i][j]表示i结点为根的树选择j个用户时的最大剩余费用 即背包容量是j,价值是最大费用 */ #include<iostream> #include<cstr ...
随机推荐
- 通过JQUERY获取SELECT OPTION中选中的值
遇到一样学一样. 一个是取KEY,一个是取VALUE,一个是取所有文本. var dbuser_select = $("#dbuser_select option:selected" ...
- kendo grid 点击新增没有反映
在datasource中缺少 editable: "inline",这一行
- zoj——3556 How Many Sets I
How Many Sets I Time Limit: 2 Seconds Memory Limit: 65536 KB Give a set S, |S| = n, then how ma ...
- Ubuntu 16.04下截图工具Shutter
Ubuntu下自带截图工具Screenshot,但是有个缺点是不能对截到的图进行标注,快捷键如下: 截图的升级软件Shutter,具有标注的功能 安装: sudo apt-get install sh ...
- PAT Broken Keyboard (20)
题目描写叙述 On a broken keyboard, some of the keys are worn out. So when you type some sentences, the cha ...
- 面向对象设计:共性VS个性-------继承的粒度和聚合的粒度以及类的重构
共性和个性 依据面向对象的原理.类是对象的抽象.也就是说.类是一系列的既有共性又有个性的对象的高度概括,类的属性和方法代表了隶属于该类的全部对象的共性,类的每一个对象实例都能够有不同的属性值,这反映了 ...
- error: 'Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)'
[root@luozhonghua ~]# /usr/bin/mysqladmin -u root password 'aaaaaa' /usr/bin/mysqladmin: connect t ...
- 第一章 Java Collections Framework总览
1.Java容器 Java容器中只能存放对象,对于基本数据类型(byte,short,char,int,long,float,double,boolean),需要包装成对象类型(Byte,Short, ...
- Linux下的ssh实验环境搭建与管理
实验环境[size=10.5000pt]1:网桥模式[size=10.5000pt]2:安装好vmtoos[size=10.5000pt]3:安装好yum[size=10.5000pt]4:安装好ss ...
- oc80--copy
// // main.m // Copy基本使用,拷贝的本质:修改其中一个不能影响另外一个. // 每个oc对象都有copy和mutableCopy方法,前提是必须遵守NSCopying协议实现cop ...