AcWing 7. Mixed knapsack problem

#include<bits/stdc++.h>
using namespace std ;
const int N=1010;
int n,m;
int f[N];
struct Thing {
    int kind;//记录背包种类
    int v,w;
};
vector<Thing>things;
int main() {
    cin>>n>>m;
    for(int i=0; i<n; i++) {
        int v,w,s;
        cin>>v>>w>>s;
        if(s<0) Things.push_back ({- . 1 , V, W}); // 01 Backpack 
        the else  IF (S == 0 ) things.push_back ({ 0 , V, W}); // complete backpack 
        the else { // multiple backpacks optimization of the backpack 01 is converted to binary 
            for ( int K = . 1 ; K <= S; K * = 2 ) { 
                S - = K; 
                things.push_back ({ - . 1 , K V *, W * K}); 
            } 
            IF ( S> 0 ) things.push_back ({- . 1 , S V *, W * S}); 
        } 
    }
    for(auto thing:things) {
        if(thing.kind<0) {//01背包 
            for(int j=m; j>=thing.v; j--) f[j]=max(f[j],f[j-thing.v]+thing.w);
        } else {//完全背包 
            for(int j=thing.v; j<=m; j++) f[j]=max(f[j],f[j-thing.v]+thing.w);
        }
    }
    cout<<f[m]<<endl;
    return 0;
}

 

 

Guess you like

Origin www.cnblogs.com/QingyuYYYYY/p/11878212.html