无向图的最小割 hdu 3002 Stoer-wagner算法

King of Destruction

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1171    Accepted Submission(s): 479


Problem Description
Zhou xingxing is the successor of one style of kung fu called "Karate Kid".he is falling love with a beautiful judo student,after being humiliated by her boyfriend,a Taekwando master from Japan,Zhou is going to fight with his rival in love.The way they fight is to destroy the wooden plank between some wooden pegs,in order to cut these wooden pegs into two disconnected parts,and destroy each piece of plank need consume different energy.However Zhou xingxing is beginner after all,so he is turn to you for help,please calculate the minimum energy he need to destroy the wooden plank.
 

Input
The input consists of multiple test cases.
Each test case starts with two integers n (0 < n <= 100) and m in one line, where n、m are the number of wooden pegs and wooden plank.
Following are m lines, each line contains three integers s, e and q (0 <= s, e < n,q > 0), meaning that there need q energy to destroy the wooden plank between s and e.
 

Output
There is only one line for each test case, which contains the minimum energy they need to complete this fight.
 

Sample Input
  
  
2 1 0 1 50 3 2 0 1 50 1 2 10
 

Sample Output
  
  
50 10
 

Source

题解:
无向图的全局最小割算法,模板,但是重边要累加起来。
最小割算法的主要思想:
每次相当于构建一个最大生成树;
然后将最后两个进入最大生成树集合的点,合并;
合并后继续构建;
直到合并为一个点时。

代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int inf = 0x3f3f3f3f;
const int maxn = 100+10;
int map[maxn][maxn];
int vis[maxn];
int node[maxn];
int dis[maxn];

int Stoer_wagner(int N) {
    int pre = 0;
    int ans = inf;
    memset(dis, 0, sizeof(dis));
    for (int i=0; i<N; i++)
        node[i] = i;
    int maxj, maxx=-1;
    while (N > 1) {
        maxx = -1; pre = 0;
        for (int i=0; i<N; i++) {
            dis[node[i]] = map[node[0]][node[i]];
            if (dis[node[i]] > maxx) {
                maxx = dis[node[i]];
                maxj = i;
            }
        }
	memset(vis, 0, sizeof(vis));
        pre = 0;
        vis[node[0]] = 1;
        for (int j=1; j<N; j++) {
            vis[node[maxj]] = 1;
            if (j == N-1) {
                ans = min(ans, maxx);
                for (int i=0; i<N; i++) {
                    map[node[pre]][node[i]] += map[node[maxj]][node[i]];
                    map[node[i]][node[pre]] += map[node[maxj]][node[i]];
                }
                node[maxj] = node[--N];
            }
            else 
            {
                pre = maxj;
                maxx = -1;
                for (int i=1; i<N; i++) {
                    if (!vis[node[i]]) {
                        dis[node[i]] += map[node[pre]][node[i]];
                        if (dis[node[i]] > maxx) {
                            maxx = dis[node[i]];
                            maxj = i;
                        }
                    }
                }
            }
        }
    }
    return ans;
}
          
int main()
{
	int n, m;
    while (scanf("%d%d", &n, &m)!=EOF) {
        memset(map, 0, sizeof(map));
        for (int i=0; i<m; i++) {
            int u, v, w;
            scanf("%d%d%d", &u, &v, &w);
            map[u][v] += w;
	    map[v][u] += w;
        }
        printf("%d\n", Stoer_wagner(n));
    }
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值