码迷,mamicode.com
首页 > 其他好文 > 详细

poj 1014 Dividing

时间:2014-05-03 21:52:18      阅读:253      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   class   code   tar   

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 57229   Accepted: 14680

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 file describes one collection of marbles to be divided. The lines contain six non-negative integers n1 , . . . , 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 collection, 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.

用多重背包可以做,貌似搜索也行,但要从大往小的地方搜。


#include"stdio.h"
#include"string.h"
#define N 120005
int num[10],w[10];
int v;
int f[N];
int max(int a,int b)
{
	return a>b?a:b;
}
void zero(int c,int w)
{
	int i;
	for(i=v;i>=c;i--)
		f[i]=max(f[i],f[i-c]+w);
}
void comple(int c,int w)
{
	int i;
	for(i=c;i<=v;i++)
		f[i]=max(f[i],f[i-c]+w);
}
void multi(int c,int w,int num)
{
	if(c*num>=v)
		comple(c,w);
	else
	{
		int k=1;
		while(k<num)
		{
			zero(k*c,k*w);
			num-=k;
			k*=2;
		}
		zero(num*c,num*w);
	}
}
int main()
{
	int i,Case=1;
	while(scanf("%d",&num[1]))
	{
		v=num[1];
		for(i=2;i<=6;i++)
		{
			scanf("%d",&num[i]);
			v+=num[i]*i;
		}
		if(v==0)
			break;
		printf("Collection #%d:\n",Case++);
		if(v%2==1)
		{
			printf("Can‘t be divided.\n\n");
			continue;
		}
		v/=2;
		memset(f,0,sizeof(f));
		for(i=1;i<7;i++)
		{
			if(num[i]==0)
				continue;
			multi(i,i,num[i]);
		}
		int flag=0;
		if(f[v]==v)
			printf("Can be divided.\n\n");
		else
			printf("Can‘t be divided.\n\n");
	}
	return 0;
}


dfs代码:

#include"stdio.h"
#include"string.h"
#define N 7
int num[N],v,flag;
void dfs(int i,int sum)
{
	if(flag)
		return ;
	if(sum==v)
	{
		flag=1;
		return ;
	}
	for(;i>0;i--)
	{
		if(num[i])          //开始用while竟然出不来了!!
		{
			if(sum+i<=v)   
			{
				num[i]--;         //先改变值,再进行下一层搜索
				dfs(i,sum+i);
				if(flag)       //剪枝
					return ;
			}
		}
	}
	return ;
}
int main()
{
	int i,Case=1;
	while(scanf("%d",&num[1]))
	{
		v=num[1];
		for(i=2;i<N;i++)
		{
			scanf("%d",&num[i]);
			v+=num[i]*i;
		}
		if(v==0)
			break;
		printf("Collection #%d:\n",Case++);
		flag=0;
		if(v%2==0)
		{
			v/=2;
			dfs(6,0);
		}
		if(flag)
			printf("Can be divided.\n\n");
		else
			printf("Can‘t be divided.\n\n");
	}
	return 0;
}



poj 1014 Dividing,布布扣,bubuko.com

poj 1014 Dividing

标签:des   style   blog   class   code   tar   

原文地址:http://blog.csdn.net/u011721440/article/details/24926625

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!