Budget of Jin Ming-two algorithms of RQNOJ_6--01 backpack and tree DP

Source: Internet
Author: User

Address: http://www.rqnoj.cn/Problem_6.html

Description

Jin Ming was very happy today. The new house purchased at home had the key. There was a very spacious room dedicated to Jin Ming in the new house. Even more pleased, his mother said to him yesterday: "You have the final say about the items you need to purchase and how to arrange in your room, as long as the price does not exceed N yuan ". Early this morning, Jin Ming began to make a budget. He divided the items he wanted to buy into two categories: main parts and accessories. attachments belong to a specific main component, the following table provides examples of the main parts and accessories:
Main attachment
Computer printer, Scanner
Bookcases
Desk Lamp, stationery
No work chair
If you want to buy an item that is classified as an attachment, you must first buy the main item to which the attachment belongs. Each master can have 0, 1, or 2 attachments. Attachments no longer have their own attachments. Jin Ming wants to buy a lot of things and will definitely exceed the limit of his mother's N yuan. Therefore, he defined an importance for each item and divided it into five equal values: an integer of 1 ~ 5 indicates that 5th is the most important. He also found the price of each item on the Internet (an integer multiple of 10 yuan ). He hopes that the sum of the product of the price and importance of each item will be maximized without exceeding N yuan (which can be equal to N yuan.

Set the price of item J to V [J], and the importance to W [J]. K items are selected, numbered J1, J2 ,......, JK, then the sum is: V [J1] * W [J1] + V [J2] * W [J2] +... + V [JK] * W [JK]. (Here, * is the multiplication number.) Please help Jin Ming design a shopping order meeting the requirements.

Input Format

Line 2 of the input file, which is two positive integers separated by a space:
N m
N (<32000) indicates the total amount of money, and M (<60) indicates the number of items to be purchased .)
From row 2nd to row m + 1, row J gives the basic data of the item numbered J-1, each row has three non-negative integers
V p q
(V indicates the price of the item (v <10000), and p indicates the importance of the item (1 ~ 5). Q indicates whether the item is a main item or an attachment. If q = 0, it indicates that the item is the main component. If q> 0, it indicates that the item is an attachment, and Q indicates the ID of the main component)

Output Format

The output file has only one positive integer, which is the maximum value of the product of price and importance that does not exceed the total amount of money.
(<200000 ).

Sample input 1000 5
800 2 0
400 5 1
300 5 1
400 3 0
500 2 0 sample output

2200

The meaning of this question is that there are some items, some are the main parts, some are attachments, attachments are attached to the main parts, and attachments are not attached to their attachments. To buy attachments, you need to buy the main parts it is attached. You are required to purchase some items within the scope of the given money, so that the cost of these items is multiplied by the sum of importance. In addition, all costs are multiples of 10, so this can be used for optimization to reduce space and time consumption.

There are two solutions for this question.

The first is the solution of the 01 backpack:

Because each primary part has a maximum of two attachments, the combination of the primary and known attachments can be enumerated. Then we put the main component and the Attachment attached to the main component in a set, and then find the largest one when finding each group, that is, Max (Master, Master + Attachment 1, Master + Attachment 2, Master + Attachment 1 + attachment 2), and then find the largest one through the algorithm of 01 backpack.

# Include <iostream> using namespace STD; int DP [62] [1, 4000]; // DP [I] [J] indicates the maximum value struct thing {int V, p; int set;} that can be obtained by spending $ J in the first class I item ;}; thing a [62] [62]; // A [I] [J] indicates the value and importance of item j of product I, where 0 represents the main component int main () {int n, m; // has n moneys, want to buy M kinds of thingswhile (scanf ("% d", & N, & M )! = EOF) {n = N/10; int I, j; int V, p, q; int S = 0; for (I = 0; I <= m; I ++) for (j = 0; j <= m; j ++) {A [I] [J]. V = 0; // As a signa [I] [J]. P = 0 ;}for (I = 1; I <= m; I ++) {scanf ("% d", & V, & P, & Q); V = V/10; // optimization, because the price must be an integer multiple of 10 if (q = 0) // It is a main thing {s ++; A [s] [0]. V = V; A [s] [0]. P = V * P; A [s] [0]. set = I;} else {int K = 1; while (k <= S & A [k] [0]. set! = Q) // find the type of K ++ for this attachment; if (a [k] [1]. V = 0) // The first {A [k] [1]. V = V; A [k] [1]. P = V * P;} else // The second {A [k] [2]. V = V; A [k] [2]. P = V * P ;}}// the following uses the 01 backpack idea to solve the problem memset (DP, 0, sizeof (DP); for (I = 1; I <= s; I ++) {for (j = N; j> = 0; j --) {DP [I] [J] = DP [I-1] [J]; // class I items are not directly obtained if (j> = A [I] [0]. V & DP [I-1] [J-A [I] [0]. v] + A [I] [0]. p> DP [I] [J]) // only take one master, DP [I] [J] = DP [I-1] [J-A [I] [0]. v] + A [I] [0]. p; If (j> = A [I] [0]. V + A [I] [1]. V & DP [I-1] [J-A [I] [0]. v-A [I] [1]. v] + A [I] [0]. P + A [I] [1]. p> DP [I] [J]) DP [I] [J] = DP [I-1] [J-A [I] [0]. v-A [I] [1]. v] + A [I] [0]. P + A [I] [1]. p; If (j> = A [I] [0]. V + A [I] [2]. V & DP [I-1] [J-A [I] [0]. v-A [I] [2]. v] + A [I] [0]. P + A [I] [2]. p> DP [I] [J]) DP [I] [J] = DP [I-1] [J-A [I] [0]. v-A [I] [2]. v] + A [I] [0]. P + A [I] [2]. p; If (j> = A [I] [0]. V + A [I] [1]. V + A [I] [2]. V & DP [I-1] [J-(A [I] [0]. V + A [I] [1]. V + A [I] [2]. v)] + A [I] [0]. P + A [I] [1]. P + A [I] [2]. p> DP [I] [J]) DP [I] [J] = DP [I-1] [J-(A [I] [0]. V + A [I] [1]. V + A [I] [2]. v)] + A [I] [0]. P + A [I] [1]. P + A [I] [2]. P ;}} printf ("% d \ n", DP [s] [N] * 10);} return 0 ;}

The second is the tree-like DP algorithm:

Because there are two main parts and accessories, the attachment can be used as the son node of the main component. Because a main component may have multiple attachments, we need to convert this into a binary tree to facilitate algorithm implementation, that is, the left son is its attachment, and the right son is a brother, that is, at the same level, all the main parts share a Father's Day, then we need to take the first node of Father's Day as the root in this tree to find the maximum value. The following code is uploaded.

// Jinming's budget is made by using the tree-like DP. Exercise yourself // but it is really disgusting # include <iostream> using namespace STD; struct bit_tree {int LD; // left child int RD; // right childint V; int W; int lastchild;} tree [62]; void fun (int x, int y ); int DP [62] [4000]; // DP [I] [J] indicates the maximum value int main () {int N, m; while (scanf ("% d", & N, & M )! = EOF) {n = N/10; // optimized int I, j; int V, p, q; for (I = 0; I <= m; I ++) {tree [I]. LD =-1; tree [I]. rD =-1 ;}for (I = 0; I <= m; I ++) for (j = 0; j <= N; j ++) DP [I] [J] =-1; // indicates the maximum value tree that can be obtained by spending J yuan on an I node [0]. W = 0; for (I = 1; I <= m; I ++) {scanf ("% d", & V, & P, & Q); V = V/10; // Optimized Tree [I]. V = V; tree [I]. W = V * P; If (tree [Q]. LD =-1) // There is no left child at Father's Day {tree [Q]. LD = I; tree [Q]. lastchild = I;} else {tree [tree [Q]. lastchild]. rD = I; tree [Q]. lastchild = I ;}for (I = 0; I <= m; I ++) // initialize the leaf node {If (tree [I]. LD =-1 & tree [I]. rD =-1) for (j = N; j> = 0; j --) if (j> = tree [I]. v) DP [I] [J] = tree [I]. w; elsedp [I] [J] = 0;} fun (1, N); printf ("% d \ n", DP [1] [N] * 10 ); // multiply by 10 because the previous optimization was divided by 10} return 0;} void fun (int x, int y) {If (DP [x] [Y]> = 0 | x =-1) return; If (tree [X]. LD =-1) // This if is part of the optimization code. To avoid timeout, it is the practice of {If (Y> = tree [x] When the node has no left child. v) {fun (tree [X]. RD, Y); DP [x] [Y] = DP [tree [X]. rd] [Y]; fun (tree [X]. RD, Y-tree [X]. v); If (DP [x] [Y] <tree [X]. W + dp [tree [X]. rd] [Y-tree [X]. v]) DP [x] [Y] = tree [X]. W + dp [tree [X]. rd] [Y-tree [X]. v]; return ;}} fun (tree [X]. RD, Y); // allocate all the Y elements to the right subtree int max = DP [tree [X]. rd] [Y]; // int K as the initial value of Max; For (k = tree [X]. v; k <= y; k ++) // if not all are allocated to the right subtree, // If the left and right subtree are separated from each other, You need to enumerate different situations {fun (tree [X]. LD, K-tree [X]. v); fun (tree [X]. RD, Y-k); int temp = DP [tree [X]. ld] [k-tree [X]. v] + dp [tree [X]. rd] [Y-K] + tree [X]. w; If (max <temp) max = temp;} DP [x] [Y] = max ;}

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.