ZJU 1149 & HDU 1049 Dividing (多重背包)

#include<iostream> #include<cstring> #include<cstdio> using namespace std; int num[7],pack[150001],sum; #define max(a,b) ((a))>((b))?(a):(b) // ashione 2011-6-15 ——多重背包问题,详情见背包九讲 bool init(){ sum=0; for(int i=0;i<6;i++) scanf("%d",&num[i]),sum+=num[i]*(i+1); return sum; } void onezeropack(int cost,int vablue){ for(int i=sum;i>=cost;i--) pack[i]=max(pack[i],pack[i-cost]+vablue); } void completepack(int cost,int vable){ for(int i=cost;i<=sum;i++) pack[i]=max(pack[i],pack[i-cost]+vable); } int main(){ int p=1; while( init() ){ cout<<"Collection #"<<p++<<":"<<endl; if(sum&1){ cout<<"Can't be divided."<<endl<<endl; continue; } sum>>=1; memset(pack,0,sizeof(pack)); for(int i=0;i<6;i++){ int k=1; if(num[i]*(i+1)>sum){ completepack((i+1),(i+1)); continue; } while(k<num[i]){ onezeropack((i+1)*k,(i+1)*k); num[i]-=k; k<<=1; } onezeropack(num[i]*(i+1),num[i]*(i+1)); } (pack[sum]==sum)?cout<<"Can be divided."<<endl:cout<<"Can't be divided."<<endl; cout<<endl; } return 0; }

你可能感兴趣的:(ZJU 1149 & HDU 1049 Dividing (多重背包))