zstu 1032 拆分物品 再01背包

判断能不能将总的物品分两半,中间拆分的时候有点多重背包的思想

#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int value[1005];
int dp[60005];
int num[7];
int main()
{
    int i,j,d,ans,sum,ncase=1;
    while(scanf("%d%d%d%d%d%d",&num[1],&num[2],&num[3],&num[4],&num[5],&num[6])!=EOF)
    {
         if(num[1]==0&&num[2]==0&&num[3]==0&&num[4]==0&&num[5]==0&&num[6]==0)
            break;
         d=sum=0;
         for(i=1;i<=6;i++)
         {
             sum+=num[i]*i;
             j=1;
             while(j<=num[i])                                                          
             {
                  num[i]-=j;
                  value[d++]=j*i;
                  j*=2;
             }
             if(num[i]>0)
                  value[d++]=num[i]*i;
         }
         printf("Collection #%d:\n",ncase++);
         if(sum%2==1)
         {
             printf("Can't be divided.\n\n");
             continue;
         }
         ans=sum/2;
         memset(dp,0,sizeof(dp));
         dp[0]=1;
         for(i=0;i<d;i++)                                                                    
          for(j=ans;j>=value[i];j--)
             if(dp[j-value[i]])
                dp[j]=1;
         if(dp[ans])
            printf("Can be divided.\n\n");
         else
            printf("Can't be divided.\n\n");
    }
    return 0;
}

  

posted @ 2011-11-19 15:11  Because Of You  Views(226)  Comments(0Edit  收藏  举报