ACwing_7. 混合背包问题_二进制dp

ACwing_7. 混合背包问题_二进制dp

//
#include<bits/stdc++.h>
using namespace std;

const int V=1111;
const int N=10*V;   // 1<<10 == 1024
int x[N],y[N],dp[V];

int main()
{
    int n,v,i,j,cnt,a,b,s,r;

    while( cin>>n>>v )
    {
        cnt=0;                          // init
        while( n-- )
        {
            cin>>a>>b>>s;
            if( s==-1 )         s=1;    // 01 背包
            else if( s==0 )     s=v/a;  // 完全背包取上界
        
            r=1;                        // init
            while( r<=s )
            {
                cnt++; x[cnt]=r*a; y[cnt]=r*b; 
                s-=r; r<<=1;
            }
            if( s ) { cnt++; x[cnt]=s*a; y[cnt]=s*b; }
        }
        memset( dp,0,sizeof( dp ) );    // init

        for( i=1;i<=cnt;i++ )
            for( j=v;j>=x[i];j-- )
                dp[j]=max( dp[j],dp[j-x[i]]+y[i] );
        cout<<dp[v]<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_63173957/article/details/124903898