Maximum Weighted binary match

Source: Internet
Author: User

There is a classic algorithm in graph theory, that is, binary matching, but simple matching sometimes cannot solve our problems, such as matching with weights, an important problem is allocation. For example, if you assign m tasks to n people, each person has different costs. How can you minimize the cost, this problem is collectively referred to as the maximum weight matching problem of a bipartite graph.

The best way to solve this type of problem is the KM algorithm. You can use Baidu for details!

The following is my code. I mainly use classes to write the code. I think this is a better understanding!


# Include
 
  
# Include
  
   
# Include
   
    
Using namespace std; # define INF 99999999 class graph {struct vertex {bool vist; // whether the int link has been accessed; // top Mark int ver; // match edge vector
    
     
Edge; // related edge vertex (bool f = 0, int v = 0, int l =-INF): vist (f), ver (v), link (l) {}}; int n, m; vector
     
      
V; vector
      
        U; vector
       
         Slack; public: graph (int x, int y): n (x), m (y) {vertex tmp; int I = 0; for (; I <= n; I ++) V. push_back (tmp), V [I]. edge. push_back (0); for (I = 0; I <= m; I ++) U. push_back (tmp), slack. push_back (INF);} void merge (int I, int w) {V [I]. edge. push_back (w); V [I]. link = V [I]. link> w? V [I]. link: w;} bool DFS (int x) {V [x]. vist = 1; for (int y = 1; y <= m; y ++) {if (U [y]. vist) continue; int tmp = V [x]. link + U [y]. link-V [x]. edge [y]; if (tmp = 0) {U [y]. vist = 1; if (U [y]. ver = 0 | DFS (U [y]. ver) {U [y]. ver = x; V [x]. ver = y; return 1 ;}} else if (slack [y]> tmp) slack [y] = tmp ;}return 0 ;} int KM () {for (int x = 1; x <= n; x ++) {for (int I = 1; I <= m; I ++) slack [I] = INF; while (1) {for (int I = 1; I <= n; I ++) V [I]. vist = 0; (Int I = 1; I <= m; I ++) U [I]. vist = 0; if (DFS (x) break; int d = INF; for (int I = 1; I <= m; I ++) if (! U [I]. vist & d> slack [I]) d = slack [I]; for (int j = 1; j <= n; j ++) if (V [j]. vist) V [j]. link-= d; for (int k = 1; k <= m; k ++) if (U [k]. vist) U [k]. link + = d; elseslack [k]-= d ;}} int MAX = 0; for (int I = 1; I <= m; I ++) {if (V [I]. ver> 0) MAX + = V [U [I]. ver]. edge [I];} return MAX ;}}; int main () {// freopen ("in.txt", "r", stdin); // freopen ("out.txt ", "w", stdout); int n, m; while (cin> n> m) {graph G (n, n); int w; for (int I = 1; I <= n; I ++) for (int j = 1; j <= n; j ++) scanf ("% d ", & w), G. merge (I,-w); int max =-G. KM (); cout <
        <
          The efficiency of this algorithm is O (n ^ 3). This should be the best algorithm at present!
         

<

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.