POJ 2914 Minimum cut minimum cut graph theory

Source: Internet
Author: User

Description

Given an undirected graph, in which-vertices can be connected by multiple edges, what is the size of the minimum cut O f the graph? i.e. how many edges must is removed at least to disconnect the graph into the subgraphs?

Input

Input contains multiple test cases. Each test case is starts with a integers n and M (2≤ n ≤ 500, 0≤ Mn x ( n ? 1)? 2) in one line, where N is the number of vertices. Following is m lines, each line contains M integers A, B and C (0≤ a, b < N, Ab, C > 0), meaning that there c edges connecting vertices A and B.

Output

There is a one line for each test case, which contains the size of the minimum cut of the graph. If the graph is disconnected, print 0.

Sample Input

3 30 1 11 2 12 0 14 30 1 11 2 12 3 18 140 1 10 2 10 3 11 2 11 3 12 3 14 5 14 6 14 7 15 6 15 7 16 7 14 0 17 3 1

Sample Output

212

Source

Baidu Star 2006 Semifinal
Wang, Ying (originator)
Chen, Shixi (Test cases)

The topic is the 06 Baidu star semi-final title, the minimum cutting problem of graph theory. It is the advanced content of graph theory.

Stoer wager algorithm, the difficulty is:

1 One-by-one find the weight of the largest edge-the process is a bit like the prime algorithm, but actually not the prime algorithm, because the purpose is not the largest spanning tree, but the need to add all the edges of a vertex. Removing these edges is the point at which the vertex is cut. Then you need to traverse the entire graph, to the last node the talent guarantee is to find all the edges of this node.

2 Indentation: The so-called contraction point is to remove the last node, at the same time to retain its edge value information, is actually to retain the vertex and other vertices connected to the minimum edge value.

More difficult to understand, generally write this report of the blog, one is either directly copied template. The second is either not explained, three is a few words to explain. The results are not well understood or even wrong;

is also very difficult to understand a topic, look at my specific gaze of the program bar.

#include <stdio.h> #include <string.h> #include <limits.h> #include <algorithm>using namespace std;const int max_n = 501;int gra[max_n][max_n];//matrix representation figure BOOL shrinkedvertices[max_n];//flag Those vertices have been indented. BOOL vis[max_n];// Flag current those nodes have visited int dis[max_n];//record maximum distance int lastsec, last;//record two vertices of each last cut edge int getlastcut (int leftvertices, int N)// Each calculation of the remaining vertices of the vertex calculation can be {fill (dis, dis+n, 0), fill (Vis, vis+n, false), int curver = 0;//curver represents the currently selected vertex, initially picking 0 vertices lastsec = Last = The main function of the 0;//loop is to add up all the edges of a vertex. There is only one last choice to make sure that all the edges of the last vertex are added up. for (int i = 1; i < leftvertices; i++) {//operation is edge. The edge is 1 less than the vertex, so I start from 1, not starting from 0 for (int v = 1; v < n; v++) {//0 vertex is first selected, so v starts from 1, not from 0 if (!vis[v] &&!shrinkedvertices[ V]) Dis[v] + = Gra[v][curver];} The main point is to add up all the edges of a vertex int maxcut = 0;//Select the current largest cut edge. Not to the last point.  There is no guarantee that the true cut edge for (int v = 1; v < n; v++) {if (!vis[v] &&!shrinkedvertices[v] && dis[v] > Maxcut) {maxcut = Dis[v];curver = V;}} if (!maxcut) return 0;//is a separate diagram, cut edges can be zero.

Vis[curver] = True;lastsec = last; Last = curver;//Each successive save two vertices}return dis[last];} int Stoer_wagner (int n) {fill (shrinkedvertices, Shrinkedvertices+n, false), int mincut = int_max;for (int i = n; i > 1; i --) {mincut = min (mincut, getlastcut (i, N)), if (!mincut) return 0;shrinkedvertices[last] = true;for (int v = 0; v < n; v + +) {if (!shrinkedvertices[v]) gra[lastsec][v] = gra[v][lastsec] + = min (Gra[v][last], gra[last][lastsec]);// In fact, the pinch point is to keep the side of the edge, the minimum edge must be retained to ensure that the minimum cut. }}return Mincut = = Int_max? 0:mincut;} int main () {int N, M, U, V, w;while (~scanf ("%d%d", &n, &m)) {for (int i = 0; i < N; i++) {for (int j = 0; J < N J + +) {Gra[i][j] = 0;}} for (int i = 0; i < M; i++) {scanf ("%d%d%d", &u, &v, &w); Gra[u][v] = Gra[v][u] + = w;} printf ("%d\n", Stoer_wagner (n));} return 0;}


Only the above procedures are inefficient. Then you can optimize it. The main method of optimization is to use an array to save the remaining vertices, and then delete the vertex directly when you indent the point. It's not the next time you need to traverse the inferred vertex.

This optimizes the constant term, the actual execution time is about 2 to 3 times times faster, the effect is very good.

#include <stdio.h> #include <string.h> #include <limits.h> #include <algorithm>using namespace std;const int max_n = 501;int gra[max_n][max_n];int vps[max_n];bool vis[max_n];int dis[max_n];int last, Sec;int getLastCu T (int v) {Fill (Vis, vis+v, false), fill (dis, dis+v, 0), last = sec = 0;int id = 0;for (int i = 1; i < V; i++) {int V = vps[ id];for (int j = 1; j < V; J + +) {if (!vis[j]) dis[j] + = Gra[v][vps[j]];} int m = 0;for (int j = 1; j < V; J + +) {if (!vis[j] && m < dis[j]) m = dis[j], id = j;} if (!m) return 0;vis[id] = True;sec = last; last = Vps[id];} Swap (Vps[id], Vps[v-1]); return dis[id];} int Stoer_wagner (int n) {for (int i = 0; i < n; i++) vps[i] = I;int mincut = int_max;for (int V = n; V > 1;  v--) {mincut = min (mincut, Getlastcut (V)), if (!mincut) return 0;for (int i = 0; i < V; i++) {int V = vps[i];gra[v][sec] = GRA[SEC][V] + = min (Gra[v][last], gra[last][sec]);}} return mincut = = Int_max? 0:mincut;} int main () {int Ver, Edge, U, V, w;while (~sCANF ("%d%d", &ver, &edge)) {for (int i = 0; i < ver; i++) for (int j = 0; J < Ver; J + +) Gra[i][j] = 0;for (int i = 0; i < Edge; i++) {scanf ("%d%d%d", &u, &v, &w); Gra[u][v] = Gra[v][u] + = w;} printf ("%d\n", Stoer_wagner (Ver));} return 0;}



Copyright notice: The author Jing Heart, Jing Space address: http://blog.csdn.net/kenden23/. Reproduced only by the author's consent.

POJ 2914 Minimum cut minimum cut graph theory

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.