hdu1059(多重背包)

Dividing

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


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.
 

Source
Mid-Central European Regional Contest 1999
 
本题要求给定数量的6种价值的物品能否分成大小相同的两部分。
本题在数据很小的情况下应该可以搜索求解。本题可以把物品的价值分别看做背包问题中的物品的价值和重量,这样一份重量对应一份价值。将所给物品的价值累加起来,若总价值为奇数,则肯定不能分成两份价值相同的;若为偶数,在把题目转化成多重背包问题,即问载重量为sum/2的背包能否装下价值为sum/2的物品。若能,则能分成价值相同的两部分;反之,则否。
 
#include<iostream>
#include<cstdio>
using namespace std;

//*****************************************************************
int dp[20000*6+10];//是具体情况而定
struct node
{
	int num,weight,value;//分别代表每种物品的数量、重量、价值
}Good[10];//定义每个物品的结构体
int Max_weight;//背包的载重量

int max(int a,int b)
{
	return a<b?b:a;
}

void ZeroOnePack(int weight,int value,int lim)
//对应01背包的求法
{
    for(int i=lim;i>=weight;i--)
		dp[i]=max(dp[i],dp[i-weight]+value);
}

void CompletePack(int weight,int value,int lim)
//对应完全背包的求法
{
    for(int i=weight;i<=lim;i++)
		dp[i]=max(dp[i],dp[i-weight]+value);
}

void MultiplePack(int weight,int value,int amount,int lim)
//选定物品后的多重背包的求法
{
    if(weight*amount>=lim)CompletePack(weight,value,lim);
    else
    {
        for(int k=1;k<amount;)
        {
            ZeroOnePack(k*weight,k*value,lim);
            amount-=k;
            k<<=1;
        }
        ZeroOnePack(amount*weight,amount*value,lim);
    }
}

void Solve(int n)
//解决物品种类数为n的多重背包问题求法
{
	memset(dp,0,sizeof(dp));
	for(int i=1;i<=n;i++)
		MultiplePack(Good[i].weight,Good[i].value,Good[i].num,Max_weight);
}
//*****************************************************************


int main()
{
	int cas,n,i,flag,total,tag=1;

	while(~scanf("%d%d%d%d%d%d",&Good[1].num,&Good[2].num,&Good[3].num,&Good[4].num,&Good[5].num,&Good[6].num))
	{
		flag=0,total=0;
		for(i=1;i<=6;i++)
		{
			if(!Good[i].num)
				flag++;
			Good[i].value=Good[i].weight=i;
			total+=(Good[i].value*Good[i].num);
		}
		if(flag==6)
			break;

		printf("Collection #%d:\n",tag++);
		if(total%2)
		{
			printf("Can't be divided.\n\n");
			continue;
		}

		Max_weight=total/2;
		Solve(6);
		if(dp[Max_weight]==Max_weight)
			printf("Can be divided.\n\n");
		else 
			printf("Can't be divided.\n\n");
	}
	return 0;
}

你可能感兴趣的:(动态规划)