Hdu2255 running well to make big money, maximum right matching, KM Algorithm

Source: Internet
Author: User

Click Open Link



Max Weight Matching

KM Algorithm

Algorithm steps:

Set the top mark of vertex XI to a [I], and the top mark of vertex Yi to B [I]

I. initially, a [I] is the maximum weight of the edge associated with Xi, B [J] = 0, ensure that a [I] + B [J]> = W (I, j) is true

Ii. When the equal subgraph does not contain a complete match, modify the top mark to expand the equal subgraph until a complete match is found.

Iii. Methods for modifying the top mark

After finding the staggered path from Xi, a tree is generated, and all its leaf nodes are x nodes, which reduces the D value for the top mark of X vertices in the staggered tree, D value is added to the top mark of Y vertex. For all edges (I, j) in the graph ),

You can see:

Neither I nor J is in the staggered tree, And the edge (I, j) is still not an equal subgraph.

Both I and J are in the staggered tree, And the edge (I, j) still belongs to the same subgraph.

I is not in the staggered tree. J is in the staggered tree. A [I] + B [J] expands, and edge (I, j) does not belong to an equal subgraph.

I is in the staggered tree, J is not in the staggered tree, and edge (I, j) may be added to an equal subgraph.

To ensure that a [I] + B [J]> = W (I, j) is always valid, and at least one edge is added to an equal subgraph, D = min {A [I] + B [J]-W (I, j)}, I is in the staggered tree, J is not in the staggered tree

 

Time Complexity:You need to find the O (n) Increment path. You need to modify the O (n) Increment path at most. During each increment, the enumeration edge is used to evaluate the D value. The complexity is O (n2 ), the total complexity is O (n4 ). simple optimization can be reduced to O (N3). Each y vertex has a "relaxation volume" function slack, Which is initialized to an infinite number every time the augmented path is searched. When you check the edge (I, j) in the process of searching for the augmented path, if it is not in the same subgraph, the slack [J] is converted into a smaller value of the original value and a [I] + B [J]-W [I, j. In this way, the minimum value of the slack value of all y vertices not in the staggered tree can be used as the D value when the top mark is modified. Note that after the top mark is modified, all slack values must be subtracted from D.


# Include <cmath> # include <cstdio> # include <cstring> # include <iostream> # include <algorithm> # include <set> # include <map> # include <stack> # include <queue> # include <vector> # include <string> # define for0 (, b) For (a = 0; A <B; ++ A) # define for1 (a, B) for (a = 1; A <= B; ++) # 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 ll; const int maxn = 310; const int in F = 1e9;/* km algorithm * O (nx * Nx * NY) * Find the maximum weight match * If the minimum weight match is obtained, the opposite number of weights can be obtained, returns the opposite number. */Int nx, NY; int G [maxn] [maxn]; int linker [maxn], LX [maxn], Ly [maxn]; // The matching status of each point in Y, 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 ;}} else if (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) scanf ("% d", & G [I] [J]); Nx = ny = N; printf ("% d \ n ", km ();} return 0 ;}


Hdu2255 running well to make big money, maximum right matching, KM Algorithm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.