Poj 2914 minimum cut Graph Theory

Source: Internet
Author: User

Description

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

Input

Input contains multiple test cases. Each test case starts with two integersNAndM(2 ≤N≤ 500, 0 ≤MN× (N? 1 )? 2) in one line, whereNIs the number of vertices. Following areMLines, each line containsMIntegersA,BAndC(0 ≤A,B<N,A=B,C> 0), meaning that thereCEdges connecting verticesAAndB.

Output

There is only 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)

This is the semi-finals of Baidu star in. The minimum cut problem in graph theory is an advanced topic in graph theory.

The stoer wager algorithm has the following difficulties:

1. Search for the maximum edge weight one by one-the process is a bit like the prime algorithm, but it is not actually the prime algorithm because the purpose is not the maximum spanning tree, instead, we need to add all the edges of a vertex and remove these edges. This is the cut point value of the vertex. Then we need to traverse the entire graph, and at the end of the node, we can ensure that all the edges of the node are found.

2 point reduction: the so-called point reduction is to remove the last node, while retaining its edge value information, is actually to keep the vertex and other vertex connected to the minimum edge value.

It is hard to understand. Generally, one of the blogs that write this solution report is either directly copying templates, the other is not commenting, and the third is having a few comments, the results are not well understood or even incorrect;

It is also hard to say that it is a clear question. Let's look at the program I have commented on in detail.

# 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]; // The matrix represents the figure bool shrinkedvertices [max_n]; // mark those vertices that have been scaled down bool vis [max_n]; // mark that the current nodes have accessed int dis [max_n]; // record the maximum distance from int lastsec, last; // record the two vertices int getlastcut (INT leftvertices, int N) of the last cut edge each time. // calculate the remaining vertices for each calculation. {fill (DIS, DIS + N, 0); fill (VIS, VIS + N, false); int curver = 0; // C Urver represents the currently selected vertex. Initially, it selects 0 vertex lastsec = last = 0; // The main function of the loop is to add all the edges of a vertex. Only when the last vertex is selected can all the edges of the last vertex be added up. For (INT I = 1; I <leftvertices; I ++) {// The operation is an edge, and the edge is 1 less than the vertex. Therefore, I starts from 1, not starting from 0 for (INT v = 1; v <n; V ++) {// 0 vertex has been selected first, so V starts from 1, if (! Vis [v] &! Shrinkedvertices [v]) dis [v] + = gra [v] [curver];} // Add all edges of a vertex to int maxcut = 0; // select the current largest cut edge. 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; // It is a separation graph, and the cut edge can be zero. Vis [curver] = true; lastsec = last; last = curver; // save the last two vertices one by one} 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 contraction point is to keep one of the sides, and the minimum side needs to be kept to ensure the minimum cut .}} Return mincut = int_max? 0: mincut;} int main () {int n, m, U, V, W; while (~ Scanf ("% 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", & U, & V, & W ); gra [u] [v] = gra [v] [u] + = W;} printf ("% d \ n", stoer_wagner (n);} return 0 ;}

However, the efficiency of the above program is not high, so we can optimize it. The main method of optimization is to use an array to save the remaining vertices, so we can directly Delete the vertex when shrinking, you don't need to traverse and judge this vertex next time. In this way, constant items are optimized, and the actual running time is about 2 to 3 times faster, which is very effective.

#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 getLastCut(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;}



Poj 2914 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.