8.15 N - Special Fish

N - Special Fish

There is a kind of special fish in the East Lake where isclosed to campus of Wuhan University. It’s hard to say which gender of thosefish are, because every fish believes itself as a male, and it may attack oneof some other fish who is believed to be female by it. 
A fish will spawn after it has been attacked. Each fish can attack one otherfish and can only be attacked once. No matter a fish is attacked or not, it canstill try to attack another fish which is believed to be female by it. 
There is a value we assigned to each fish and the spawns that two fish spawnedalso have a value which can be calculated by XOR operator through the value ofits parents. 
We want to know the maximum possibility of the sum of the spawns.

Input

The input consists of multiply test cases. The first lineof each test case contains an integer n (0 < n <= 100), which is thenumber of the fish. The next line consists of n integers, indicating the value(0 < value <= 100) of each fish. The next n lines, each line contains nintegers, represent a 01 matrix. The i-th fish believes the j-th fish is femaleif and only if the value in row i and column j if 1. 
The last test case is followed by a zero, which means the end of the input.

Output

Output the value for each test in a single line.

Sample Input

3

1 2 3

011

101

110

0

Sample Output

6



题意: 题意为有n条特殊的鱼,每个鱼都有一个价值,如果鱼i认为鱼j 性别不同,那么就攻击它,繁殖的后代的价值为 s1[i] ^ s1[j], 每条鱼只能攻击或被攻击一次,问最后繁殖的后代的最大价值为多少。

思路:先用一个数组s1记录每一行的价值,用字符串读入矩阵(因为矩阵数字之间没有空格,所以用字符串读取),然后如果读入的一个字符为1,计算s1[i]^s1[j](^:异或判断)计算价值,存入一个矩阵,然后进行km算法计算。


#include<stdio.h>
#include<string.h>
int a[210][210],x1[210],x2[210],y1[210],y2[210],c[210],match[210],s1[210];
char s[210][210];
int m,n;
int max(int x,int y)
{
	if(x<y)
		x=y;
	return x;
}

int min(int x,int y)
{
	if(x>y)
		x=y;
	return x;
}

int dfs(int w)
{
	
	int i,t;//printf("%d.....\n",w);
	x2[w]=1;
	for(i=1;i<=n;i++)
	{
		if(y2[i])
			continue;
		t=x1[w]+y1[i]-a[w][i];
		if(t==0)
		{
			y2[i]=1;
			if(match[i]==-1||dfs(match[i]))
			{
				match[i]=w;
				return 1;
			}	
		}
		else
		{
			c[i]=min(c[i],t);
		}
	}	
	return 0;
}

void km()
{
	int i,j,t;
	memset(match,-1,sizeof(match));
	memset(y1,0,sizeof(y1));
	for(i=1;i<=n;i++)
	{
		x1[i]=a[i][1];
		for(j=2;j<=n;j++)
		{
			x1[i]=max(x1[i],a[i][j]);
		}
	}
	
	for(i=1;i<=n;i++)
	{
		for(j=1;j<=n;j++)
			c[j]=1e9;
			
		while(1)
		{
			memset(x2,0,sizeof(x2));
			memset(y2,0,sizeof(y2));
			if(dfs(i))
				break;
			t=1e9;
			for(j=1;j<=n;j++)
			{
				if(!y2[j]&&t>c[j])
					t=c[j];
			}
			for(j=1;j<=n;j++)
			{
				if(x2[j])
					x1[j]-=t;
			}
			for(j=1;j<=n;j++)
			{
				if(y2[j])
					y1[j]+=t;
				else
					c[j]-=t;
			}
		}
	}
	int sum=0;
	for(i=1;i<=n;i++)
	{
		if(match[i]==-1||a[match[i]][i]==0)
        {
            continue;
        }
		sum+=a[match[i]][i];
	}
	printf("%d\n",sum);
}

int main()
{
	int i,j,k;
	while(scanf("%d",&n),n!=0)
	{
		for(i=1;i<=n;i++)
		{
			scanf("%d",&s1[i]);
		}
		memset(a,0,sizeof(a));
		
		for(i=1;i<=n;i++)
		{
			scanf("%s",s[i]+1);//注意矩阵之间没有空格,最好用字符串读取
			for(j=1;j<=n;j++)
			{
		//		printf("i=%d j=%d s[i][j]=%c\n",i,j,s[i][j]);
				if(s[i][j]=='1')
				{
					a[i][j]=s1[i]^s1[j];//异或运算计算价值
				}
			}
		}
		km();
	}
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值