hdu3395 Special Fish(KM)

解决一个特殊鱼类配对问题,通过Kuhn-Munkres算法(KM算法)寻找最大价值匹配,实现最大化的总产卵数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Problem 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

简介:
鱼和鱼之间×之后产卵
每对鱼之间能够产的卵数目不同
最大化总产卵数

分析:
我真的是不明白出题人是怎么想到这个题目背景的,我都不忍心吐槽。。。

KM即可

当然还有一种费用流做法
(我没有实测,只是看网上大神的解释 @AC菜鸟机
最大费用最大流:
这个题没有说要是最大流的情况下,
ta要的只是最大费用
而用传统的模板保证的是最大流
这个题就被套路了.
题目没说每条鱼都要匹配
所以我们只要还要建一条边,让第i条鱼直接连到终点来三张神图解释:
这里写图片描述
这里写图片描述
这里写图片描述

tip

多组数据

千万不要忘记维护slack
i和j千万不要搞混了

//这里写代码片
#include<cstdio>
#include<cstring>
#include<iostream>

using namespace std;

const int INF=1e9;
const int N=103;
int Lx[N],Ly[N];
int W[N][N],belong[N],slack[N],a[N];
bool L[N],R[N];
int n;

int match(int i)
{
    L[i]=1;
    for (int j=1;j<=n;j++)
        if (!R[j])
        {
            int v=Lx[i]+Ly[j]-W[i][j];
            if (!v)
            {
                R[j]=1;
                if (!belong[j]||match(belong[j]))
                {
                    belong[j]=i;
                    return 1;
                }
            }
            else slack[j]=min(slack[j],v);
        }
    return 0;
}

int KM()
{
    memset(belong,0,sizeof(belong));
    memset(Ly,0,sizeof(Ly));
    for (int i=1;i<=n;i++)
    {
        Lx[i]=W[i][1];
        for (int j=2;j<=n;j++)
            Lx[i]=max(Lx[i],W[i][j]);
    }

    for (int i=1;i<=n;i++)
    {
        for (int j=1;j<=n;j++) slack[j]=INF;
        while (1)
        {
            memset(L,0,sizeof(L));
            memset(R,0,sizeof(R));

            if (match(i)) break;

            int a=INF;
            for (int j=1;j<=n;j++)
                if (!R[j]) a=min(a,slack[j]);
            for (int j=1;j<=n;j++)
                if (L[j]) Lx[j]-=a;
            for (int j=1;j<=n;j++)
                if (R[j]) Ly[j]+=a;
                else slack[j]-=a;
        }
    }

    int ans=0;
    for (int i=1;i<=n;i++)
        ans+=W[belong[i]][i];
    return ans;
}

int main()
{
    while (scanf("%d",&n)!=EOF&&n!=0)
    {
        for (int i=1;i<=n;i++) scanf("%d",&a[i]);
        memset(W,0,sizeof(W));
        char s[N];
        for (int i=1;i<=n;i++)
        {
            scanf("%s",&s);
            for (int j=0;j<n;j++)
                if (s[j]=='1')
                    W[i][j+1]=a[i]^a[j+1];
        }
        printf("%d\n",KM());
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值