[Kuangbin take you to fly] topic ten matching problem binary graph maximum weight matching

Source: Internet
Author: User

Maximum weight matching of binary graphs with km algorithm and network flow algorithm

KM algorithm template defaults to solving the problem of maximum weight matching and using the minimum cost maximum flow is to solve the minimum weight matching problem

Both of these methods can be used to find the maximum minimum weight of two times to take the reverse

TAT feels that the KM will be very difficult to look like ...

P hdu2255

Miles of template questions

#include <stdio.h> #include <string.h> #include <algorithm> #include <math.h> #include <map > #include <string> #include <vector> #include <queue> #include <iostream>using namespace std ; #define L Long Longbool visx[305], visy[305]; int sla[305]; int linker[305];int lx[305], ly[305];int n; int c[305][305]    ; bool Fin (int u) {Visx[u] = false;            for (int v = 1; v <= N; v + +) {if (Visy[v]) {int tmp = Lx[u] + ly[v]-c[u][v];                if (TMP = = 0) {Visy[v] = false;                    if (linker[v] = = 1 | | fin (LINKER[V])) {Linker[v] = u;                return true;            }} else {sla[v] = min (sla[v], TMP); }}} return false;}    int km () {memset (linker,-1, sizeof (linker));    memset (ly, 0, sizeof (ly));        for (int i = 1; I <= n; i + +) {Lx[i] = 0; for (int j = 1; J <= N; j + +) {lx[I] = max (Lx[i], c[i][j]);        }} for (int i = 1; i<= n; i + +) {for (int j = 1; J <= N; j + +) sla[j] = 999999999;            while (true) {memset (VISX, True, sizeof (VISX));            Memset (Visy, True, sizeof (Visy));            if (Fin (i)) break;            int d = 999999999;                for (int j = 1; j<= N; j + +) {if (Visy[j]) {d = min (d, sla[j]); }} for (int j = 1; j<= N; j + +) {if (visx[j] = = False) {Lx[j]                -= D;                } if (visy[j] = = False) {Ly[j] + = D;                } else {Sla[j]-= D;    }}}} int res = 0;        for (int j = 1; J <= N; j + +) {if (linker[j]! =-1) {res + = C[linker[j]][j]; }} return res;} int main () {while (scanf ("%d", &n)!=eof) {for (int i = 1; I <= n ; i + +) {for (int j = 1; J <= N; j + +) scanf ("%d", &c[i][j]);        } int ans = km ();    printf ("%d\n", ans); }}

Q hdu3488

is a requirement to connect all points to some rings and the addition and minimization of edge weights

The consolidation of the rings can be done with the minimum cost maximum flow

can be converted to each point connected to two edges so that the total edge and minimum of the minimum right to match

The map is on the left N points to the right n points then you can finally find a complete match to the left of the X point and the right X point has an edge connected

KM algorithm and network solution are all available km100+ms cost flow 900+ms

KM:

#include <stdio.h> #include <string.h> #include <algorithm> #include <math.h> #include <map > #include <string> #include <vector> #include <queue> #include <iostream>using namespace std ; #define L Long Longbool visx[305], visy[305]; int sla[305]; int linker[305];int lx[305], Ly[305];int n, m; int c[305][    305];bool fin (int u) {Visx[u] = false;            for (int v = 1; v <= N; v + +) {if (Visy[v]) {int tmp = Lx[u] + ly[v]-c[u][v];                if (TMP = = 0) {Visy[v] = false;                    if (linker[v] = = 1 | | fin (LINKER[V])) {Linker[v] = u;                return true;            }} else {sla[v] = min (sla[v], TMP); }}} return false;}    int km () {memset (linker,-1, sizeof (linker));    memset (ly, 0, sizeof (ly));        for (int i = 1; I <= n; i + +) {Lx[i] =-999999999;  for (int j = 1; J <= N; j + +) {          Lx[i] = max (Lx[i], c[i][j]);        }} for (int i = 1; i<= n; i + +) {for (int j = 1; J <= N; j + +) sla[j] = 999999999;            while (true) {memset (VISX, True, sizeof (VISX));            Memset (Visy, True, sizeof (Visy));            if (Fin (i)) break;            int d = 999999999;                for (int j = 1; j<= N; j + +) {if (Visy[j]) {d = min (d, sla[j]); }} for (int j = 1; j<= N; j + +) {if (visx[j] = = False) {Lx[j]                -= D;                } if (visy[j] = = False) {Ly[j] + = D;                } else {Sla[j]-= D;    }}}} int res = 0;        for (int j = 1; J <= N; j + +) {if (linker[j]! =-1) {res + = C[linker[j]][j]; }} return res;}    int main () {int t;    scanf ("%d", &t);     while (t--) {   scanf ("%d%d", &n,&m);        for (int i = 1; i<= n; i + +) {for (int j = 1; J <= N; j + +) c[i][j] = 999999999;            } for (int i = 1; I <= m; i + +) {int u, V, W;            scanf ("%d%d%d", &u,&v,&w);        C[U][V] = max (C[u][v],-W);        } int ans = km ();    printf ("%d\n",-ans); }}

Charge Flow:

#include <stdio.h> #include <string.h> #include <algorithm> #include <math.h> #include <map > #include <string> #include <vector> #include <queue> #include <iostream>using namespace std ; #define L long longint n; int m; int cnt; struct ed{int V, NEX, cap, flow, cost;}    e[405 * 405]; int head[405];int tol;int pre[405];int dis[405];bool vis[405];void init () {cnt = 0; Memset (Head,-1, sizeof (head));}    void Add (int u, int v, int cap, int cost) {e[cnt].v = v;    E[cnt].nex = Head[u];    E[cnt].cap = cap;    E[cnt].cost = Cost;    E[cnt].flow = 0;    Head[u] = cnt;    CNT + +;    E[CNT].V = u;    E[cnt].nex = Head[v];    E[cnt].cap = 0;    E[cnt].cost =-cost;    E[cnt].flow = 0;    HEAD[V] = cnt; CNT + +;}    BOOL SPFA (int s, int t) {Queue<int >que;        for (int i = 0; i<= 2*n + 1; i + +) {Dis[i] = 999999999;        Vis[i] = false;    Pre[i] =-1;    } Dis[s] = 0;    Vis[s] = true;    Que.push (s); WhilE (!que.empty ()) {int u = que.front (); Que.pop ();        Vis[u] = false;            for (int i = head[u]; i =-1; i = E[i].nex) {int v = E[I].V;                if (E[i].cap > E[i].flow && dis[v] > Dis[u] + e[i].cost) {Dis[v] = Dis[u] + e[i].cost;                PRE[V] = i;                    if (vis[v] = = False) {Vis[v] = true;                Que.push (v);    }}}} if (pre[t] = = 1) return false; else return true;}    int fyl (int s, int t, int &cost) {int flow = 0;    Cost = 0;        while (SPFA (s,t)) {int minn = 999999999; for (int i = pre[t]; i =-1; i = pre[e[i^1].v]) {if (Minn > e[i].cap-e[i].flow) {minn = e[            I].cap-e[i].flow;            }} for (int i = pre[t]; I! =-1; i = pre[e[i^1].v]) {e[i].flow + = Minn;            E[i^1].flow-= Minn;        Cost + = E[i].cost * MINN; } Flow+ = Minn; } return flow;}    int main () {int t;    scanf ("%d", &t);        while (t--) {init ();        scanf ("%d%d", &n,&m);            for (int i = 1; I <= m; i + +) {int u, V, W;            scanf ("%d%d%d", &u,&v,&w);        Add (U,V+N,1,W);            } for (int i = 1; I <= n; i + +) {Add (0,i,1,0);        Add (i+n, N*2 + 1, 1, 0);        } int cost;        int flow = FYL (0,n*2+1,cost);    printf ("%d\n", cost); }}

[Kuangbin take you to fly] topic ten matching problem binary graph maximum weight matching

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.