Tour -- best binary match

Source: Internet
Author: User

Question: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 3488

Note: Minimum weighted binary matching

Source code:

# Include <stdio. h> # include <string. h >#include <algorithm> # define n 305 # define INF 1e9 // do not use too large using namespace STD; int n, m, Cas, U, V, W; int G [N] [N], NX, NY; // You Need to initialize int MX [N], my [N], LX [N], Ly [N]; // lx [], Ly [] is the bool SX [N] And Sy [N] vertex numbers of Xi and Yi in the KM algorithm. // indicates whether the int Prev [N] and slack [N] are in the staggered tree; // Prev [I] indicates the front point of I in Y; slack is the amount of relaxation int Q [N * 2], Head, tail; void augment (INT v) {// augmented while (V! =-1) {int Pv = mx [Prev [v]; MX [Prev [v] = V; my [v] = Prev [v]; V = PV ;}} bool BFS () {While (Head! = Tail) {int P = Q [head ++], u = P> 1; if (P & 1) {If (my [u] =-1) {augment (U); Return true;} else {q [tail ++] = My [u] <1; SX [my [u] = true ;}} else For (INT I = 0; I <NY; I ++) if (SY [I]) continue; else if (LX [u] + ly [I]! = G [u] [I]) {int EX = Lx [u] + ly [I]-G [u] [I]; If (Slack [I]> ex) {slack [I] = ex; Prev [I] = u ;}} else {Prev [I] = u; Sy [I] = true; Q [tail ++] = I * 2 + 1;} return false;} int kmmatch (bool maxsum) {// The maximum weight matches int I, j, ex by default, cost = 0; If (! Maxsum) for (I = 0; I <NX; I ++) for (j = 0; j <NY; j ++) G [I] [J] * =-1; memset (MX,-1, sizeof (MX); memset (My,-1, sizeof (my )); memset (ly, 0, sizeof (ly); for (I = 0; I <NX; I ++) for (LX [I] =-INF, j = 0; j <NY; j ++) lx [I] = max (LX [I], G [I] [J]); For (INT live = 0; live <NX; live ++) {memset (sx, 0, sizeof (SX); memset (SY, 0, sizeof (SY); for (I = 0; I <NY; I ++) slack [I] = inf; head = tail = 0; Q [tail ++] = live * 2; SX [live] = true; while (! BFS () {for (EX = inf, I = 0; I <NY; I ++) if (! Sy [I]) EX = min (ex, slack [I]); for (I = 0; I <NX; I ++) if (SX [I]) lx [I]-= ex; For (j = 0; j <NY; j ++) {If (SY [J]) ly [J] + = ex; slack [J]-= ex;} for (I = 0; I <NY; I ++) if (! Sy [I] & slack [I] = 0) {q [tail ++] = I * 2 + 1; Sy [I] = true ;}}} if (! Maxsum) for (I = 0; I <NX; I ++) for (j = 0; j <NY; j ++) G [I] [J] * =-1; for (I = 0; I <NX; I ++) Cost + = G [I] [MX [I]; return cost;} int main () {// freopen ("F: \ a.txt", "r", stdin); scanf ("% d", & CAS ); while (CAS --) {scanf ("% d", & N, & M); For (INT I = 0; I <n; I ++) for (Int J = 0; j <n; j ++) g [I] [J] =-INF; For (INT I = 0; I <m; I ++) {scanf ("% d", & U, & V, & W); If (G [U-1] [V-1] <-W) G [U-1] [V-1] =-W;} Nx = ny = N; printf ("% d \ n",-kmmatch (true ));}}

Related Article

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.