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.
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.
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;}
- HDU 3395 Special Fish 二分匹配(求最大匹配值)
- HDU 3395 Special Fish(二分图中最优匹配)
- HDU 3395 Special Fish(二分图中最优匹配)
- HDU 3995 Special Fish(KM最大匹配)
- HDU 3395Special Fish(二分图的最大权匹配)
- [ACM] HDU 3395 Special Fish (二分图最大权匹配,KM算法)
- hdu 3395 Special Fish(异或,最大费用任意流,最优匹配)
- 【HDU】 3395 Special Fish 费用流(可KM匹配)
- 7_4_H题 Special Fish题解 [hdu 3395] (二分图最大权匹配)
- hdu 3395 Special Fish(二分图)
- hdu 3395 Special Fish(费用流//KM匹配)
- hdu 3395 Special Fish 带权匹配 费用流
- HDU3359 Special Fish (KM匹配)
- 二分匹配求最大匹配数和最大匹配值
- hdu(3395)Special Fish
- HDU 3395 Special Fish(最大费用流)
- hdu 3395 Special Fish 最大费用流
- hdu 3395 Special Fish【最大费用流】
- 2013-8-6 java.lang.UnsatisfiedLinkError: no jnicsspui in java.library.path
- Valgrind
- Highcharts使用指南
- Respon.WriteFile 下载文件
- SQL Server 2012 SEQUENCE对象
- HDU 3395 Special Fish 二分匹配(求最大匹配值)
- 线程挂起与恢复
- 解决:busybox编译 fatal error: curses.h: 没有那个文件或目录
- 编译Linux程序出现“对‘pthread_create’未定义的引用”
- MySQL中FOREIGN_KEY_CHECKS相关
- 安卓开发手记 intent 返回结果
- 每天一个go程序
- hdu 1251 统计难题 trie树
- SAP一句话入门:Financial & Controlling Accounting