Blue Bridge: algorithm improves Jinming budget plan

 Algorithm to Improve Jinming budget plan   

Problem 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, it is an example of some of the main parts and accessories in the following table:

Main items annex
computer Printers, scanners
bookcase books
desk Lamps, stationery
Work chairs no


  If you buy items classified as an attachment, 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], k items were selected, followed by numbers j_1, j_2, ......, j_k, then the required sum is:
  V [j_1 ] * w [j_1] + v [j_2] * w [j_2] + ... + v [j_k] * w [j_k]. (Where * is the multiplication sign)
  Please help Jinming designed to meet the requirements of a shopping list.

Input Format

  Budget.in 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 row 2 to row m + 1, j-th row gives the article number j-1 is the basic data, each line 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 article is attachment, q is relevant to No primary member)

Output Format

  The maximum value of the output file budget.out only 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).

Sample input

1000 5
800 2 0
400 5 1
300 5 1
400 3 0
500 2 0

Sample Output

2200

#include <algorithm>
#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include <cmath>
using namespace std;
struct rec{
    int v,p=0,q=0;
}a[60],b[60],c[60];
//主件a,附件b,附件c 
int n,m,f[32000],maxn,aa,bb,cc;
int main()
{ 
    memset(f,0,sizeof(f));
    cin>>n>>m;
    for(int i=1;i<=m;i++)
    {
        cin>>aa>>bb>>cc;
        if(cc!=0)//附件 
        {
            if(b[cc].q==0)//没有附件1,成为附件1 
            {
                b[cc].v=aa;//价格 
                b[cc].p=aa*bb;//价值 
                b[cc].q=2516;//密匙
            }
            else//已经有附件1,成为附件2 
            {
                c[cc].v=aa;
                c[cc].p=aa*bb;
                c[cc].q=2516;
            }
        }
        else//主件 
        {
            a[i].v=aa;
            a[i].p=aa*bb;
            a[i].q=2516;
        }
    }
    for(int i=1;i<=m;i++)
        for(int j=n;j>=a[i].v;j--)
            if(a[i].q==2516)
            {
                f[j]=max(f[j],f[j-a[i].v]+a[i].p);//买主件 
                if(j>=a[i].v+b[i].v&&b[i].q==2516) f[j]=max(f[j],f[j-a[i].v-b[i].v]+a[i].p+b[i].p);//买主件和附件1 
                if(j>=a[i].v+c[i].v&&c[i].q==2516) f[j]=max(f[j],f[j-a[i].v-c[i].v]+a[i].p+c[i].p);//买主件和附件2 
                if(j>=a[i].v+b[i].v+c[i].v&&b[i].q==2516&&c[i].q==2516) f[j]=max(f[j],f[j-a[i].v-b[i].v-c[i].v]+a[i].p+b[i].p+c[i].p);//买主件和附件1、2 
                maxn=max(maxn,f[j]);
            }
    cout<<maxn;
    return 0;
}

 

Published 736 original articles · won praise 123 · views 80000 +

Guess you like

Origin blog.csdn.net/S_999999/article/details/103298981