【二分图匹配入门专题1】 N

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.

InputThe 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.OutputOutput the value for each test in a single line.Sample Input

3
1 2 3
011
101
110
0

Sample Outpu6




首先反思!!今天写的题中,除了一道题是1a,其余全部tle,tle,tle,tle......哦,还有一次1wrong,真考验我耐性,更有甚者自己把结束条件给弄错了,变量名弄混,定义全局又定义局部变量。
这次又是初始化的问题,话说,为什么把图初始化为-INF就超时,初始化为0就A了呢。
题意:给你n个鱼的价值,第n行和第n列为1,则两者相连,两者相连的价值是二者价值异或运算,问最大匹配值。
思路:这个建图比较简单的哦~只要进行一次异或运算就好了,感觉这道题应该放在J题后面才合适啊。
#include<stdio.h>
#include<string.h>
#define INF 0x3f3f3f3f
#define N 110
int n;
int v[N],w[N][N],lx[N],ly[N];
int visx[N],visy[N];
int linker[N];
char s[N][N];
int d,ans;

void Getmap()
{
    int i,j;
    for(i = 1; i <= n; i ++)
        for(j = 1; j <= n; j ++)
            if(s[i][j]=='1')
                    w[i][j] = v[i]^v[j];
    return;
}

int dfs(int x)
{
    int y,tmp;
    visx[x] = 1;
    for(y = 1; y <= n; y ++)
    {
        if(!visy[y])
        {
            tmp = lx[x] + ly[y] - w[x][y];
            if(!tmp)
            {
                visy[y] = 1;
                if(linker[y]==-1||dfs(linker[y]))
                {
                    linker[y] = x;
                    return 1;
                }
            }
            else if(d > tmp)
                d = tmp;
        }
    }
    return 0;
}

int KMP()
{
    int sum,x,i,j;
    memset(linker,-1,sizeof(linker));
    memset(ly,0,sizeof(ly));
    for(i = 1; i <= n; i ++)
        for(j = 1,lx[i]=-INF; j <= n; j ++)
            if(lx[i] < w[i][j])
                lx[i] = w[i][j];
    for(x = 1; x <= n; x ++)
    {
        while(1)
        {
            memset(visx,0,sizeof(visx));
            memset(visy,0,sizeof(visy));
            d = INF;
            if(dfs(x))
                break;
            for(i = 1; i <= n; i ++)
                if(visx[i])
                    lx[i] -= d;
            for(i = 1; i <= n; i ++)
                if(visy[i])
                    ly[i] += d;
        }
    }
    sum = 0;
    for(i = 1; i <= n; i ++)
        if(linker[i]!=-1)
            sum += w[linker[i]][i];
    return sum;
}

int main()
{
    int i,j;
    while(scanf("%d",&n),n!=0)
    {
        memset(w,0,sizeof(w));
        for(i = 1; i <= n; i ++)
            scanf("%d",&v[i]);
        getchar();
        for(i = 1; i <= n; i ++)
            scanf("%s",s[i]+1);
        
        Getmap();
        ans = KMP();
        printf("%d
",ans);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/hellocheng/p/7359199.html