Los Valley - [template] 1064 Jinming budget plan (dependent backpack, both just grouping backpack backpack +)

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 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 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: represents an integer 1-5, 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.
Provided on jjj items prices v [j], the degree of importance of w [j], were selected kkk items, numbers followed by j1, j2, ..., jk, then the required sum is:
V [J1] × w [j1] + v [ j2] × w [j2] + ... + v [jk] × w [jk]
Please help Jinming designed to meet the requirements of a shopping list.
Input Output Format
Input Format:
Line 111, two positive integers, separated by a space:
Nm (where N (<32000) represents the total amount of money, m (<60) m ( <60) m (<60) for the desired number of purchase items.) from the second line to the first line m + 1, j-th row gives the number j-1j-1j-1 basic items of data, each row has three non-negative integers
vpq (where v represents the price of the item (v <10000), p indicates the degree of importance of the item (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 the number of main member belongs)
output format:
a positive integer, and the maximum importance degree to the price does not exceed the total amount of money of the sum of the products of the articles (<200 000).

Sample Input Output
Input Sample # 1:
1000. 5
800 2 0
400. 5 1
300. 5 1
400. 3 0
500 2 0

Output Sample # 1:
2200

Description
NOIP 2006 to improve the Group II title

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
struct cas{
    int v,p,q;
}a[60],pat[60][60];
int n,m,t[60],V[60][10],P[60][10],cnt[60],f[32000],ans;
int main(){
    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){
            t[a[i].q]++;
            pat[a[i].q][t[a[i].q]].v=a[i].v;
            pat[a[i].q][t[a[i].q]].p=a[i].p;
            pat[a[i].q][t[a[i].q]].q=a[i].q;
        }
    }
    for(int i=1;i<=m;i++){
        if(t[i]){
            memset(f,-1,sizeof(f));
            f[0]=0;
            for(int j=1;j<=t[i];j++)
                for(int k=n-a[i].v;k>=pat[i][j].v;k--)
                    if(f[k]<f[k-pat[i][j].v]+pat[i][j].v*pat[i][j].p && f[k-pat[i][j].v]!=-1) f[k]=f[k-pat[i][j].v]+pat[i][j].v*pat[i][j].p;
            for(int j=0;j<=n-a[i].v;j++)
                if(f[j]!=-1){
                    cnt[i]++;
                    V[i][cnt[i]]=j+a[i].v;
                    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 j=n;j>=0;j--)
            for(int k=1;k<=cnt[i];k++)
                if(j>=V[i][k])
                    f[j]=max(f[j],f[j-V[i][k]]+P[i][k]);
    for(int i=0;i<=n;i++)
        ans=max(ans,f[i]);
    printf("%d",ans);
    return 0;
}

Guess you like

Origin blog.csdn.net/mkopvec/article/details/92440215