HDU 3449 (Dependent 01 backpack)

Source: Internet
Author: User

Dependent backpack In fact, this is a tree-shaped DP, which is characterized by the need for each parent node to be a DP to its individual son's properties to obtain its own related properties.

FJ was going to buy something, and before that, he needed some boxes to fit the different things he was going to buy. Every box has something special (that is, if he wants to buy one of these things, he has to buy a box first). Each item has its own value, now FJ only w yuan to go shopping, he intends to use the money to buy the most valuable things.

Problem DESCRIPTIONFJ is going to does some shopping, and before that, he needs some boxes to carry the different kinds of s Tuff He is going to buy. Each box was assigned to carry some specific kinds of stuff (that's to say, if he was going to buy one of these stuff, he h As to buy the box beforehand). Each kind of stuff have its own value. Now FJ only have an amount of W dollars for shopping, he intends to get the highest value with the money.

Inputthe first line would contain the integers, n (the number of boxes 1 <= n <=), W (the amount of money FJ have, 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 go ODS ith box can carry), and mi pairs of numbers, the price CJ (1<=cj<=100), the value VJ (1<=vj<=1000000)

Outputfor each test case, output the maximum value FJ can get

Sample INPUT3 800 300 2 30 50 25 80 600 1 50 130 400 3 40 70 30 40 35 60

Sample Output210

Test instructions: There are n items that should have different prices and values, which is typical of the 01 backpack. But now there is a limit to buying items first to buy a specific box that can fit the item, and the value of the box is 0.

The code doesn't understand too well, it feels like a "double" 01 backpack. First of all, suppose to buy the first box, a 01 backpack for each item in the box, and then a 01 backpack for the box to decide whether or not to buy this box

DP[I][J] means that the first box has the maximum value that J Yuan can get, then it is dp[n][total]

Because the item has a "dependency" on the box, it is necessary to assign a value of 1 to the DP, which means that it is more impossible to buy the item without buying the box.

Code:

1#include <stdio.h>2#include <stdlib.h>3#include <string.h>4#include <math.h>5#include <algorithm>6#include <iostream>7 using namespacestd;8 9 #defineN 100010Ten intdp[ the][n]; One  A intMain () - { -     intn,total; the      while(~SCANF ("%d%d",&n,&Total )) { -memset (dp,-1,sizeof(DP));//have a dependency, to assign an initial value of -1 -memset (dp[0],0,sizeof(dp[0])); -          for(intI=1; i<=n;i++){ +             intbox,m; -scanf"%d%d",&box,&m); +              for(intj=box;j<=total;j++) Adp[i][j]=dp[i-1][j-box];//Let the I layer buy the box first, because the box has no value, at                                         //so directly equal to the cost of the previous layer + box money -              for(intj=0; j<m;j++) {//On the basis of spending the box money, at this time again to dp[i] layer do 01 backpack, -                                  //that I layer a box the maximum value of multiple items -                 intc,w; -scanf"%d%d",&c,&W); -                  for(intk=total;k>=c;k--){ in                     if(dp[i][k-c]!=-1)//Note that it is impossible to rely on the backpack, where k cannot buy the box and the item, -                                       //Cannot load items toDp[i][k]=max (DP[I][K],DP[I][K-C]+W);//not here Dp[i][k]=max (dp[i][j],dp[i][k-box-c]+w) +                                                             //because the box has already been bought, this expression represents a box based on an item with a box -                 } the             } *              for(intj=0; j<=total;j++)//decision whether to buy box I $Dp[i][j]=max (dp[i][j],dp[i-1][J]);//don't forget to consider the case of not selecting the current group (not required)Panax Notoginseng         } -printf"%d\n", Dp[n][total]); the     } +     return 0; A}

Reference: http://blog.csdn.net/yan_____/article/details/8539745

HDU 3449 (Dependent 01 backpack)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.