P1064 Jinming budget plan relies backpack

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 NN 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 to, the following table is an example of some of the main parts and accessories: the
main pieces of accessories

Computer printers, scanners
bookcase books
desk lamp, stationery
work chair without
if you buy classified as accessory items, you must first buy the main pieces of the attachment belongs. Each main member 00 may have, attachments 11 or 22. Annex no longer subordinate to their own accessories. Jinming want to buy a lot of things, certainly more than qualified NN mother yuan. So he put each item provides an important degree, it is divided into 55 such as: integer 1-51-5 said the first 55 as the most important. He also found the price of each item (is an integer multiple of 1010 yuan) from the Internet. He hoped that under the premise of no more than NN yuan (RMB can be equal NN), so that the sum of the product of price and degree of importance of each item maximum.
Provided on jj items prices v_ [j] v [j] , the degree of importance of w_ [j] w [j] , were selected kk items, numbers j_1, j_2 ..., j_kj1, j 2, ..., jk then ask the sum of: v_ [j_1] \ times w_ [j_1] + v_ [j_2] \ times w_ [j_2] + ... + v_ [j_k] \ times w_ [j_k]
Please help Jinming design that meets requirements shopping list.
Input:
two positive integers, separated by a space:
N mNm (where N (<32000) N (< 32000) represents the total amount of money, m (<60) m ( <60) to a desired number of purchase items .) from the 22 th row to row m + 1m + 1, the first line gives the basic data jj number j-1j-1 of the article, each row has 33 non-negative integers
vp qvpq (vv represents wherein the article price (v <10000v <10000), p indicates the degree of importance of the article (1-51-5), qq indicates the article is a primary member or attachments. If q = 0q = 0, represents the main element of the article, if q> 0q> 0, indicating that the article is attachment, qq is the number belongs to the main member)
output
The maximum value of a positive integer, and the importance of the product for the price does not exceed the total amount of money is the sum of the items

The backpack and had seen some differences, called dependent backpack, the backpack and the packet is somewhat similar, is composed of the main parts and accessories collection into a group, then run each 01 backpack.

#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cstdio>

using namespace std;

struct node
{
    int v, p, q;
};

node a[65], belong[65][65];
int n, m;
int cnt[65], tot[65], f[32010], v[65][65], p[65][65];

int main()
{
    int n, m;
    scanf("%d%d", &n, &m);
    for(int i = 1; i <= m; i++)
    {
        scanf("%d%d%d", &a[i].v, &a[i].p, &a[i].q);
        if(a[i].q)
        {
            tot[a[i].q]++;
            belong[a[i].q][tot[a[i].q]] = a[i];
        }
    }
    for(int i = 1; i <= m; i++)
    {
        if(tot[i])
        {
            memset(f, -1, sizeof(f));
            f[0] = 0;
            for(int j = 1; j <= tot[i]; j++)
                for(int k = n - a[i].v; k >= belong[i][j].v; k--)//跑出恰好背包
                {
                    if(f[k] < f[k - belong[i][j].v] + belong[i][j].v * belong[i][j].p && f[k - belong[i][j].v] != -1)
                        f[k] = f[k - belong[i][j].v] + belong[i][j].v * belong[i][j].p;
                }
            for(int j = 0; j <= n - a[i].v; j++)
            {
                if(f[j] != -1)
                {
                    cnt[i]++;
                    v[i][cnt[i]] = a[i].v + j;
                    p[i][cnt[i]] = f[j] + a[i].v * a[i].p;
                }
            }
        }
        if(!a[i].q)
        {
            cnt[i]++;
            v[i][cnt[i]] = a[i].v;
            p[i][cnt[i]] = a[i].v * a[i].p;
        }
    }
    memset(f, 0, sizeof(f));
    for(int i = 1; i <= m; i++)
        for(int k = n; k >= 0; k--)// 这里注意, 每个组里只能取一件物品,就这样。
            for(int j = 1; j <= cnt[i]; j++)
            {
                if(k >= v[i][j])
                {
                    f[k] = max(f[k], f[k - v[i][j]] + p[i][j]);
                }
            }
    int ans = 0;
    for(int i = 0; i <= n; i++)
        ans = max(ans, f[i]);
    printf("%d\n", ans);
    return 0;

}

Published 40 original articles · won praise 13 · views 863

Guess you like

Origin blog.csdn.net/weixin_43891021/article/details/100099940