[Luogu P1064] Jinming budget plan

https://www.luogu.org/problemnew/show/P1064

  From the title description we can clearly see that this is a knapsack problem, and only two kinds of items: primary items and accessories. So this is a non-tree has knapsack problem dependent. Each of the first set of attachment is a primary member  01 backpack process, first find the combination thereof can annex, the maximum value of each of the cost for each of the main member comprises a.

This main assembly obtained k attachment cost were 0 ~ n- - V [ k corresponding to the maximum value] when F [ 0 ~ n- - V [ k ] ], then we have a main member k and a set of accessories n- - V [ K ] + . 1 different options, the cost of which is V [ K ] + value of the items is t F [ t ] + V [ K ] * P [ K ].

Code:

#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<cstring>
using namespace std;
struct node {
    int v, p, q;
} a[65], p[65][65];
int n, m;
int t[65], V[65][15], F[65][15], cnt[65], f[32005], 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].p) {
            t[a[i].q]++;
            p[a[i].q][t[a[i].q]].v = a[i].v;
            p[a[i].q][t[a[i].q]].p = a[i].p;
            p[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 >= p[i][j].v; k--)
                    if(f[k] < f[k - p[i][j].v] + p[i][j].v * p[i][j].p && f[k - p[i][j].v] != -1)
                        f[k] = f[k - p[i][j].v] + p[i][j].v * p[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;
                    F[i][cnt[i]] = f[j] + a[i].v * a[i].p;
                }
        }
        if(!a[i].q) {
            cnt[i]++;
            V [i] [cnt [i]] = a [i] .v;
            F[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]] + F[i][k]);
    for(int i = 0; i <= n; i++)
        years = max (years, f [i]);
    printf("%d",ans);
    return 0;
}
View Code

 

 

 

Guess you like

Origin www.cnblogs.com/jiqimin/p/11030989.html