标签:tchar ted 思路 ++ another dfs scanf 结束 code
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\n",ans); } return 0; }
【二分图匹配入门专题1】 N - Special Fish hdu3395 【km算法】【春风十里,都不如tle~~~】
标签:tchar ted 思路 ++ another dfs scanf 结束 code
原文地址:http://www.cnblogs.com/chengdongni/p/7359199.html