Transmission Door
Solution:
KM algorithm
There is an excellent document about the KM algorithm http://www.cse.ust.hk/~golin/COMP572/Notes/Matching.pdf
Implementation:
#include <cstdio>#include<algorithm>#include<cstring>#include<climits>using namespacestd;Const intN305);intW[n][n];intLx[n], ly[n], slack[n];BOOLS[n], t[n];intMatch[n];intN;BOOLDfsintu) {S[u]=true; for(intv=1; v<=n; v++) { if(T[v]) {Continue; } inttmp=lx[u]+ly[v]-W[u][v]; if(tmp==0) {T[v]=true; if(!match[v] | |DFS (Match[v])) {Match[v]=u; return true; } } Else{Slack[v]=min (slack[v], TMP); } } return false;}voidKM () {memset (match,0,sizeof(match)); memset (Lx,0x3f,sizeof(Lx)); memset (Ly,0x3f,sizeof(Ly)); for(intI=1; i<=n; i++)//Phase { for(intI=1; i<=n; i++) {Slack[i]=int_max;//Error-prone } for(intA;;) {memset (S),0,sizeof(S)); memset (T,0,sizeof(T)); if(Dfs (i)) Break; A=Int_max; for(intj=1; j<=n; J + +) { if(!T[j]) {a=min (A, slack[j]); } } for(intj=1; j<=n; J + +) { if(S[j]) {Lx[j]-=A; } if(T[j]) {Ly[j]+=A; } Else{Slack[j]-=A; } } } } intres=0; for(intI=1; i<=n; i++) {res+=W[match[i]][i]; } printf ("%d\n", res);}intMain () { for(; ~SCANF ("%d", &N); ) { for(intI=1; i<=n; i++) { for(intj=1; j<=n; J + +) {scanf ("%d", w[i]+j); }} miles (); }}
Error-prone:
I've initialized the LX, Ly, slack to 0x3f3f3f3f, resulting in DFS
Slack[v]=min (Slack[v], TMP);
Failure.
HDU 2255 Ben-well-off hair big wealth