poj1014 dfs/背包/类筛法求素数

题意:有分别价值为1,2,3,4,5,6的6种物品,输入6个数字,表示相应价值的物品的数量,问能否将物品分成两份,使两份价值相等,其中每个物品不能切开,只能分给其中的某一方,当输入六个0时,程序结束,总物品的总个数不超过20000。

输出:每个测试用例占三行:
第一行: Collection #k: k为第几组测试用例
第二行:是否能分(具体形式见用例)

第三行:空白(必须注意,否则PE)


算法:

1.Dfs搜索

2.多重背包  ->  0-1背包+二进制拆分

3.类筛法求素数


/*
dfs:无回溯WA,无剪枝TLE 
*/

#include<iostream>
using namespace std;

int values[7];
bool flag;
int halfValue;
bool visited[120001];		// visited[i]=true:数值i已经计算过,即marbles可以分为i和sum-i,该数组用于剪枝 

void init()
{
	for (int i=0; i<=120001; i++)
	{
		visited[i] = false;
	} 
}

void output()
{
	cout <<  values[1] << " " <<  values[2] << " " <<  values[3] << " " <<  values[4] << " " <<  values[5] << " " <<  values[6] << " " << endl;
} 

void Dfs(int currVal)
{
	if (currVal == 	halfValue)
	{
		flag = true;
		return;
	}
	for (int i=6; i>=1; i--)
	{
		if (values[i] > 0 && !flag)
		{
			if (currVal + i > halfValue)
			{
				continue;
			}
			else
			{
				values[i]--;
				if (!visited[currVal + i])	// 如果 数值currVal + i已经计算过,不要重复计算,如此可以减少大量计算,否则会TLE 
				{
					Dfs(currVal + i);
					visited[currVal + i] = true;
				}
				values[i]++;				// 此处需要回溯,否则0 0 3 0 3 1难过也! 
			}
		}
	}
}

int main()
{
	int cases = 1;
	while (cin >> values[1] >> values[2] >> values[3] >> values[4] >> values[5] >> values[6])
	{
		int sum = values[1]*1 + values[2]*2 + values[3]*3 + values[4]*4 + values[5]*5 + values[6]*6;
		if (sum == 0)
		{
			break;
		}
		
		cout << "Collection #" << cases++ <<':' << endl;
		
		if (sum % 2 != 0)
		{
            cout << "Can't be divided." << endl << endl;    //注意有空行
            continue;
		}
		
		flag = false;
		halfValue = sum / 2;
		init();
		Dfs(0);
		flag?  cout<<"Can be divided."<<endl<<endl : cout<<"Can't be divided."<<endl<<endl;
	}
	return 0;
}



/*
多重背包 -> 二进制拆分 + 0-1背包 
*/
#include <iostream>
#include <string.h>
using namespace std;

const int MAX=120000;
int cnt;			// the total number of the marbles after binary split
int marbles[MAX];		// marble[i]=j: the value of the ith marble is j
int bag[MAX];			// bag[i]=j: when the capacity of the bag is i,the max value of the marbles can be put into the bag is j
int values[7];

// binary split (please refer to《二进制拆分》)
void binSplit(int val, int num)
{
	int bin_num;
	int bin_sum = 0;
	for (int i=0; ; i++)
	{
		bin_num = 1<<i;
		if (bin_sum+bin_num > num)
		{
			break;
		}
		marbles[cnt++] = bin_num * val;
		bin_sum += bin_num ;
	}
	int t = val*num - bin_sum*val;
	if (t > 0)
	{
		marbles[cnt++] = t; 
	}
}

// 0-1 bag
bool dp(int c)
{
	// For each marble which after binary split
	for (int i=0; i<cnt; i++)
	{
		// For each capacity of j,enumerate every marble i,judge whether it should be put into the bag or not
		// If put marble i into bag,then bag[j] = bag[j-marbles[i]]+marbles[i]
		// Surely,in order to ensure marble i can be put into the bag which it's capacity is j,j should greater than marbles[i]
		// If do not put marble i into bag,then bag[j] is just bag[j]
		// By comparing the size of bag[j] and bag[j-marbles[i]]+marbles[i],we get the answer......
		for (int j=c; j>=marbles[i]; j--)
		{
			if (bag[j-marbles[i]]+marbles[i] > bag[j])
			{
				bag[j] =  bag[j-marbles[i]]+marbles[i];
			}
		}
	}
	return bag[c] == c;
}

int main()
{
	int cases = 1;
	int halfValue;
	
	while (cin >> values[1] >> values[2] >> values[3] >> values[4] >> values[5] >> values[6])
	{
		int sum = values[1]*1 + values[2]*2 + values[3]*3 + values[4]*4 + values[5]*5 + values[6]*6;
		if (sum == 0)
		{
			break;
		}
		
		cout << "Collection #" << cases++ <<':' << endl;
		
		if (sum % 2 != 0)
		{
            cout << "Can't be divided." << endl << endl;    // Output a blank line after each test case
            continue;
		}
		
		memset(bag,0,sizeof(bag));
		memset(marbles,0,sizeof(int));
		cnt = 0;
		for (int i=1; i<=6; i++)
		{
			if (values[i] > 0)
			{
				binSplit(i,values[i]);
			} 
		}

		halfValue = sum / 2;
		dp(halfValue)? cout<<"Can be divided."<<endl<<endl:cout<<"Can't be divided."<<endl<<endl;
	}
	return 0;
}



/*
类似筛法求素数:设总数为sum,flag[i]=true表示可以分为i和sum-i。显然,flag[0]=true。
如果flag[i]=true,而且存在重量为j(1<=j<=6)的marble,那么flag[i+j]=true。
而题目要求的是flag[sum/2]。 
*/
#include <iostream>
#include <string.h>
using namespace std;

const int MAX=120000;
bool flag[MAX];				// flag[i]=ture:marbles可以分为i和sum-i 
bool tmp_flag[MAX];    
int values[7];				// values[i]=j:重量为i的marble有j个 


int main()
{
	int cases = 1;
	int halfValue;
	
	while (cin >> values[1] >> values[2] >> values[3] >> values[4] >> values[5] >> values[6])
	{
		int sum = values[1]*1 + values[2]*2 + values[3]*3 + values[4]*4 + values[5]*5 + values[6]*6;
		if (sum == 0)
		{
			break;
		}
		
		cout << "Collection #" << cases++ <<':' << endl;
		
		// 如果sum为奇数,肯定不可分 
		if (sum % 2 != 0)
		{
            cout << "Can't be divided." << endl << endl;    //注意有空行
            continue;
		}
		memset(flag,false,sizeof(flag));
		memset(tmp_flag,false,sizeof(tmp_flag));
		flag[0] = true;
		// for each type of marble
		halfValue = sum / 2;
		for (int i=1; i<=6; i++)
		{
			if (flag[halfValue])
			{
				break;
			}
			if (values[i] > 0)
			{ 
				// for each value
				for (int j=0; j<=halfValue; j++)
				{
					if (tmp_flag[halfValue])
					{
						break;
					}
					if (flag[j])
					{
						for (int k=1; k<=values[i]; k++)
						{
							if (tmp_flag[halfValue])
							{
								break;
							}
							if (j+i*k > halfValue)
							{
								break;
							}
							tmp_flag[j+i*k] = true;
						}
					}
				}
				for (int j=0; j<=halfValue; j++)
				{
					flag[j] = flag[j] || tmp_flag[j];
				}
			}
		}

		// output
		if (flag[halfValue])
		{
			cout<<"Can be divided."<<endl<<endl;
		}
		else
		{
			cout<<"Can't be divided."<<endl<<endl;
		}
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kangwq2017

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值