[Template] bipartite graph maximum matching algorithm and Hungary

. 1  BOOL DFS ( int u)
 2  {
 . 3      for (I = iterator_t G [u] .begin ();! G = I [u] .end (); I ++) { // each adjoining point of u 
. 4          int V = Edges [* I] .to;
 . 5          IF (! Check [V]) {      // claims path not alternately 
. 6              Check [V] = to true ; // put alternate path 
. 7              IF (matching [V] == - . 1 || DFS (matching [V])) {
 . 8                  // If the point is not a cover, as described alternate path augmenting path, the switch path, and returns a success 
. 9                  matching [V] = U;
 10                 matching [U] = V;
 . 11                  return  to true ;
 12 is              }
 13 is          }
 14      }
 15      return  to false ; // augmenting path does not exist, a failure return 
16  }
 . 17  
18 is  int hungarian ()
 . 19  {
 20 is      int ANS = 0 ;
 21 is      Memset ( matching, - . 1 , the sizeof (matching));
 22 is      for ( int U = 0 ; U <num_left; ++ U) {
 23 is          IF(matching [U] == - . 1 ) {
 24              Memset (Check, 0 , the sizeof (Check));
 25              IF (DFS (U))
 26 is                  ++ ANS;
 27          }
 28      }
 29      return ANS;
 30  }
 31 is  // https://www.cnblogs.com/wangjunyan/p/5563154.html 
32  // above the critical table below adjacency matrix
 33 is  
34 is  
35  @ bipartite graph 
36  #define MAXN 10 // represents a set of x and y vertex set the maximum number! 
37 [   int NX, NY; //the number x set of vertices and the set of Y 
38 is   int Edge [MAXN] [MAXN]; // Edge [I] [J] may be matched to a ij represents 
39   int CX [MAXN], CY [MAXN]; // with to match the record set x y elements which! 
40   int visited [MAXN]; // to record whether the vertices are visited! 
41 is   int path ( int U)
 42 is  {
 43 is       int V;
 44 is       for (V = 0 ; V <NY; V ++ )
 45       {
 46 is           IF (! Edge [U] [V] && visited [V])
 47           {
 48               visited [ V] = . 1 ;
 49             IF (cy [v] == - 1 || path (cy [v])) // If the element v y in the collection does not match or match v already, but can be found in the cy [v] in a augmenting path 
50               {
 51 is                   CX [U] = V;
 52 is                   CY [V] = U;
 53 is                   return  . 1 ;
 54 is               }
 55           }
 56 is       }
 57 is       return  0 ;
 58  }
 59   int maxmatch ()
 60  {
 61 is       int RES = 0 ;
 62 is       Memset ( CX, 0xff , sizeof(CX)); // The initial value of -1 means the two sets of elements are not matched! 
63 is       Memset (CY, 0xFF , the sizeof (CY));
 64       for ( int I = 0 ; I <= NX; I ++ )
 65       {
 66           IF (CX [I] == - . 1 )
 67           {
 68               Memset (visited, 0 , the sizeof (visitited));
 69               RES = + path (I);
 70           }
 71 is       }
 72       return RES;
 73 is  }
 74   //https://www.cnblogs.com/shenben/p/5573788.html

 

Guess you like

Origin www.cnblogs.com/xiaobuxie/p/11391871.html