您的位置:首页 > 其它

POJ Minimum Cut

2016-12-28 18:47 281 查看
Minimum Cut

Time Limit: 10000MSMemory Limit: 65536K
Total Submissions: 9302Accepted: 3902
Case 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 Mintegers A, B and C (0 ≤ A, B < N, A ≠ B, C > 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 3
0 1 1
1 2 1
2 0 1
4 3
0 1 1
1 2 1
2 3 1
8 14
0 1 1
0 2 1
0 3 1
1 2 1
1 3 1
2 3 1
4 5 1
4 6 1
4 7 1
5 6 1
5 7 1
6 7 1
4 0 1
7 3 1

Sample Output

2
1
2

Source

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

[Submit] [Go Back] [Status] [Discuss]

无向图的边连通度,有一个神奇的算法,还有我蒟蒻的模板。

#include <cstdio>
#include <cstring>

inline int min(int a, int b)
{
return a < b ? a : b;
}

const int inf = 2e9;
const int maxn = 505;

int n, m;
int vis[maxn];
int wet[maxn];
int com[maxn];
int G[maxn][maxn];

inline int search(int &S, int &T)
{
memset(vis, 0, sizeof(vis));
memset(wet, 0, sizeof(wet));

S = -1, T = -1;

int id, maxi, ret = 0;

for (int i = 0; i < n; ++i)
{
maxi = -inf;

for (int j = 0; j < n; ++j)
if (!com[j] && !vis[j] && wet[j] > maxi)
id = j, maxi = wet[j];

if (id == T)
return ret;

S = T;
T = id;
ret = maxi;
vis[id] = 1;

for (int j = 0; j < n; ++j)
if (!com[j] && !vis[j])
wet[j] += G[id][j];
}
}

inline int StoerWagner(void)
{
int ret = inf, S, T;

memset(com, 0, sizeof(com));

for (int i = 0; i < n - 1; ++i)
{
ret = min(ret, search(S, T));

if (!ret)return 0;

com[T] = 1;

for (int j = 0; j < n; ++j)
if (!com[j])
G[S][j] += G[T][j],
G[j][S] += G[j][T];
}

return ret;
}

signed main(void)
{
while (~scanf("%d%d", &n, &m))
{
memset(G, 0, sizeof(G));

for (int i = 1; i <= m; ++i)
{
int x, y, w;
scanf("%d%d%d", &x, &y, &w);
G[x][y] += w;
G[y][x] += w;
}

printf("%d\n", StoerWagner());
}
}


@Author: YouSiki
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: