【最小割】POJ-2914 Minimum Cut

Minimum Cut
Time Limit: 10000MS Memory Limit: 65536K
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 M integers 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


题意:给出一个图,点之间有若干条边,问最少需要割断多少条边使得该图变得不连通。
思路:明显的最小割,点之间边的数量相当于权值。一般来说使用最大流的方法复杂度会很高 因此使用Stoer-Wagner算法。
关于这个算法找到以下描述:

  1. min=MAXINT,固定一个顶点P
  2. 从点P用类似prim的算法扩展出“最大生成树”,记录最后扩展的顶点和最后扩展的边
  3. 计算最后扩展到的顶点的切割值(即与此顶点相连的所有边权和),若比min小更新min
  4. 合并最后扩展的那条边的两个端点为一个顶点(当然他们的边也要合并)
  5. 转到2,合并N-1次后结束
  6. min即为所求,输出min

这份代码可以在3000+ms过该题,我终于看懂了node数组的作用
正是为了降低复杂度。
每次建立完一棵生成树的最后一条边的时候,都要判断它是不是最小割,换句话说,更新最小割,然后将这条边上的两个点合并,之后更新整张图,那么删除了这个点之后,如果将它“变成”最后一个点,那么下次就不需要枚举了。复杂度就是这样降下来了。

node[k] = node[--now];

代码如下:

/*
 * ID: j.sure.1
 * PROG:
 * LANG: C++
 */
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <ctime>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <climits>
#include <iostream>
#define PB push_back
#define LL long long
using namespace std;
const int INF = 0x3f3f3f3f;
const double eps = 1e-8;
/****************************************/
const int N = 505;
int G[N][N];
int dis[N], node[N];
bool vis[N];
int n, m;

int min_cut(int now)
{
    int ret = INF;
    for(int i = 0; i < n; i++) node[i] = i;
    while(now > 1) {//重复建立n-1次最大生成树
        int k, pre = 0;
        memset(vis, 0, sizeof(vis));
        memset(dis, 0, sizeof(dis));
        for(int i = 1; i < now; i++) {
            k = -1;
            for(int j = 1; j < now; j++) if(!vis[node[j]]) {
                dis[node[j]] += G[node[pre]][node[j]];
                if(k == -1 || dis[node[k]] < dis[node[j]]) {
                    k = j;
                }
            }//找出最远的点k
            vis[node[k]] = true;//作标记
            if(i == now - 1) {//当找到生成树的最后一条边的时候,进行合并操作并更新图
                ret = min(ret, dis[node[k]]);
                for(int j = 0; j < now; j++) {
                    G[node[pre]][node[j]] += G[node[j]][node[k]];
                    G[node[j]][node[pre]] = G[node[pre]][node[j]];
                }
                node[k] = node[--now];//利用马甲将k移动到最后并删除
            }
            pre = k;
        }
    }
    return ret;
}

int main()
{
#ifdef J_Sure
    freopen("000.in", "r", stdin);
    //freopen("999.out", "w", stdout);
#endif
    while(~scanf("%d%d", &n, &m)) {
        memset(G, 0, sizeof(G));
        int u, v, w;
        while(m--) {
            scanf("%d%d%d", &u, &v, &w);
            G[u][v] += w;
            G[v][u] += w;
        }
        int ans = min_cut(n);
        printf("%d\n", ans);
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章