Dividing----POJ_1014----多重背包问题
来源:互联网 发布:微信群加人软件免费版 编辑:程序博客网 时间:2024/05/20 16:40
题目地址:http://poj.org/problem?id=1014
Dividing
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 45579 Accepted: 11364
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
这个题的题意是,有六种物品,每种物品的价值依次为1,2,3,4,5,6。每种物品有多重。现在给出每种物品的个数。要你判断这些物品可不可以平分使得两边的价值相等。我的思路是将这些背包的总价值统计出来。然后将总价值除以2作为一个背包的容量,看这个背包能否装满。如果能够装满的话,则说明以上物品可以拿一半出来装在这个背包里面,价值为总价值的一半,那么也就是可以平分了,如果不能装满,则不能平分。这样我们就将这个问题转化为多重背包问题求解了。多重背包问题的解题思路请参考Tianyi Cui 大牛写的:http://love-oriented.com/pack/P03.html。但是这里还有一个初始化的问题,在最开始的时候,我们是一个也没装的,所以除了f[0]=0以外,其它的都应设为负无穷,以表示这些状态在没装物品的时候是无效的。
#include<iostream>using namespace std;#define MAX 240050#define INF 100000000int f[MAX]; //f[v]表示以v为容量的背包装满时能装的最大价值,不能装满则为负数 int data[7];int V;void CompletePack(int c,int w){int v;for(v=c;v<=V;v++)if( (f[v-c]+w) > f[v])f[v] = f[v-c]+w;}void ZeroOnePack(int c,int w){int v;for(v=V;v>=c;v--)if( (f[v-c]+w) > f[v])f[v] = f[v-c]+w;}void MulPack(int c,int w,int num){if(c*num >= V){CompletePack(c,w);return ;}int k=1;while(k<num){ZeroOnePack(k*c,k*w);num = num - k;k = k*2;}ZeroOnePack(num*c,num*w);}int main(){int w[7] = {0,1,2,3,4,5,6};int c[7] = {0,1,2,3,4,5,6};int i;int count = 1;while(scanf("%d%d%d%d%d%d",&data[1],&data[2],&data[3],&data[4],&data[5],&data[6]) != EOF){if(data[1] == 0 && data[2] == 0 && data[3] == 0 && data[4] == 0 && data[5] == 0 && data[6] == 0)break;V=0;for(i=1;i<=6;i++)V+=w[i]*data[i]; //统计总价值 f[0] = 0;printf("Collection #%d:\n",count++);if(V % 2 == 1)//如果总价值是奇数的话则可以直接判断不能平分了 {printf("Can't be divided.\n");}else{V=V/2;for(i = 1;i<=V;i++)f[i] = -INF; //初始化 for(i=1;i<=6;i++)MulPack(c[i],w[i],data[i]);if(f[V] < 0) //如果小于0,则是无效状态,即没装满 printf("Can't be divided.\n"); elseprintf("Can be divided.\n");}printf("\n");}return 0;}
- Dividing----POJ_1014----多重背包问题
- Dividing 多重背包问题
- Dividing 03多重背包问题
- hdu 1059 Dividing--DP-多重背包问题
- poj1014 Dividing 动态规划 多重背包问题
- ZOJ1149 POJ1014 HDU1059 Dividing,多重背包问题
- POJ 1014 Dividing (多重背包问题)
- HDU 1059 Dividing (多重背包问题)
- hdu 1059 多重背包问题 Dividing
- poj 1014 Dividing 多重背包问题
- HDU 1059 Dividing 多重背包问题
- POJ1014--多重背包--Dividing
- 1014 Dividing 多重背包
- Dividing ( 多重背包)
- hdu1059 Dividing 多重背包
- [hdu1059]Dividing 多重背包
- hdu1059 Dividing (多重背包)
- hdoj1059 Dividing 多重背包
- POJ3687-反向拓扑排序
- 完善PHP登陆注册页面,同时连接mysql数据库
- Android.mk简介
- Android实现推送方式解决方案
- c语言--求两圆交点
- Dividing----POJ_1014----多重背包问题
- hdu 4301 Divide Chocolate (多校联合1)
- 屏幕方向android:screenOrientation
- wchar_t TO char & char TO wchar_t
- struts2 OGNL $ % *的用法
- 找不到System.Web.Extensions.dll引用
- HDU 1569 方格取数(2)(最大点权独立集)
- 史上最牛最全android开发知识汇总
- Android Push Notification实现信息推送使用