Test instructions: Tell the relationship between any two fishes, then, the weights of the two related fishes are different or spawn values, and the maximum value of all spawn is obtained.
Idea: pretreatment of any two fish spawn value, km match can be.
#include <cstdio>#include <iostream>#include <algorithm>#include <cmath>#include <set>#include <map>#include <string>#include <cstring>#include <stack>#include <queue>#include <vector>#include <cstdlib>#define Lson (rt<<1), L,m#define Rson (rt<<1|1), M+1,r#define M ((l+r) >>1)#define CL (A, B) memset (A,b,sizeof (a));#define LL Long Long#define P pair<int,int>#define X First#define Y Second#define PB Push_back#define FREAD (ZCC) freopen (ZCC, "R", stdin)#define FWRITE (ZCC) freopen (ZCC, "w", stdout)using namespace STD;Const intmaxn= the;Const intinf=999999999;intW[MAXN][MAXN];intLINKER[MAXN],LX[MAXN],LY[MAXN],SLACK[MAXN];BOOLVISX[MAXN],VISY[MAXN];intNx,ny;BOOLDfsintx) {visx[x]=true; for(inty=0; y<ny;y++) {if(Visy[y])Continue;intTmp=lx[x]+ly[y]-w[x][y];if(tmp==0) {visy[y]=true;if(linker[y]==-1|| DFS (Linker[y])) {linker[y]=x;return true; } }Else if(slack[y]>tmp) {slack[y]=tmp; } }return false;}intKM () {cl (linker,-1); Cl (ly,0); for(intI=0; i<nx;i++) {lx[i]=-inf; for(intj=0; j<ny;j++)if(W[i][j]>lx[i]) {LX[I]=W[I][J]; } } for(intx=0; x<nx;x++) {Fill (slack,slack+ny+1, INF); while(true) {cl (VISX,false); CL (Visy,false);if(Dfs (x)) Break;intD=inf; for(intI=0; i<ny;i++)if(!visy[i]&&slack[i]<d) {D=slack[i]; } for(intI=0; i<nx;i++)if(Visx[i]) {Lx[i]-=d; } for(intI=0; i<ny;i++)if(Visy[i]) Ly[i]+=d;ElseSlack[i]-=d; } }intans=0; for(intI=0; i<ny;i++)if(linker[i]!=-1) {Ans+=w[linker[i]][i]; }returnAns;}intA[MAXN];CharS[MAXN][MAXN];intMain () {intN while(scanf("%d", &n) &&n) { for(intI=0; i<n;i++) {scanf("%d", &a[i]); } for(intI=0; i<n;i++) { for(intj=0; j<n;j++) {w[i][j]=a[i]^a[j]; } } for(intI=0; i<n;i++) {scanf('%s ', S[i]); for(intj=0; j<n;j++) {if(s[i][j]==' 0 ') {w[i][j]=0; }}} Nx=ny=n;printf("%d\n", km ()); }return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
HDU3359 Special Fish (km matches)