Jin Ming's Budget Scheme (01 backpack, bundled attachment)

Source: Internet
Author: User
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 Component

Attachment

Computer

Printers, scanners

Bookcases

Books

Desk

Desk Lamp, stationery

Work chair

None

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 5 levels: 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]. (Where * is the multiplication number)

Please help Jin Ming design a shopping order that meets requirements.


Input
The first line of the input file budget. In 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
The output file budget. Out has only one positive integer, which is the maximum value of the product of price and importance of an item 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

Question:

This is a typical knapsack problem. What is troublesome is that there are accessories and main parts. However, this does not affect the problem solving. Since there are two attachments at most, we just need to bind the attachment to the master.

(1) Standard Algorithms

This is a typical knapsack problem. Obviously, the standard algorithm is dynamic planning. Because we need to bind the primary and attachments. As the subject does not directly provide the attachment corresponding to each primary component, a preprocessing is required: two additional arrays Q1 and Q2 are opened to record the attachment of the corresponding I primary component respectively. No need to deal with attachments. There are four main component costs. (W is used to indicate the cost)

W1 = V [I] (buyer only)

W2 = V [I] + V [Q1 [I] (buyer and first attachment)

W3 = V [I] + V [Q2 [I] (buyer and second attachment)

W4 = V [I] + V [Q1 [I] + V [Q2 [I] (buyer and those two attachments)

Design a State opt [I] to indicate the maximum price importance of the items that can be purchased at I yuan. The boundary condition is OPT [0] = 0. It is not difficult to design the transition equation for this state:

OPT [I] = max {OPT [I], OPT [I-WJ]} (I-WJ> 0) and (OPT [I-WJ]> 0 )) (0 <j <= 4)

Obviously, the solution of the question is a maximum value from OPT [1] to OPT [N. But in the output, note that 1 is removed.

Note: The price is 10, which is an integer multiple. Therefore, you can use n = N Div 10, Wi = wi Div 10 to read data.

#include<cstdio>#include<cmath>#include<cstring>#include<algorithm>using namespace std;int dp[33000];int v[70], w[70], q[70];int c[70][3], tw[5], tv[5], cnt;int N, m;void pack(int i){    if(c[i][0] == 0)    {        tv[1] = v[i];        tw[1] = v[i] * w[i];        cnt = 1;    }    else if(c[i][0] == 1)    {        tv[1] = v[i];        tw[1] = v[i] * w[i];        tv[2] = v[i] + v[c[i][1]];        tw[2] = v[i] * w[i] + v[c[i][1]] * w[c[i][1]];        cnt = 2;    }    else    {        tv[1] = v[i];        tw[1] = v[i] * w[i];        tv[2] = v[i] + v[c[i][1]];        tw[2] = v[i] * w[i] + v[c[i][1]] * w[c[i][1]];        tv[3] = v[i] + v[c[i][2]];        tw[3] = v[i] * w[i] + v[c[i][2]] * w[c[i][2]];        tv[4] = v[i] + v[c[i][1]] + v[c[i][2]];        tw[4] = v[i] * w[i] + v[c[i][1]] * w[c[i][1]] + v[c[i][2]] * w[c[i][2]];        cnt = 4;    }}int main(){    while(scanf("%d%d",&N,&m) != EOF)    {        N /= 10;        int i, j, k;        memset(c,0,sizeof(c));        for(i = 1; i <= m; i++)        {            scanf("%d%d%d",&v[i],&w[i],&q[i]);            v[i] /= 10;            if(q[i]) c[q[i]][++c[q[i]][0]] = i;        }        memset(dp, 0, sizeof(dp));        for(i = 1; i <= m; i++)        {            if(q[i]) continue;            pack(i);            for(j = N; j >= v[i]; j--)                for(k = 1; k <= cnt; k++)                    if(j >= tv[k]) dp[j] = max(dp[j], dp[j-tv[k]] + tw[k]);        }        printf("%d\n",dp[N] * 10);    }    return 0;}

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.