POJ-2914 Minimum Cut(全局最小割)

来源:互联网 发布:数据库安全管理制度 编辑:程序博客网 时间:2024/06/05 09:21

Minimum Cut
Time Limit: 10000MS Memory Limit: 65536KTotal Submissions: 9913 Accepted: 4158Case Time Limit: 5000MS

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 integers N and M (2 ≤ N ≤ 500, 0 ≤ M ≤ N × (N − 1) ⁄ 2) in one line, where N is the number of vertices. Following are M lines, each line contains M integers AB and C (0 ≤ AB <NA ≠ BC > 0), meaning that there C edges connecting vertices A and B.

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
模板题,找出一个图中的最小割使得整个图分成2个联通块

#include<cstdio>#include<string.h>#include<queue>#include<algorithm>using namespace std;const int MX = 505;const int inf = 0x3f3f3f3f;struct Stoer_Wagner {    int mp[MX][MX], s, t;    int v[MX], d[MX];    void init(int n) {        for (int i = 0; i < n; i++)            for (int j = 0; j < n; j++)                mp[i][j] = 0;    }    void add(int u, int v, int w) {        mp[u][v] += w;        mp[v][u] += w;    }    int solve(int n) {        int i, j, now, ret = inf;        for (i = 0; i < n; i++)v[i] = i;//下标从0开始        while (n > 1) {            for (now = 0, i = 1; i < n; i++) d[v[i]] = 0;            for (i = 1; i < n; i++) {                swap(v[now], v[i - 1]);                for (now = j = i; j < n; j++) {                    d[v[j]] += mp[v[i - 1]][v[j]];                    if (d[v[now]] < d[v[j]])now = j;                }            }            if (ret > d[v[now]]) {                ret = d[v[now]];                s = v[now - 1];                t = v[now];            }            for (j = 0; j < n; j++)                mp[v[j]][v[now - 1]] = mp[v[now - 1]][v[j]] += mp[v[j]][v[now]];            v[now] = v[--n];        }        return ret;    }} F;


原创粉丝点击