您的位置:首页 > 其它

POJ1014--多重背包--Dividing

2013-03-27 18:40 429 查看
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.
/*
这就是个多重背包问题啊。大理石不超过20000快
每块最大的体积是6.开个12W的数组就行啦。
以下做法是借鉴1742的。
*/
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
bool p[122000];
int A[8];
int use[122000];
int main()
{
int t=0;
while(scanf("%d%d%d%d%d%d",&A[1],&A[2],&A[3],&A[4],&A[5],&A[6])==6&&(A[1]||A[2]||A[3]||A[4]||A[5]||A[6]))
{
//这里再来一个
t++;
bool flag=true;
int value=A[1]+A[2]*2+A[3]*3+A[4]*4+A[5]*5+A[6]*6;
if(value&1)
{
flag=false;
goto L;
}
memset(p,0,sizeof(p));
p[0]=1;
for(int i=1;i<=6;i++)
{
memset(use,0,sizeof(use));
for(int j=i;j<=value;j++)
{
if(!p[j]&&p[j-i]&&use[j-i]<A[i])
{
use[j]=use[j-i]+1;
p[j]=1;
}
}
}
if(!p[value/2])
{
flag=false;
}
L:
printf("Collection #%d:\n",t);
if(!flag)
{
printf("Can't be divided.\n\n");
}
else printf("Can be divided.\n\n");
}
return 0;
}


接下来的做法是根据A[i]*i与value/2的关系将石头分为多重背包和完全背包问题。
然后再value/2的背包体积重装入最大值。看能否装value/2;
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int A[8];
int f[60008];
int xinstw[200];
int xinstv[200];
bool Aq[8];//用来标记是否为完全背包
//有的石头的总价值>value/2.那么就是完全背包。显然完全背包只可能有一种。
inline int max(int a,int b)
{
return a>b?a:b;
}
int main()
{
int t=0;
while(scanf("%d%d%d%d%d%d",&A[1],&A[2],&A[3],&A[4],&A[5],&A[6])==6&&(A[1]||A[2]||A[3]||A[4]||A[5]||A[6]))
{
memset(f,0,sizeof(f));
for(int i=1;i<=6;i++)
{
Aq[i]=1;
}
t++;
int value=A[1]+A[2]*2+A[3]*3+A[4]*4+A[5]*5+A[6]*6;
int k=1;
int vv=value/2;
bool flag=true;
if(value&1)
{
flag=false;
goto L;
}
for(int i=1;i<=6;i++)
{
if(A[i]*i>vv)
{
Aq[i]=false;
}
}
for(int i=1;i<=6;i++)
{
if(A[i]&&Aq[i])//如果有而且为多重背包
{
int temp=1;
while(A[i]>temp)
{
xinstv[k]=temp*i;
xinstw[k++]=temp*i;
A[i]-=temp;
temp*=2;
}
xinstv[k]=A[i]*i;
xinstw[k++]=A[i]*i;
}
}
for(int i=1;i<k;i++)
{
for(int j=vv;j>=xinstw[i];j--)
{
f[j]=max(f[j],f[j-xinstw[i]]+xinstv[i]);
}
}
for(int i=1;i<=6;i++)
{
if(!Aq[i])
{
for(int j=i;j<=vv;j++)
{
f[j]=max(f[j],f[j-i]+i);
}
}
}
if(f[vv]!=vv)
{
flag=false;
}
L:
printf("Collection #%d:\n",t);
if(!flag)
{
printf("Can't be divided.\n\n");
}
else printf("Can be divided.\n\n");
}
return 0;
}


 
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息