HDU 3395 Special Fish 二分匹配(求最大匹配值)

来源:互联网 发布:经济数据网站 编辑:程序博客网 时间:2024/05/22 00:31

点击打开链接

 

 

Special Fish

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1111    Accepted Submission(s): 422


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
31 2 30111011100
 


 

Sample Output
6
 


 

Author
momodi@whu
 


 

Source
The 5th Guangting Cup Central China Invitational Programming Contest
 


 

Recommend
notonlysuccess

 

题意:有一种鱼,它们可以攻击攻击别的鱼。但是只能攻击一次。每条鱼都有一个value。给你一个0,1矩阵。1代表第i条鱼攻击第j条鱼。并且能够spawned的概率是val[i]和val[j]的异或(XOR)。求所有的鱼最大的spawned值。二分匹配。

 

#include<stdio.h>#include<string.h>#define inf 99999using namespace std;int g[217][217];int lx[217],ly[217];int slack[217],match[217],val[217];bool visx[217],visy[217];int n;bool dfs(int cur){    visx[cur]=true;    for(int y=1;y<=n;y++)    {        if(visy[y])continue;        int t=lx[cur]+ly[y]-g[cur][y];        if(t==0)        {            visy[y]=true;            if(match[y]==-1||dfs(match[y]))            {                match[y]=cur;                return true;            }        }        else if(slack[y]>t)        {            slack[y]=t;        }    }    return false;}int KM(){    memset(match,-1,sizeof(match));    memset(ly,0,sizeof(ly));    for(int i=1;i<=n;i++)    {        lx[i]=-inf;        for(int j=1;j<=n;j++)        {            if(g[i][j]>lx[i])            lx[i]=g[i][j];        }    }    for(int x=1;x<=n;x++)    {        for(int i=1;i<=n;i++)        slack[i]=inf;        while(true)        {            memset(visx,false,sizeof(visx));            memset(visy,false,sizeof(visy));            if(dfs(x))break;            int d=inf;            for(int i=1;i<=n;i++)            {                if(!visy[i]&&d>slack[i])                d=slack[i];            }            for(int i=1;i<=n;i++)            if(visx[i])            {                lx[i]-=d;            }            for(int i=1;i<=n;i++)            if(visy[i])ly[i]+=d;            else slack[i]-=d;        }    }    int reslut=0,flag=0;    for(int i=1;i<=n;i++)    {        if(match[i]==-1||g[match[i]][i]==-inf)        continue;        if(match[i]>-1)        {            reslut+=g[match[i]][i];            flag++;        }    }    if(flag<n)reslut=-1;    return reslut;}int main(){    while(scanf("%d",&n),n)    {        for(int i=1;i<=n;i++)            scanf("%d",&val[i]);        char a;        memset(g,0,sizeof(g));        for(int i=1;i<=n;i++)        for(int j=1;j<=n;j++)        {            scanf(" %c",&a);            if(a=='1')                g[i][j]=val[i]^val[j];        }        int ans=KM();        printf("%d\n",ans);    }    return 0;}


 

原创粉丝点击