My DP training program: Bundle backpack

Classic Title: Jinming budget plan

Questions surface:

description

Jinming I am very happy, the family purchased a new house on the key essentials, the new room has a Jinming own dedicated very spacious room. He became even more pleased that my mother yesterday and said to him: "You need room which items to buy, how layout, you have the final say, as long as no more than N dollars on the line." Today morning, Jin Ming started doing the budget, and he wanted to buy the items into two categories: primary and Accessories, Accessories is one of the main pieces of subordinate to, the following table is an example of some of the main parts and accessories: the
main pieces of accessories
computer printers, scanners
bookcase books
desk lamp, stationery
work chair without
if you buy classified as accessory items, you must first buy the main pieces of the attachment belongs. Each main member can have zero, one or two attachments. Annex no longer subordinate to their own accessories. Jinming want to buy a lot of things, certainly more than the limit N mother yuan. Thus, each item he specifies a degree of importance, and the like is divided into 5: 1 to 5 show an integer, fifth, etc. most important. He also found the price of each item ($ 10 are integer multiples) from the Internet. He wants without exceeding N element (element may be equal to N) of the sum of the products and the degree of importance of the price of each item maximum.

The first set price for items j V [j], a degree of importance W [j], a total of k selected items, numbered sequentially as j1, j2, ......, jk, then the required sum is: v [j1 ] * w [j1] + v [j2] * w [j2] + ... + v [jk] * w [jk]. (Where * is the multiplication sign) Please help Jinming designed to meet the requirements of a shopping list.

format

Input Format

Input line of the first file, two positive integers, separated by a space:
N m
where N (<32000) represents the total amount of money, m (<60) to a desired number of items purchased. )
From the second line to the first m \ +1 row, j-th row shows the basic data for the item number j \ -1 is, each row has three non-negative integer
vpq
(where v represents the price of the item (v <10000), p indicates the degree of importance of the items (1 ~ 5), q represents the article is a primary member or attachments. If q = 0, represents the main element of the article, if q> 0, indicating that the attachment of the article, q is the number belongs to the main member)

Output Format

Only the maximum value of an output file a positive integer, with the degree of importance of the product of the price does not exceed the total amount of money of the sum of the article
(<200 000).

 

 

Bundled backpack: some main pieces and some accessories, they have their price, the difference is, must be purchased before you buy the main pieces of accessories . The main pieces did not buy the premise.

Ideas: For each possible combination of all of the main pieces of this gradually, enumeration, thus split into 01 backpack , then use the method to solve the 01 backpack.

Mistakes (may be I will make this chicken dish such a stupid mistake):

  I ripped out all the combinations as a list of items into new items in the ...

  This will result in actual operation time becomes: buy one of the main pieces, buy a main part and an annex to the main pieces of +1

  The correct approach is, each main element enumeration, and enumeration in each case (i.e., in all possible combinations of the main element), is expressed with the formula (in the pressure-like):

i = 1..n for 
  for J = V..0
    enumeration: All programs of the i-th main member
      if j> some cases the cost of
      f [j] = max (f [j], f [i- this Consideration of cases] + value in this case)

AC Code (VScode written, so to add a system pause):

#include <cstdio>
#include <cstdlib>
int N,M;
int f[32001];
int max(int a,int b){return a>b?a:b;}
struct Node {
    int c,v,len;
    int son[3];
    bool maj;
};
Node node[61];
void dp(){
    for(int i=1;i<=M;i++){
        if(!node[i].maj)continue;
        for(int j=N;j>=0;j--){
            int C=j-node[i].c;
            int V=node[i].v;
            if(C>=0){
                f[j]=max(f[j],f[C]+V);
                if(node[i].len==1){
                    C-=node[node[i].son[1]].c;
                    V+=node[node[i].son[1]].v;
                    if(C>=0)
                        f[j]=max(f[j],f[C]+V);
                }
                else if(node[i].len==2){
                    int copy_C=C,copy_V=V;
                    //only 1
                    C-=node[node[i].son[1]].c;
                    V+=node[node[i].son[1]].v;
                    if(C>=0)
                        f[j]=max(f[j],f[C]+V);
                    //only 2
                    C=copy_C;V=copy_V;
                    C-=node[node[i].son[2]].c;
                    V+=node[node[i].son[2]].v;
                    if(C>=0)
                        f[j]=max(f[j],f[C]+V);
                    //1 and 2
                    C=copy_C;V=copy_V;
                    C-=node[node[i].son[1]].c+node[node[i].son[2]].c;
                    V+=node[node[i].son[1]].v+node[node[i].son[2]].v;
                    if(C>=0)
                        f[j]=max(f[j],f[C]+V);
                }
            }
        }
    }
}
int main(){
    scanf("%d%d",&N,&M);
    for(int i=1;i<=M;i++){
        int V,P,Q;
        scanf("%d%d%d",&V,&P,&Q);
        node[i].c=V;
        node[i].v=P*V;
        if(Q>0){
            node[Q].len++;
            node[Q].son[node[Q].len]=i;
            node[i].maj=false;
        }
        else if(Q==0){
            node[i].maj=true;
        }
    }
    dp();
    printf("%d",f[N]);
    system("pause");
    return 0;
}

Guess you like

Origin www.cnblogs.com/Zarax/p/12037645.html