hdu1059 二进制优化多重背包

 1 #include<stdio.h>
 2 int max(int x,int y)
 3 {
 4     if (x>y) return x;
 5     return y;
 6 }
 7 int v[105],w[105],ans[1000005];
 8 int main()
 9 {
10     int t=0,f[10],er[20],i,j,sum,zz,dd;
11     while (~scanf("%d%d%d%d%d%d",&f[1],&f[2],&f[3],&f[4],&f[5],&f[6])
12            &&(f[1]!=0||f[2]!=0||f[3]!=0||f[4]!=0||f[5]!=0||f[6]!=0))
13     {
14         er[0]=1;
15         for (i=1;i<=15;i++) er[i]=2*er[i-1];
16         sum=0; zz=0;
17         for (i=1;i<=6;i++)
18         {
19             zz+=i*f[i]; dd=0;
20             while (f[i]>=er[dd])
21             {
22                 sum++;
23                 v[sum]=i*er[dd];w[sum]=er[dd];
24                 f[i]-=er[dd]; dd++;
25             }
26             if (f[i]!=0) {sum++;v[sum]=i*f[i]; w[sum]=f[i]; f[i]=0; }
27         }
28 //        for (i=1;i<=sum;i++) printf("%d ",v[i]); printf("\n");
29         t++; printf("Collection #%d:\n",t);
30         if (zz%2==1) {printf("Can't be divided.\n\n"); continue; }
31         zz=zz/2;
32         ans[0]=0; for (i=1;i<=zz;i++) ans[i]=-0x3f3f3f3f;
33         for (i=1;i<=sum;i++)
34          for (j=zz;j>=v[i];j--)
35          ans[j]=max(ans[j],ans[j-v[i]]+w[i]);
36         if (ans[zz]>0) printf("Can be divided.\n\n");
37         else printf("Can't be divided.\n\n");
38     }
39 }

http://acm.hdu.edu.cn/showproblem.php?pid=1059

posted on 2014-07-18 18:07  xiao_xin  阅读(92)  评论(0编辑  收藏  举报

导航