Huawei OJ: Shopping list (I don't understand...)

Title description
Wang Qiang is very happy today, the company issued a year-end bonus of N yuan. Wang Qiang decided to use the year-end bonus for shopping. He divided the items he wanted to buy into two categories: main parts and accessories. The accessories belonged to a certain main part. The following table is some examples of main parts and accessories:

主件  附件
电脑  打印机,扫描仪
书柜  图书
书桌  台灯,文具
工作椅 无

If you want to buy an item classified as an accessory, you must first buy the main item to which the accessory belongs. Each main piece can have 0, 1 or 2 attachments . Attachments no longer have attachments of their own. Wang Qiang wanted to buy a lot of things. In order not to exceed the budget, he bought them 每件物品规定了一个重要度,分为 5 等:用整数 1 ~ 5 表示,第 5 等最重要. He also checked the price of each item (all in multiples of 10 yuan) from the Internet. He wants to maximize the sum of the products of the price and importance of each item under the premise of not exceeding N dollars (which can be equal to N dollars).
Suppose the price of the jth item is v[j] , the importance is w[j] , k items are selected in total, and the numbers are j 1 , j 2 ,..., jk , then the required sum is:
v [j 1 ] w[j 1 ]+v[j 2 ]*w[j 2 ]+ … +v[jk ]*w[jk ] . (where is the multiplication number)
Please help Wang Qiang to design a shopping list that meets the requirements.

Input description:
The first line of input is two positive integers, separated by a space: N m
(where N (<32000) represents the total amount of money, and m (<60) is the number of items you want to buy.)
From Lines 2 to m+1, the jth line gives the basic data of the item numbered j-1, each line has 3 non-negative integers vpq
(where v represents the price of the item ( v<10000 ), p represents the importance of the item (1 ~ 5), q represents whether the item is a main part or an accessory. If q=0, it means the item is the main part, if q>0, it means the item is an accessory, and q is the owner part number)

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

Example 1
input

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

output

2200
#include <iostream>
#include <vector>
#include <map>
#include <algorithm>

using namespace std;

int main()
{
    int N, m;
    int weight[60][3] = {0}; //价格(成本),假设主件均有2个附件
    int value[60][3] = {0}; //价值(重要度 * 价格)
    int f[60][3201]; //第 i 个物品在 j 容量下可以获得的最大价值
    int v, p, q;
    cin >> N >> m;
    N = N / 10;
    //存储清单
    for(int i = 1; i <= m; ++i)
    {
        cin >> v >> p >> q;
        v = v / 10;
        if(q == 0)                      //主件
        {
            weight[i][0] = v;
            value[i][0] = p * v;
        }
        else                            //附件
        {
            if(weight[i][1] == 0)       //第一个附件
            {
                weight[i][1] = v;
                value[i][1] = p * v;
            }
            else                        //第二个附件
            {
                weight[i][2] = v;
                value[i][2] = p * v;
            }
        }
    }
    //遍历计算
    for(int i = 1; i <= m; ++i)
    {
        for(int j = N; j > 0; --j)
        {
            if(j >= weight[i][0])   //可以容下第i个主件时,比较放第i个或者不放第i个物品的价值
                f[i][j] = max(f[i-1][j],f[i-1][j - weight[i][0]] + value[i][0]);

            if(j >= weight[i][0] + weight[i][1])    //可以容下第i个主件和此主件的第1个附件时
                f[i][j] = max(f[i-1][j],f[i-1][j - weight[i][0] - weight[i][1]] + value[i][0] + value[i][1]);

            if(j >= weight[i][0] + weight[i][2])    //可以容下第i个主件和此主件的第2个附件时
                f[i][j] = max(f[i-1][j],f[i-1][j - weight[i][0] - weight[i][2]] + value[i][0] + value[i][2]);

            if(j >= weight[i][0] + weight[i][1] + weight[i][2])   //可以容下第i个主件和此主件的第1个附件和第2个附件时
                f[i][j] = max(f[i-1][j],f[i-1][j-weight[i][0] - weight[i][1] - weight[i][2]] + value[i][0] + value[i][1] + value[i][2]);
        }
    }
    cout << f[m][N]*10 << endl;
    return 0; 
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325337860&siteId=291194637