Title Link: http://acm.acmcoder.com/showproblem.php?pid=2255
Test instructions: Chinese
KM algorithm template problem, used to test the template
Code:
#include <stdio.h>#include <ctime>#include <math.h>#include <limits.h>#include <complex>#include <string>#include <functional>#include <iterator>#include <algorithm>#include <vector>#include <stack>#include <queue>#include <set>#include <map>#include <list>#include <bitset>#include <sstream>#include <iomanip>#include <fstream>#include <iostream>#include <ctime>#include <cmath>#include <cstring>#include <cstdio>#include <time.h>#include <ctype.h>#include <string.h>#include <assert.h>using namespace STD;Const intN =310;Const intINF =0x3f3f3f3f;intNX, NY;intG[n][n];intLinker[n], Lx[n], ly[n];intSlack[n];BOOLVisx[n], visy[n];BOOLDfsintx) {Visx[x] =true; for(inty =0; Y < NY; y++) {if(Visy[y])Continue;intTMP = Lx[x] + ly[y]-g[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 () {memset(linker,-1,sizeof(linker));memset(Ly,0,sizeof(ly)); for(inti =0; I < NX; i++) {Lx[i] =-inf; for(intj =0; J < NY; J + +) {if(G[i][j] > Lx[i]) lx[i] = G[i][j]; } } for(intx =0; x < NX; X + +) { for(inti =0; i < NY; i++) Slack[i] = INF; while(true) {memset(VISX,false,sizeof(VISX));memset(Visy,false,sizeof(Visy));if(Dfs (x)) Break;intD = INF; for(inti =0; i < NY; i++)if(!visy[i] && d > slack[i]) 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; } } }intres =0; for(inti =0; i < NY; i++) {if(Linker[i]! =-1) Res + = G[linker[i]][i]; }returnRes;}intMain () {intN while(scanf("%d", &n)! = EOF) { for(inti =0; I < n; i++) for(intj =0; J < N; J + +)scanf("%d", &g[i][j]); NX = NY = n;printf("%d\n", KM ()); }return 0;}
HDU 2255 Ben-off make a lot of money "maximum right match"