这道题真正困扰了笔者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. [K/3Cloud]DBServiceHelper.ExecuteDataSet(this.Context, sql)) 返回数据问题

    例如下面代码: int sQty = 0; string sql = string.Format(@" Select FMATERIALID ,FBASEUNITID ,FAUXPROPID ...

  2. 武大OJ 613. Count in Sama’s triangle

    Description Today, the math teacher taught Alice Hui Yang’s triangle. However, the teacher came up w ...

  3. ADO连接ACCESS2007及以上版本的数据库

    function getaccessstr(databasename:string;password:string;accessVer:string='access2003'):string; beg ...

  4. 转 BlockingQueue(阻塞队列)详解

    转自 http://wsmajunfeng.iteye.com/blog/1629354 前言: 在新增的Concurrent包中,BlockingQueue很好的解决了多线程中,如何高效安全“传输” ...

  5. 在InternetExplorer.Application中显示本地图片

    忘记了,喜欢一个人的感觉 Demon's Blog  »  程序设计  »  在InternetExplorer.Application中显示本地图片 « 对VBS效率的再思考——处理二进制数据 Wo ...

  6. oc const 关键字 对指针的理解

    /* int const *p; *p是常量, p是变量 const int *p; *p是常量, p是变量 int * const p; *p是变量, p是常量 const int * const ...

  7. LeetCode 160. Intersection of Two Linked Lists (两个链表的交点)

    Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...

  8. Cocos2d-x教程(26)-Cocos2d-x + Lua脚本实现大地图缩放功能

    欢迎增加 Cocos2d-x 交流群: 193411763 视频教程地址:http://www.tudou.com/programs/view/qRiOfppMghM/ 转载请注明原文出处:http: ...

  9. Linux 下查找文件或文件夹

    有些在我看来比较实用的命令,在这里记一下,避免每次都搜索一轮. 1.查找文件和文件夹 $ find . -name "mongo*" 从当前路径开始,向子目录查找名字含有 &quo ...

  10. How to use eclipse quickly

    --> // TODO Quickly find outstanding event 快速查找未完成事件        eg: // TODO Robin   --> Templates  ...