Test 6.29 T1 budget

Problem Description

"I need you to develop a plan to shop for me. I'm going to my palace home by some of the furniture. There are n kinds of furniture options, furniture and accessories of the main points. Prior to the purchase of a primary attachment member, I you must purchase the corresponding main pieces of Annex will not be a main part of another piece of furniture as the main corresponding each piece of furniture has its own price x i and the importance of y. to accomplish this, you need to I draw furniture can get in case it takes no more than m elements in the 1s and importance of product price and maximum. "CJK not neglect, immediately start code code.

Input Format

The first line of two numbers, m and n, n the next row, the i-th row of three numbers x, y, z, respectively, the price of the item number, and its degree of importance of the main assembly (z 0 indicates that the items mainly pieces).

Output Format

Like every product of the purchase price of the furniture and the degree of importance and. (Shaped like XI Yi + XJ YJ + ......, I, J for the purchase of furniture ID)

Sample input and output

Sample input 1

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

Sample output 1

2200

Sample input 2

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

Sample output 2

1600

Resolve

If you do not consider the primary attachment, which is a completely bare knapsack problem. But if the election will have to choose the main pieces of accessories, so we consider how to deal with dependencies. We can set up a fraction of the results of the current group is a group selected after running 01 backpack with accessory after the main part of the main pieces. Before each DP, regard the results of the main array before the copy group to the score, a score set and then finish the backpack to update the main array. Note that when you run in the 01 backpack, has been selected due to the current main parts, the scope of spending to pay attention.

Code

#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
using namespace std;
vector<int> v[62];
int n,m,i,j,k,w[62],c[62],op[62],f[30002],g[30002];
int main()
{
    freopen("budget.in","r",stdin);
    freopen("budget.out","w",stdout);
    cin>>m>>n;
    for(i=1;i<=n;i++){
        cin>>w[i]>>c[i]>>op[i];
        c[i]*=w[i];
        if(op[i]) v[op[i]].push_back(i);
    }
    for(i=1;i<=n;i++){
        if(!op[i]){
            for(j=w[i];j<=m;j++) g[j]=f[j-w[i]]+c[i];
            for(j=0;j<v[i].size();j++){
                int x=v[i][j];
                for(k=m;k>=w[i]+w[x];k--) g[k]=max(g[k],g[k-w[x]]+c[x]);//在这里注意范围
            }
            for(j=1;j<=m;j++) f[j]=max(f[j],g[j]);
            memset(g,0,sizeof(g));
        }   
    }
    cout<<f[m]<<endl;
    fclose(stdin);
    fclose(stdout);
    return 0;
}

to sum up

I did not expect or want because not detailed enough, there is no separate DP constraints and the state, leading constraint failure.

Guess you like

Origin www.cnblogs.com/LSlzf/p/11111572.html