pku 1837 Balance (01背包)

pku 1837 Balance (01背包)
01 背包问题.

f[i][j] 表示将前i个物品放入力矩为j的可行方案

f[i][j] = sum{ f[i-1][j-w[i][k] }

#include  < iostream >

using   namespace  std;

const   int  mid  =   6000 ;

int  w[ 25 ],v[ 25 ];
int  f[ 25 ][mid + mid];

int  C,G;

int  main() {
    
int i,j,k;

    cin
>>C>>G;

    
for(i=1; i<=C; ++i) cin>>v[i];
    
for(i=1; i<=G; ++i) cin>>w[i];

    
for(i=1; i<=C; ++i)
        f[
1][mid+v[i]*w[1]] = 1;

    
for(i=2; i<=G; ++i)
        
for(j=1; j<=C; ++j)
            
for(k=0; k<=2*mid; ++k)
                
if(f[i-1][k]>0)
                    f[i][k
+w[i]*v[j]] += f[i-1][k];

    cout
<<f[G][mid]<<endl;

    
return 0;
}


你可能感兴趣的:(pku 1837 Balance (01背包))