[Luogu p1064] Jinming budget plan

Portal

Jinming budget plan

Title 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:

The main pieces of accessories
computer printers, scanners
bookcase books
desk lamp, stationery
work chair without

If you buy items classified as an attachment, you must first buy the main pieces of the attachment belongs. Each member can have a master \ (0 \) a, \ (1 \) or \ (2 \) Annex. Annex no longer subordinate to their own accessories. Jinming want to buy a lot of things, certainly more than qualified mother \ (N \) yuan. Thus, each item he specifies a degree of importance, is divided into \ (5 \) like: integer \ (1-5 \) represents the first \ (5 \) as the most important. He also found the price of each item on the Internet (both \ (10 \) yuan integer multiple). He does not want to exceed \ (N \) element (may be equal to \ (N \) under membered) premise that the sum of the product of the price and degree of importance of each item of the maximum.

Set up the first \ (j \) price items for \ (V_ [J] \) , the degree of importance for the \ (W_ [J] \) , were selected \ (k \) items, numbers followed by \ (j_1 , J_2, ..., J_k \) , the sum of the requirements are:

\(v_[j_1] \times w_[j_1]+v_[j_2] \times w_[j_2]+ ...+v_[j_k] \times w_[j_k]\)

Please help Jinming designed to meet the requirements of a shopping list.

Input and output formats

Input Format

The first \ (1 \) line, two positive integers, separated by a space: \ (N \ Quad m \) (where \ (N (<32000) \ ) represents the total amount of money, \ (m (<60 ) \) wishing to purchase the number of items.)

From \ (2 \) th row to the \ (m + 1 \) th, \ (J \) line gives the number \ (j-1 \) basic data items, each row having \ (3 \) non-negative integers \ (V, P, Q \) (where \ (V \) represents the price of the item ( \ (V <10000 \) ), \ (P \) represents the degree of importance of the article ( \ (1-5 \) ), \ (Q \) indicates that the article is a master or member attachment. If \ (Q = 0 \) , the article indicates the main member, if \ (Q> 0 \) , indicating that the article as an attachment, \ (Q \) is relevant to the number of the main member).

Output Format

A positive integer, the total number of items of money not more than the price of the product and the degree of importance of the sum of the maximum value ( \ (<20,000 \) )

Sample input and output

Input # 1

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

Output # 1

2200

Description / Tips

NOIP 2006 to improve the Group II title

analysis

First Tucao about the original title face this problem, the input and output formats bit chaotic qaq.Handling when I have to repair some of the

Then analyze this question. First, we will find that although this is a bundled backpack, but it is quite conscience, each of the main pieces of up to two attachment, the attachment is no longer attached annex. If we backpack main pieces, there will be \ (5 \) kinds of decisions:

  1. Uncheck the main pieces
  2. Select only the main member, not optional accessory
  3. And a member selected from primary attachment (attachment if any)
  4. And the second main option attachment member (if it has two attachment)
  5. Option main pieces and his two annexes (Annex if two words)

Then after four cases the state transition. Of course, you have to determine the feasibility (buying affordable).

  1. Not only select the main member selected from the Accessories: \ (dp_j = \ max (dp_j, J-main_ dp_ {}} + {I_V I_W main_ {}) \)
  2. And a member selected from primary attachment: \ (dp_j = \ max (dp_j, J-main_ dp_ {{{I_ -annex_ I_V}}}} + {1_v I_W main_ {} + {I_ annex_ 1_w {}}) \ )
  3. And the second main option Annex: \ (dp_j = \ max (dp_j, J-main_ dp_ {{{I_ -annex_ I_V}}}} + {2_v I_W main_ {} + {I_ annex_ 2_w {}}) \ )
  4. Two main pieces and accessories Select: \ (dp_j = \ max (dp_j, J-main_ dp_ {{{I_ -annex_ I_V}}} {1_v - annex_ I_ {}}} + {2_v main_} + {I_W annex_ {i_ {1_w}} + annex_ {i_ {2_w}}) \)

Then follow 01the board backpack write to imitate.

Code

/*
 * @Author: crab-in-the-northeast 
 * @Date: 2020-03-11 23:42:13 
 * @Last Modified by: crab-in-the-northeast
 * @Last Modified time: 2020-03-12 00:19:27
 */
#include <iostream>
#include <cstdio>

const int maxn = 32005;
inline int max(int a, int b) {
    return a > b ? a : b;
}

int n,m;

struct MAIN {
    int v,w;
}my_main[maxn];

struct ANNEX {
    int v[3],w[3],cnt = 0;
}my_annex[maxn];

int dp[maxn];

int main() {
    std :: cin >> n >> m;
    for(int i = 1; i <= m; i++) {
        int tmpv,tmpp,tmpq;
        std :: cin >> tmpv >> tmpp >> tmpq;
        if(tmpq) {
            my_annex[tmpq].cnt++;
            my_annex[tmpq].v[my_annex[tmpq].cnt] = tmpv;
            my_annex[tmpq].w[my_annex[tmpq].cnt] = tmpv * tmpp;
        }
        else {
            my_main[i].v = tmpv;
            my_main[i].w = tmpv * tmpp;
        }
    }

    for(int i = 1; i <= m; i++)
        for(int j = n; j >= my_main[i].v; j--) {
            dp[j] = max(dp[j], dp[j - my_main[i].v] + my_main[i].w);
            if(j >= my_main[i].v + my_annex[i].v[1]) dp[j] = max(dp[j], dp[j - my_main[i].v - my_annex[i].v[1]] + my_main[i].w + my_annex[i].w[1]);
            if(j >= my_main[i].v + my_annex[i].v[2]) dp[j] = max(dp[j], dp[j - my_main[i].v - my_annex[i].v[2]] + my_main[i].w + my_annex[i].w[2]);
            if(j >= my_main[i].v + my_annex[i].v[1] + my_annex[i].v[2]) dp[j] = max(dp[j], dp[j - my_main[i].v - my_annex[i].v[1] - my_annex[i].v[2]] + my_main[i].w + my_annex[i].w[1] + my_annex[i].w[2]);
        }

    std :: cout << dp[n] << std :: endl;
    return 0;
}

Evaluation results

`AC 100:R31647243

Guess you like

Origin www.cnblogs.com/crab-in-the-northeast/p/luogu-p1064.html