Luogu P1064_01 backpack variant

Because each main piece has only 0~2 attachments, and each attachment has no attachments

val[66][3], wei[66][3]//0 means master i, 1 means the first attachment of master i, 2 means the second attachment of master i

*************************************************************************

state transition

1. Choose not to install

2. Choose to install and only install the main part

3. Select the main part and the first accessory

4. Select the main part and the second accessory

5. Select the main part and the 1st and 2nd accessories

*************************************************************************

#include <cstdio>
#include <algorithm>

using namespace std;

int n, m;
int dp[32010]={0};
int val [66] [3] = {0}, wei [66] [3] = {0};

intmain()
{
    int i, j, v, p, q;
    scanf("%d %d", &n, &m);
    for(i = 1; i <= m; ++i)
    {
        scanf("%d %d %d", &v, &p, &q);
        if(!q) val[i][0]=v*p, wei[i][0]=v;//If it is the main component
        else if(!val[q][1]) val[q][1]=v*p, wei[q][1]=v;//If it is the first attachment of the main part p
        else val[q][2]=v*p, wei[q][2]=v;//Otherwise it is the second attachment of the main component p
    }
    for(i = 1; i <= m; ++i)
    {
        for(j = n; j >= 1; --j)
        {
            if(j >= wei[i][0]) //Only install the main piece
                dp[j]=max(dp[j], dp[j-wei[i][0]]+val[i][0]);
            if(j >= wei[i][0]+wei[i][1])//Install the main part and accessory 1
                dp[j]=max(dp[j], dp[j-wei[i][0]-wei[i][1]]+val[i][0]+val[i][1]);
            if(j >= wei[i][0]+wei[i][2])//Only install the main part and attachment 2
                dp[j]=max(dp[j], dp[j-wei[i][0]-wei[i][2]]+val[i][0]+val[i][2]);
            if(j >= wei[i][0]+wei[i][1]+wei[i][2])
                dp[j]=max(dp[j], dp[j-wei[i][0]-wei[i][1]-wei[i][2]]+val[i][0]+val[i][1]+val[i][2]);
        }
    }
    printf("%d\n", dp[n]);
    return 0;
}


Guess you like

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