HDU1059-多重背包转换成01背包

Dividing

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 30257    Accepted Submission(s): 8581


Problem Description
Marsha and Bill own a collection of marbles. They want to split the collection among themselves so that both receive an equal share of the marbles. This would be easy if all the marbles had the same value, because then they could just split the collection in half. But unfortunately, some of the marbles are larger, or more beautiful than others. So, Marsha and Bill start by assigning a value, a natural number between one and six, to each marble. Now they want to divide the marbles so that each of them gets the same total value.
Unfortunately, they realize that it might be impossible to divide the marbles in this way (even if the total value of all marbles is even). For example, if there are one marble of value 1, one of value 3 and two of value 4, then they cannot be split into sets of equal value. So, they ask you to write a program that checks whether there is a fair partition of the marbles.
 

Input
Each line in the input describes one collection of marbles to be divided. The lines consist of six non-negative integers n1, n2, ..., n6, where ni is the number of marbles of value i. So, the example from above would be described by the input-line ``1 0 1 2 0 0''. The maximum total number of marbles will be 20000.

The last line of the input file will be ``0 0 0 0 0 0''; do not process this line.
 

Output
For each colletcion, output ``Collection #k:'', where k is the number of the test case, and then either ``Can be divided.'' or ``Can't be divided.''.

Output a blank line after each test case.
 

Sample Input
 
  
1 0 1 2 0 0 1 0 0 0 1 1 0 0 0 0 0 0
 

Sample Output
 
  
Collection #1: Can't be divided. Collection #2: Can be divided.

题意:输入6个数,表示6种不同的物品的个数,每个物品的重量和价值和自己的编号一样,也就是value[i]=i,weight[i]=i.

然后判断能不能平均分成两部分

所以这是一道多重背包的题目,但是题目的数据很大,多重的算法会超时,所以把多重背包转换成01背包就可以了

坑点:无

AC代码:

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=120000;
int dp[maxn],num[10];
int num2[20000]; 
int main()
{
    int v1,v2,v3,v4,v5,v6;
    int sum,i,j,k,flag=1;
    while (cin>>v1>>v2>>v3>>v4>>v5>>v6&&(v1+v2+v3+v4+v5+v6))
        {
            printf("Collection #%d:\n",flag++);
            sum=v1*1+v2*2+v3*3+v4*4+v5*5+v6*6;
            if (sum%2==1)                                //这种情况不要考虑,因为奇数不可能分成两部分
                {
                    cout<<"Can't be divided."<<endl<<endl;
                    continue;
                }
            num[1]=v1;num[2]=v2;num[3]=v3;num[4]=v4;num[5]=v5;
            num[6]=v6;
            int k=1,t;
            for (i=1;i<=6;i++)
                if (num[i]==0)
                    continue;
                else 
                    {
                        t=1;
                        while (num[i]-t>0)            //进行转换
                            {
                                num2[k++]=t*i;       //进行压缩 
                                num[i]=num[i]-t;
                                t=t*2;
                            }
                        if (num[i]>0)
                            num2[k++]=num[i]*i;            //可能num[i]<t还有剩余,这里补上 
                    }
            memset(dp,0,sizeof(dp));
            for (i=1;i<k;i++)                            //01背包代码
                for (j=sum/2;j>=num2[i];j--)            //只要算到sum/2就行了
                    dp[j]=max(dp[j],dp[j-num2[i]]+num2[i]);
            if (dp[sum/2]==sum/2)
                cout<<"Can be divided."<<endl<<endl;
            else cout<<"Can't be divided."<<endl<<endl;
        }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/z1164754004z/article/details/81004877
今日推荐