背包问题以及DFS算法均可解之1014
来源:互联网 发布:烈火是什么软件 编辑:程序博客网 时间:2024/06/14 17:58
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.
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.
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
第一种解法,套用前面几篇博文提到的背包问题揭发。程序如下:
// acm2.cpp : 定义控制台应用程序的入口点。//#include <iostream>using namespace std;#define MAXNUM 6#define MAXV 100000int F[MAXV];int max(int i,int j){return i>j?i:j;}void zeroPack(int v,int weight,int value){for(int i=v;i>=weight;i--){F[i]=max(F[i],F[i-weight]+value);}}void completePack(int v,int weight,int value){for(int i=weight;i<=v;i++){F[i]=max(F[i],F[i-weight]+value);}}void multiplePack(int v,int weight,int value,int num){if(weight*num>=v)completePack(v,weight,value);else{int count=num,k=1;while(k<count){zeroPack(v,k*weight,k*value);count-=k;k=k+k;}zeroPack(v,count*weight,count*value);}}int main(){int n[10],i=0,totalNum=0,k=1;while(true){int bin=0;for(i=1;i<=MAXNUM;i++){cin>>n[i];totalNum+=n[i]*i;bin|=n[i];}if(!bin)break;F[0]=0;for(i=1;i<MAXV;i++)F[i]=0xffffffff;if(totalNum&0x01){cout<<"Collection #"<<k<<":\n"<<"Can't be divided."<<endl<<endl; }else{int num=totalNum>>1;for(i=1;i<=MAXNUM;i++){multiplePack(num,i,i,n[i]);}if(F[num]==num){cout<<"Collection #"<<k<<":\n"<<"Can be divided."<<endl<<endl;}elsecout<<"Collection #"<<k<<":\n"<<"Can't be divided."<<endl<<endl; }k++;}}
第二种解法:DFS
// acm.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include<iostream>using namespace std;#define NUMMAX 6int n[NUMMAX];bool flag=false;void DFS(int value,int numMax,int weight){if(value==weight){flag=true;return;}for(int i=numMax;i>=1;i--){if(n[i]){if(i+value<=weight){n[i]--;DFS(value+i,i,weight);if(flag==true)break;}}}}int main(){int numMax=0;for(int i=1;i<=6;i++)cin>>n[i];for(int i=1;i<=6;i++)numMax+=n[i]*i;if(numMax%2!=0)cout<<"no "<<endl;else{DFS(0,6,numMax/2);if(flag==false)cout<<"no"<<endl;elsecout<<"yes"<<endl;}}
- 背包问题以及DFS算法均可解之1014
- 经典算法之背包问题
- 经典算法之背包问题
- 算法之01背包问题
- 经典算法之背包问题
- 贪心算法之背包问题
- 贪心算法之背包问题
- 贪心算法之背包问题
- 经典算法之背包问题
- DFS(背包问题)
- dfs 简单背包问题
- DFS 做背包问题
- 【算法导论】贪心算法之背包问题
- DFS算法之迷宫寻路问题
- 回溯算法之0-1背包问题
- C语言算法之背包问题
- 背包问题之贪心算法实现
- acm算法之三大背包问题
- Linux学习之diff 和 patch命令的使用简介
- NSData 与 NSString,Byte数组,UIImage 的相互转换
- Sql Server 关于整表插入另一个表部分列的语法以及select 语句直接插入临时表的语法 .
- hdu 4714 Tree2cycle
- 全国计算机等级考试无纸化程序修改题
- 背包问题以及DFS算法均可解之1014
- VS2005 MFC 对话框 全局函数访问成员变量
- 周末小记2013/9/2---2013/9/8
- window.onload执行顺序详解
- iPhone开发中图像处理相关要点
- 转载-Const,Const函数,Const变量,函数后面的Const
- [C++面试题]之面向对象
- 近期计划
- MySql 链接url 参数详解