Topic: Given an n-point, M-edge of the non-weighted graph, the weight of the edge is a positive integer. To make it a non-connected graph, what is the minimum total weight value to remove?
N≤500, there is no self-ring in the figure, but there may be heavy edges (here test instructions not explained clearly).
Stoer-wagner algorithm bare topic. English Wiki: Https://en.wikipedia.org/wiki/Stoer%E2%80%93Wagner_algorithm
One of the ideas of this algorithm is: for a undirected connected graph, the selection of a two point s,t, and a s-t of the graph cut C, then "C is the graph of the global minimum cut" is "C is s-t minimum cut" is a sufficient not necessary condition.
That is, if we get the minimum cut C for a two-point s-t, then the global minimum cut is either C or another cut that keeps s,t connected. So we can merge the two nodes after we get the s-t minimum cut, and then continue to ask for a minimum cut of the other two points.
Method of merging nodes: for each node other than s,t, the weight of the set Edge (S,u) is v1 (if the edge does not exist v1=0), the weight of the Edge (T,u) is v2, then the combined new node and u have an edge, the weight value is v1+v2 (of course, if v1=0 and v2=0 do not even have this edge , the edge of the weight is 0 anyway can be arbitrarily deleted).
The process of the algorithm is:
def mincutphase (a) point set s←a while | s| < the number of points in the current graph the sum of the weights of the edges between the nodes u and all the nodes in S is w (u), select the node that does not belong to S and the largest w (u) u0 will u0 join s = Last node joined S = second to last join the node merge point of S x, yreturn before merging W (y) def mincut ()= +∞ while1 Optionally a point a = min (ans, mincutphase (a)) return ans
It can be proved that in the process of Mincutphase,W (y) is the total weight of the X-y minimum cut (see Wiki, to be honest I can not understand  ̄- ̄).
The process of finding W (u) is similar to the prim algorithm for the minimum spanning tree. Maintain a cost array and update the value of the node connected to it for each new node that joins S.
Time complexity: The complexity of a single mincutphase process is the same as the prim algorithm. Before optimization is O (n^2), if the adjacency table + heap optimization, the complexity can be reduced to O (M + N Logn).
The mincutphase process performed a total of O (N) times, so the total complexity was O (n^3) or O (MN + n^2 Logn).
O (n^3) AC code:
1#include <cstdio>2#include <cstring>3#include <algorithm>4#include <climits>5 6 using namespacestd;7 8 Const intMAXN = -+5;9 Ten intDIST[MAXN][MAXN];//Edge (U, v) does not exist if dist[u][v] = = 0 One intCOST[MAXN]; A BOOLERASED[MAXN];//whether a vertex is erased after merging - BOOLVIS[MAXN];//whether a vertex is visited during mincutphase process - intN, M; the - BOOLinput () - { - if(SCANF ("%d%d", &n, &m) = =EOF) + return false; - +memset (Dist,0,sizeof(Dist)); A for(intU, V, W, i =0; i < M; i++) at { -scanf"%d%d%d", &u, &v, &W); -DIST[U][V] = (Dist[v][u] + =W); - } - return true; - } in - ///@brief: Merge U and V, let dist[u][i] + = Dist[v][i], dist[v][i] = 0; (i.e. "erase" node V) to voidMergevertex (intUintv) + { - for(inti =0; i < N; i++) the { *Dist[i][u] = (Dist[u][i] + =dist[v][i]); $Dist[v][i] = Dist[i][v] =0;Panax Notoginseng } -ERASED[V] =true; the } + A ///@brief: Get the minimum s-t cut, and merge S and T the ///@param remn:number of remaining vertices + intMincutphase (intremn) - { $memset (Vis,0,sizeof(Vis)); $memset (Cost,0,sizeof(cost)); - - intCur = static_cast<int> (Find (erased, erased + N,false)-erased);//Find a non-erased node the intPre =cur; -Vis[cur] =true;Wuyi the for(inti =1; i < remn; i++)//repeat (remN-1) times - { Wu for(intto =0; to < N; to++) -Cost[to] + =Dist[cur][to]; About $ intMaxcost =0; - intMaxcostvertex =0; - for(intj =0; J < N; J + +) - { A if(!vis[j] && cost[j] >maxcost) + { theMaxcost =Cost[j]; -Maxcostvertex =J; $ } the } the the if(Maxcost = =0)//Not updated at all, indicating that the graph was not connected the return 0; - inPre =cur; theCur =Maxcostvertex; theVis[cur] =true; About } the the Mergevertex (pre, cur); the returnCost[cur]; + } - the intmincut ()Bayi { thememset (erased,0,sizeof(erased)); the - intAns =Int_max; - for(intRemn = N; Remn >1; --remn) the { the intt =mincutphase (remn); the if(T = =0) the return 0; -Ans =min (ans, t); the } the returnans; the }94 the intMain () the { the while(Input ())98printf"%d\n", Mincut ()); About return 0; -}
(G, W, a) {\displaystyle (G,w,a)}
| V | > 1 {\displaystyle | v|>1} minimumcutphase ( G , w , a ) {\ Displaystyle (G,w,a)} if the cut-of-the-phase is lighter than the current minimum cut then
Store the Cut-of-the-phase as the current minimum cut
POJ 2914-minimum Cut-Global minimum cut, Stoer-wagner algorithm