HDU 3395 Special Fish(二分图中最优匹配)

题意:有N条鱼(不分性别),每条鱼有一个价值vi.且给出一个N*N的矩阵,矩阵中(i,j)格为1表示,第i条鱼会攻击第j条鱼并产下卵.产卵的数量= vi XOR vj. 现在每条鱼只能被攻击1次(一条鱼只能攻击1次且被攻击1),且每条鱼只会攻击它可能会攻击的所有鱼中的一条(哪些其他鱼它会攻击已经在N*N矩阵中给出).现在要你求这N条鱼产卵数目的最大值.

思路:简单的建图然后二分最优匹配即可


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
using namespace std;
const int maxn=500+5;

//把W[maxn][maxn]数组的内容读进去之后,调用solve(n)即可计算出二分图最优匹配
//不过需要保证该图肯定有完美匹配
//因为本图用的W[][]来表示一个完全图,所以一定存在完美匹配的
struct Max_Match
{
    int W[maxn][maxn],n;  //W是权值矩阵,n为左右点集大小
    int Lx[maxn],Ly[maxn];//左右点集的可行顶标值
    bool S[maxn],T[maxn]; //标记左右点集是否已被访问过
    int left[maxn];       //left[i]=j表右i与左j匹配,为-1时表无匹配

    bool match(int i)
    {
        S[i]=true;
        for(int j=1;j<=n;j++)if(Lx[i]+Ly[j]==W[i][j] && !T[j])
        {
            T[j]=true;
            if(left[j]==-1 || match(left[j]))
            {
                left[j]=i;
                return true;
            }
        }
        return false;
    }

    //更新可行顶标,纳入更多的边进来
    void update()
    {
        int a=1<<30;
        for(int i=1;i<=n;i++)if(S[i])
        for(int j=1;j<=n;j++)if(!T[j])
        {
            a = min(a,Lx[i]+Ly[j]-W[i][j]);
        }
        for(int i=1;i<=n;i++)
        {
            if(S[i]) Lx[i]-=a;
            if(T[i]) Ly[i]+=a;
        }
    }

    int solve(int n)
    {
        this->n=n;
        memset(left,-1,sizeof(left));
        for(int i=1;i<=n;i++)//初始化可行顶标值
        {
            Lx[i]=Ly[i]=0;
            for(int j=1;j<=n;j++)
                Lx[i]=max(Lx[i], W[i][j]);
        }

        for(int i=1;i<=n;i++)
        {
            while(true)
            {
                for(int j=1;j<=n;j++) S[j]=T[j]=false;
                if(match(i)) break;
                else update();
            }
        }

        int ans=0;//最优完美匹配的权值
        for(int i=1;i<=n;i++) ans+= W[left[i]][i];
        return ans;
    }
}KM;
int main()
{
	int n,val[maxn];
	while (scanf("%d",&n)!=EOF && n)
	{
		for (int i = 1;i<=n;i++)
			scanf("%d",&val[i]);
		for (int i = 1;i<=n;i++)
			for (int j = 1;j<=n;j++)
			{
				char temp;
				scanf(" %c",&temp);
				if (temp == '1')
					KM.W[i][j]=val[i]^val[j];
				else
					KM.W[i][j]=0;
			}
	
		printf("%d\n",KM.solve(n));
	}
}

Description

There is a kind of special fish in the East Lake where is closed to campus of Wuhan University. It’s hard to say which gender of those fish are, because every fish believes itself as a male, and it may attack one of 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 other fish and can only be attacked once. No matter a fish is attacked or not, it can still 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 spawned also have a value which can be calculated by XOR operator through the value of its parents. 
We want to know the maximum possibility of the sum of the spawns.
 

Input

The input consists of multiply test cases. The first line of each test case contains an integer n (0 < n <= 100), which is the number 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 n integers, represent a 01 matrix. The i-th fish believes the j-th fish is female if 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
 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值