Luogu1064 金明的预算方案 (有依赖的背包)

枚举多个状态

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define R(a,b,c) for(register int  a = (b); a <= (c); ++ a)
#define nR(a,b,c) for(register int  a = (b); a >= (c); -- a)
#define Max(a,b) ((a) > (b) ? (a) : (b))
#define Min(a,b) ((a) < (b) ? (a) : (b))
#define Fill(a,b) memset(a, b, sizeof(a))
#define Abs(a) ((a) < 0 ? -(a) : (a))
#define Swap(a,b) a^=b^=a^=b
#define ll long long

//#define ON_DEBUG

#ifdef ON_DEBUG

#define D_e_Line printf("\n\n----------\n\n")
#define D_e(x)  cout << #x << " = " << x << endl
#define Pause() system("pause")
#define FileOpen() freopen("in.txt","r",stdin);

#else

#define D_e_Line ;
#define D_e(x)  ;
#define Pause() ;
#define FileOpen() ;

#endif

struct ios{
    template<typename ATP>ios& operator >> (ATP &x){
        x = 0; int f = 1; char c;
        for(c = getchar(); c < '0' || c > '9'; c = getchar()) if(c == '-')  f = -1;
        while(c >= '0' && c <= '9') x = x * 10 + (c ^ '0'), c = getchar();
        x*= f;
        return *this;
    }
}io;
using namespace std;

int w[67][3], val[67][3], items[67];
int f[32007];

int main(){
    int V, n;
    io >> V >> n;
    R(i,1,n){
        int v, p, q;
        io >> v >> p >> q;
        if(q == 0){
            w[i][0] = v, val[i][0] = v * p;
        }
        else{
            ++items[q];
            w[q][items[q]] = v;
            val[q][items[q]] = v * p;
        }
    }
    
    R(i,1,n){
        nR(j,V,w[i][0]){
            if(w[i][0] == 0) continue;
            f[j] = Max(f[j], f[j - w[i][0]] + val[i][0]);
            if(items[i] && j >= w[i][1] + w[i][0])
                f[j] = Max(f[j], f[j - w[i][1] - w[i][0]] + val[i][1] + val[i][0]);
            if(items[i] == 2 && j >= w[i][2] + w[i][0]){
                f[j] = Max(f[j], f[j - w[i][2] - w[i][0]] + val[i][2] + val[i][0]);
            }
            if(items[i] == 2 &&j >= w[i][2] + w[i][1] + w[i][0]){
                f[j] = Max(f[j], f[j - w[i][1] - w[i][2] - w[i][0]] +val[i][0] + val[i][1] + val[i][2]);
            }
        }
    } 
    
    printf("%d", f[V]);
    
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/bingoyes/p/11361629.html