poj 2914

        这是一道求全局最小割的题目,可以用Stoer Wagner算法。题目很短,但是有一点不要混乱了,就是这里权值代表两点间边的条数,所以要区分好是权值还是真的边数。这题要求权值最小的割集。

        下面是Stoer Wagner算法的介绍(摘自:http://poj.org/showmessage?message_id=117503):

        Stoer-Wagner 算法用来求无向图 G=(V, E)的全局最小割。

        算法基于这样一个定理:对于任意s, t  ∈V ,全局最小割或者等于原图的s-t 最小割,或者等于将原图进行 Contract(s, t)操作所得的图的全局最小割。

        算法框架:
            1. 设当前的最小割MinCut 为+∞
            2. 在 G中求出任意 s-t 最小割 c,MinCut = min(MinCut, c)
            3. 对 G作 Contract(s, t)操作,得到 G'=(V', E'),若|V'| > 1,则G=G'并转 2,否则MinCut 为原图的全局最小割 

            若不存在边(p, q),则定义边(p, q)权值w(p, q) = 0 
            Contract 操作定义:               
                  Contract(a, b): 删掉点 a, b 及边(a, b),加入新节点 c,对于任意 v  ∈V ,w(v, c) = w(c, v) = w(a, v) + w(b, v)

            求 G=(V, E)中任意 s-t 最小割的算法: 定义w(A, x) = ∑w(v[i], x),v[i]  ∈A, 定义 Ax 为在x 前加入 A 的所有点的集合(不包括 x)
                 1. 令集合 A={a},a为 V中任意点
                 2. 选取 V - A中的 w(A, x)最大的点 x加入集合 A
                 3. 若|A|=|V|,结束
                 令倒数第二个加入 A的点为 s,最后一个加入 A的点为 t,则s-t 最小割为 w(At, t)

         此外,关于这个算法还可以参考http://www.docin.com/p-48578124.html

        此题的其他解题报告:http://blog.sina.com.cn/s/blog_700906660100v7vb.htmlhttp://www.cnblogs.com/ylfdrib/archive/2010/08/17/1801784.html


代码(C++):

#include <iostream>
#include <cstdio>
#include <cstring>

#define MAX 509
#define INF 2000000000
using namespace std;

int map[MAX][MAX],n,count,w[MAX];
bool valid[MAX],inset[MAX];

void contract(int a,int b)
{
    int i;
    valid[b]=false;
    for(i=0;i<n;i++)
    {
        map[a][i]+=map[b][i];
        map[i][a]=map[a][i];
    }
}

int min_cut(int s)
{
    int i,pre,maxw,maxp,num;

    num=count;
    memset(w,0,sizeof(w));
    memset(inset,false,sizeof(inset));
    inset[s]=true;
    while(1)
    {
        pre=s;
        maxw=-1;
        for(i=0;i<n;i++)
        {
            if(valid[i]&&!inset[i])
            {
                w[i]+=map[s][i];
                if(maxw<w[i])
                {
                    maxw=w[i];
                    maxp=i;
                }
            }
        }
        inset[maxp]=true;
        s=maxp;
        num--;
        if(num==1)
        {
            contract(s,pre);
            return w[s];
        }
    }
}

int stoer_wagner()
{
    int i,ans;
    ans=INF;
    count=n;    //记录当前图的点的个数
    memset(valid,true,sizeof(valid));   //记录点是否被收缩
    while(count>1)
    {
        for(i=0;i<n;i++)
        {
            if(valid[i]) break;
        }
        ans=min(ans,min_cut(i));
        count--;
    }
    return ans;
}

int main()
{
    //freopen("in.txt","r",stdin);

    int m,i,a,b,c;
    while(scanf("%d %d",&n,&m)!=EOF)
    {
        memset(map,0,sizeof(map));
        for(i=0;i<m;i++)
        {
            scanf("%d %d %d",&a,&b,&c);
            map[a][b]+=c;
            map[b][a]=map[a][b];
        }

        printf("%d\n",stoer_wagner());
    }
    return 0;
}

题目( http://poj.org/problem?id=2914):

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 integersAB 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 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值