这道题真正困扰了笔者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. mtk display 架构

    Hwc Dispatch Layers  .Layer 的区分 每个layer 都会有对应的一个BufferQueue,BufferQueue都有一个mConnectapi属性,mConnectAPI ...

  2. jQuery中事情的动态绑定 (转)

    小弟初来乍到,还弄不清楚如何添加链接   这是我转别人的,原文地址:http://blog.csdn.net/zhuyong0722/article/details/8590815#comments ...

  3. R - Milking Time DP

    Bessie is such a hard-working cow. In fact, she is so focused on maximizing her productivity that sh ...

  4. MAPZONE GIS SDK接入Openlayers3之一——矢量数据集接入

    在选择开源前端GIS框架的时候,定下来MapBox和Openlayers3.起初被MapBox美观的地图显示效果所吸引,研究后发现其实现机制与MAPZONE GIS SDK相差深远,不如Openlay ...

  5. [JavaEE] Injecting Bean

    So what is a Bean, in JavaEE, any class expect Entity are Bean. One usefully thing in Bean is Depend ...

  6. 走进windows编程的世界-----画图相关

    Windows画图 1 图形绘制      1.1 图形绘制的方式      获取到画图句柄-设备描写叙述表(DC),使用对应的画图的API,在设备上绘制图形.          1.2 颜色     ...

  7. cocos2D(七)---- CCScene

    CCScene普通情况是游戏里面的根节点.称之为"场景",执行游戏时须要通过CCDirector启动第一个场景. 当然,游戏略微复杂一点的话.可能会包括非常多个场景,这就涉及到场景 ...

  8. LINKs: Xamarin.Forms + Prism

    LINK 1 - How to use Prism with Xamarin.Forms http://brianlagunas.com/first-look-at-the-prism-for-xam ...

  9. Bitmap通过getWidth和getHeight获取尺寸不符

    在使用BitmapFactory载入图片时,常会出现这样的情况,返回的图片尺寸与实际尺寸不符.这是因为我们把图片资源放到res/drawable文件路径下时,选择的文件不同所致.不同的目录会有不同的缩 ...

  10. C语言控制台窗体图形界面编程(总结)

    本系列文章是笔者通过学习<C语言控制台窗体界面编程(修正版)>而写(关于此文档的很多其它信息请看本系列文章第一篇),旨在让大家更加清晰简洁easy地学习C语言控制台窗体界面的编程. 通过本 ...