Still not very understand, post two study blog:
Http://www.hankcs.com/program/algorithm/poj-2914-minimum-cut.html
Http://blog.sina.com.cn/s/blog_700906660100v7vb.html
Algorithm steps:
1. Set minimum cut Cut=inf, select a point s to set a, define W (A, p) as the sum of the weights of all points in a and a point outside a.
2. Update W (a,p) for the s selected just now (this value increments).
3. Select a point P outside a, and W (A,P) the largest as the new S, if A!=g (V), then continue 2.
4. Note the last entry to a of two points as s and T, with W (a,t) to update the cut.
5. Merge St, i.e. new vertex u, Edge right W (u, v) =w (S, v) +w (T, v), delete vertices s and t, and edges connected to them.
6. If | v|! The =1 continues 1.
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <algorithm>5 using namespacestd;6 7 #defineMax_n 500 + 168 #defineINF 0x3f3f3f3f9 Ten intG[max_n][max_n]; One intV[max_n];//V[i] represents the vertex to which node I is merged A intW[max_n];//define W (a,x) =∑w (v[i],x), V[i]∈a - BOOLVisited[max_n];//used to mark whether the point has been added to a collection - the intStoer_wagner (intN) - { - intMin_cut =INF; - for(inti =0; I < n; ++i) + { -V[i] = i;//the initial is not merged, all of which represent the node itself + } A at while(N >1) - { - intPre =0;//The pre is used to indicate the point before adding a set (a point added before T) -Memset (visited,0,sizeof(visited)); -Memset (W,0,sizeof(w)); - for(inti =1; I < n; ++i) in { - intK =-1; to for(intj =1; J < N; ++J)//Select W (a,x) largest point x in V-a to join the collection + { - if(!Visited[v[j]]) the { *W[V[J]] + =G[v[pre]][v[j]]; $ if(k = =-1|| W[v[k]] <W[v[j]])Panax Notoginseng { -K =J; the } + } A } the +Visited[v[k]] =true;//mark the point X has joined a set - if(i = = N-1)//If | a|=| V| (all points are added a), end $ { $ Const ints = v[pre], t = v[k];//make the second-to-last point (V[pre]) to S, and the final point with a (V[k]) is T -min_cut = min (min_cut, w[t]);//The s-t min Cut is w (a,t), updated with its min_cut - for(intj =0; J < N; ++J)//contract (S, T) the { -G[S][V[J]] + =G[v[j]][t];WuyiG[v[j]][s] + =G[v[j]][t]; the } -V[K] = V[--n];//Delete the last point (that is, delete T and merge T into s) Wu } - //Else Continue AboutPre =K; $ } - } - returnMin_cut; - } A + intMainintargcChar*argv[]) the { - intN, M; $ while(SCANF ("%d%d", &n, &m)! =EOF) the { thememset (G,0,sizeof(G)); the while(m--) the { - intu, V, W; inscanf"%d%d%d", &u, &v, &W); theG[U][V] + =W; theG[v][u] + =W; About } theprintf"%d\n", Stoer_wagner (n)); the } the return 0; +}
POJ2914 (unresolved) non-graph minimal cut | Stoer-wagner Algorithm | templates