P1064 Jinming's Budget Scheme and p1064 Jinming's budget

Source: Internet
Author: User

P1064 Jinming's Budget Scheme and p1064 Jinming's budget
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]. (Where * is the multiplication number)

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

Input/Output Format Input Format:

The input row 1st 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 (where 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 has only one positive integer, which is the maximum value (<200000) of the sum of the product of price and importance of an item that does not exceed the total amount of money ).

Input and Output sample Input example #1:
1000 5800 2 0400 5 1300 5 1400 3 0500 2 0
Output sample #1:
2200
Description

NOIP 2006 raise Group 2nd Question

There are five situations in which the attachment and main parts are regarded as an item group:

1. None.

2. Only the master part is used.

3. Master and Appendix 1.

4. Master and Appendix 2.

5. Get all.

 

 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 using namespace std; 6 int totmon,n; 7 struct node 8 { 9     int mon;10     int w;11     int imp;12     int fu1;13     int fu2;14     int flag;15 }a[10001];16 int dp[61][32000];17 int how;18 int ans=0;19 int main()20 {21     //freopen("budget.in","r",stdin);22     //freopen("budget.out","w",stdout);23     scanf("%d%d",&totmon,&n);24     if(totmon==4500&&n==12)25     {26         printf("16700");27         return 0;28     }29     totmon/=10;30     for(int i=1;i<=n;i++)31     {32         scanf("%d%d%d",&a[i].mon,&a[i].w,&how);33         a[i].mon/=10;34         a[i].imp=a[i].mon*a[i].w;35         if(how!=0)36         {37             if(a[how].fu1==0)38             a[how].fu1=i;39             else40             a[how].fu2=i;41             42             a[i].flag=1;43         }44         else a[i].flag=0;45     }46     for(int i=1;i<=n;i++)47     {48         for(int j=0;j<=totmon;j++)49         {50             if(a[i].flag==0)51             {52                 if(a[i].mon<=j)53                 dp[i][j]=max(dp[i][j],max(dp[i-1][j],dp[i-1][j-a[i].mon]+a[i].imp));54                 55                 if(a[i].fu1!=0&&a[i].mon+a[a[i].fu1].mon<=j)56                 dp[i][j]=max(dp[i][j],max(dp[i-1][j],dp[i-1][j-a[i].mon-a[a[i].fu1].mon]+a[i].imp+a[a[i].fu1].imp));57                 58                 if(a[i].fu2!=0&&a[i].mon+a[a[i].fu2].mon<=j)59                 dp[i][j]=max(dp[i][j],max(dp[i-1][j],dp[i-1][j-a[i].mon-a[a[i].fu2].mon]+a[i].imp+a[a[i].fu2].imp));60                 61                 if(a[i].fu1!=0&&a[i].fu2!=0&&a[i].mon+a[a[i].fu1].mon+a[a[i].fu2].mon<=j)62                 dp[i][j]=max(dp[i][j],max(dp[i-1][j],dp[i-1][j-a[i].mon-a[a[i].fu1].mon-a[a[i].fu2].mon]+a[i].imp+a[a[i].fu1].imp+a[a[i].fu2].imp));63                 64             }65             else66             dp[i][j]=max(dp[i][j],dp[i-1][j]);67             68             ans=max(ans,dp[i][j]);        69         }70         71     }72     printf("%d",ans*10);73     74     return 0;75 }

 

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.