poj 1014Dividing

/* Name: poj 1014Dividing Author: UnimenSun Date: 14/05/2011 18:54 Description: 多重背包体积与价值相等 */ /* 解题报告: 1、多重背包体积与价值相乘的特例,用线性动规优化解决(数组计数的方法) */ #include <iostream> #include <cstring> using namespace std; const int MAXN = 60010; int vw[7], sum; int nCase; int used[MAXN], dp[MAXN]; int main() { int i, j; bool bFlag = false; while(cin>>vw[1]>>vw[2]>>vw[3]>>vw[4]>>vw[5]>>vw[6]) { if(!bFlag) { bFlag = true; } else { cout<<endl; } sum = 0; sum += vw[1]*1 + vw[2]*2 + vw[3]*3 + vw[4]*4 +vw[5]*5 + vw[6]*6; if(0 == sum) break; if(sum%2 != 0) { cout<<"Collection #"<<++nCase<<":"<<endl <<"Can't be divided."<<endl; continue; } sum/=2; memset(dp, 0, sizeof(dp)); //线性多重背包处理 for(i=1; i<=6; ++i) { memset(used, 0, sizeof(used)); for(j=i; j<=sum; ++j) { int temp = dp[j-i] + i; if(temp>dp[j] && used[j-i]<vw[i]) { dp[j] = temp; used[j] = used[j-i] + 1; } } } int nMax = 0; for(i=1; i<=sum; ++i) { if(nMax<dp[i]) nMax = dp[i]; } if(nMax == sum) { cout<<"Collection #"<<++nCase<<":"<<endl <<"Can be divided."<<endl; } else { cout<<"Collection #"<<++nCase<<":"<<endl <<"Can't be divided."<<endl; } } return 0; }

你可能感兴趣的:(poj 1014Dividing)