这道题真正困扰了笔者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 有依赖背包的更多相关文章

  1. 背包系列 hdu 3535 分组背包

    题意: 有n组工作,现在有T分钟时间去做一些工作.每组工作里有m个工作,并且类型为s,s类型可以为0,1,2,分别表示至少选择该组工作的一项,至多选择该工作的一项,不限制选择.每个工作有ci,gi两个 ...

  2. Gym - 100502G Outing (强连通缩点+树形依赖背包)

    题目链接 问题:有n个人,最多选k个,如果选了某个人就必须选他指定的另一个人,问最多能选多少个人. 将每个人所指定的人向他连一条单向边,则每一个点都有唯一的前驱,形成的图是个基环树森林,在同一个强连通 ...

  3. RNQOJ [stupid]愚蠢的矿工(树形依赖背包)

    题意 题目链接 Sol 树形依赖背包板子题 树形依赖背包大概就是说:对于一个点,只有选了它的父亲才能选自身 把dfs序建出来,倒过来考虑 设\(f[i][j]\)表示从第\(i\)个节点往后背包体积为 ...

  4. 【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 ...

  5. hdu 1561 The more, The Better (依赖背包 树形dp)

    题目: 链接:点击打开链接 题意: 非常明显的依赖背包. 思路: dp[i][j]表示以i为根结点时攻击j个城堡得到的最大值.(以i为根的子树选择j个点所能达到的最优值) dp[root][j] = ...

  6. hdoj1010Starship Troopers (树dp,依赖背包)

    称号:hdoj1010Starship Troopers 题意:有一个军队n个人要占据m个城市,每一个城市有cap的驻扎兵力和val的珠宝,并且这m个城市的占率先后具有依赖关系,军队的每一个人能够打败 ...

  7. 依赖背包——cf855C好题

    比较裸的依赖背包,但是想状态还是想了好久 转移时由于边界问题,虽然可以倒序转移,但当容量为0|1的时候,由于有初始值的存在 很难再原dp数组上进行修改,所以额外用tmp数组来保存修改后的值 #incl ...

  8. cf581F 依赖背包+临时数组 好题

    这题得加个临时数组才能做.. /* 给定一棵树,树节点可以染黑白,要求叶子节点黑白平分 称连接黑白点的边为杂边,求使得杂边最少的染色方 那么设dp[i][j][0|1]表示i子树中有j个叶子节点,i染 ...

  9. poj1155 依赖背包

    /* 依赖背包 dp[i][j]表示i结点为根的树选择j个用户时的最大剩余费用 即背包容量是j,价值是最大费用 */ #include<iostream> #include<cstr ...

随机推荐

  1. [luoguP2038] 无线网络发射器选址(模拟)

    传送门 又是个模拟水题,考虑边界就好,连long long都不用开. ——代码 #include <cstdio> #include <iostream> int n, d, ...

  2. codevs1127 接水问题

    题目描述 Description 学校里有一个水房,水房里一共装有m 个龙头可供同学们打开水,每个龙头每秒钟的供水量相等,均为1. 现在有n 名同学准备接水,他们的初始接水顺序已经确定.将这些同学按接 ...

  3. [bzoj1072][SCOI2007]排列(状态压缩DP)

    题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1072 分析:看了题解才知道,状态的设计很巧妙,用余数表示,即f[i][j]表示二进制状 ...

  4. Ubuntu 16.04错误:The update information is outdated this may be caused by network...的问题解决

    说明:这个问题没有最终的解决方案,只有不断的尝试. 错误: The update information is outdated this may be caused by network probl ...

  5. FLASH BACK

    overview of different flashback technologies flashback query(including flashback query, flashback ve ...

  6. Java Web文件下载

    Web文件下载有两种.一种是文件在站点文件夹下.在浏览器中直接输入文件路径就可以下载.如http://www.xxx.com/file.zip.第二种是文件不在站点文件夹下或者文件是动态生成的(导出报 ...

  7. ObjectiveC开发教程--字符串的连接

    NSString *type = @"hello"; NSString *subtype = @"good"; NSString *typesub = [NSS ...

  8. Android 淘宝搜索记录分析及千牛数据库名称关联

    一 taobao搜索关键字分析1.导出淘宝数据文件夹.2.搜索search 找到search文件夹.查看里面可疑文件如history_8d4255cc9c9199c6ec3be940936986b9. ...

  9. 将jsp页面的<s:iterator>的数据返回到action

    jsp: <form method="post" id="createTable"> <table width="98%" ...

  10. Bing Maps进阶系列三:使用地图图像服务(ImageryService)

    Bing Maps进阶系列三:使用地图图像服务(ImageryService) 地图图像服务(ImageryService)提供了根据地理位置(经度和纬度)坐标和地图的缩放级别解析出对应于地图图片系统 ...