hdu 1059 Dividing

http://acm.hdu.edu.cn/showproblem.php?pid=1059  多重背包

View Code
 1 #include<iostream>
2 #include<cstring>
3 #include<cmath>
4 using namespace std;
5 int dp[120008];
6 int num[7],total=0;//
7 bool dp1()//能装一半,返回true
8 {
9 memset(dp,0,sizeof(dp));
10 int half=total/2;
11 //cout<<"half:"<<half<<endl;
12 int i,j,k;
13 for(i=1;i<=6;i++)//每个价值
14 for(j=1;j<=num[i];j++)//数量
15 for(k=half;k>=i;k--)//多重背包
16 {
17 dp[k]=max(dp[k],dp[k-i]+i);
18 }
19 //cout<<dp[half]<<endl;
20 if(dp[half]==half) return true;//能装满
21 else return false;
22 }
23 int main()
24 {
25 int t=0;
26 while(cin>>num[1]>>num[2]>>num[3]>>num[4]>>num[5]>>num[6])
27 {
28 if(!num[1]&&!num[2]&&!num[3]&&!num[4]&&!num[5]&&!num[6]) break;
29 int i;
30 total=0;
31 for(i=1;i<=6;i++)
32 {
33 num[i]%=10;//数量对10取余,因为10个的话,肯定可以平分,也可对100取余
34 total+=i*num[i];//总价值
35 }
36 cout<<"Collection #"<<++t<<":"<<endl;
37 if(total&1) cout<<"Can't be divided."<<endl;
38 else
39 {
40 if(dp1()) cout<<"Can be divided."<<endl;
41 else cout<<"Can't be divided."<<endl;
42 }
43 cout<<endl;
44 }
45 return 0;
46 }


 

posted @ 2012-04-02 17:34  keepmoving89  阅读(255)  评论(0编辑  收藏  举报