Click to open link
The most powerful match
KM algorithm
Algorithm steps:
Set vertex Xi's top mark as a[i], Apex Yi's top label is B[i]
Ⅰ. Initial time. A[i] is the maximum weight of the edge associated with XI. B[j]=0. Guarantee A[i]+b[j]>=w (I,J) established
Ⅱ. When a complete match is not included in the equality sub-graph, the top label is modified appropriately to enlarge the equality sub-graph until a complete match is found
Ⅲ. How to change the top label
When a staggered path from XI fails, a staggered tree is obtained, and all its leaf nodes are X nodes. The top of the x vertex in the interleaved tree decreases the D value, and the top of the y vertex adds the D value, for all edges in the graph (i,j),
can see:
Both I and J are not in the staggered tree, and the edges (i,j) still do not belong to the equal sub-graph
Both I and J are in the staggered tree, and the edges (i,j) still belong to the equal sub-graph
I is not in the interlaced tree. J in the staggered tree, A[i]+b[j] expands. Edge (I,J) does not belong to an equal sub-graph
I in the staggered tree, J is not in the interlaced tree, The Edge (I,J) may be added to the equal sub-graph
In order for the A[i]+b[j]>=w (I,J) to always be true, and at least one edge is added to the equal sub-graph, d=min{a[i]+b[j]-w (i,j)},i in the interlaced tree, J is not in the interlaced tree
time complexity: need to find an O (n) time augmentation path. Each augmentation requires an O (n) sub-index change. Each time you change the top, enumerate the edges to find the D value, the complexity is O (N2), and the total complexity is O (N4). Simple optimizations can be reduced to O (N3), each y vertex a "slack" function slack, initialized to infinity each time you start looking for an augmented path.
While looking for an augmented path, check the edges (i,j). Assume that you are not in the equal sub-graph, let slack[j] be the smaller value of the original value and A[i]+b[j]-w[i,j]. Such When you change the top label, take the minimum value in the slack value of the Y vertex that is not in the interleaved tree as the D value. However, it is important to note that all slack values are subtracted from D after changing the top label.
#include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include < algorithm> #include <set> #include <map> #include <stack> #include <queue> #include < vector> #include <string> #define FOR0 (A, B) for (a=0;a<b;++a) #define FOR1 (A, B) for (A=1;A<=B;++A) #define Foru (i,a,b) for (i=a;i<=b;++i) #define FORD (I,A,B) for (i=a;i>=b;--i) using namespace Std;typedef long Long ll;const int MAXN = 310;const int INF = 1e9;/*km algorithm *o (nx*nx*ny) * For maximum weight matching * If minimum weight matching is obtained, the inverse number of weights can be obtained and the inverse number will be obtained. */int NX, Ny;int g[maxn][maxn];int LINKER[MAXN], LX[MAXN], ly[maxn];//y in each point matching state, X, y in the top int slack[maxn];bool VISX[MAXN], Visy[maxn];bool DFS (int x) {visx[x] = true; for (int y=0; y<ny; ++y) {if (visy[y]) continue; int tmp = 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; }} elseif (slack[y]> tmp) slack[y] = tmp; } return false;} int KM () {memset (linker,-1, sizeof linker); memset (ly, 0, sizeof ly); for (int i=0; i<nx; ++i) {Lx[i] =-INF; for (int j=0; j<ny; ++j) if (g[i][j]> lx[i]) lx[i] = G[i][j]; } for (int x=0, x<nx; ++x) {for (int i=0; i<ny; ++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=0; i<ny; ++i) if (!visy[i] && d>slack[i]) d = slack[i]; for (int i=0; i<nx; ++i) if (Visx[i]) lx[i]-= D; for (int i=0; i<ny; ++i) {if (Visy[i]) ly[i] + = D; else Slack[i]-= D; }}} int res = 0; for (int i=0; i<ny; ++i)if (linker[i]! =-1) Res + = G[linker[i]][i]; return res;} HDU 2255int Main () {#ifndef Online_judge freopen ("In.cpp", "R", stdin); Freopen ("Out.cpp", "w", stdout); #endif//Online_judge int n; while (~SCANF ("%d", &n)) {for (Int. i=0; i<n; ++i) for (int j=0; j<n; ++j) scan F ("%d", &g[i][j]); NX = NY = n; printf ("%d\n", KM ()); } return 0;}
hdu2255 Ben-off make money, Max right match, km algorithm