全局最小割模板 poj2914

http://poj.org/problem?id=2914
参考资料:http://www.hankcs.com/program/algorithm/poj-2914-minimum-cut.html
就是单纯的全局最小割

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;
#define M 1009
const int INF = 0x3f3f3f3f;
int G[M][M];
int w[M],v[M];
bool vis[M];
int n,m;
int sw(int n)
{
    int min_cut = INF;
    for(int i = 0;i < n;i++) v[i] = i;
    while(n > 1)
    {
        int pre = 0;
        memset(vis,false,sizeof(vis));
        memset(w,0,sizeof(w));
        for(int i = 1;i < n;i++)
        {
            int k = -1;
            for(int j = 1;j < n;j++)
            {
                if(vis[v[j]]) continue;
                w[v[j]] += G[v[pre]][v[j]];
                if(k == -1 || w[v[k]] < w[v[j]])
                {
                    k = j;
                }
            }
            vis[v[k]] = true;
            if(i == n-1)
            {
                int s = v[pre],t = v[k];
                min_cut = min(min_cut,w[t]);
                for(int j = 0;j < n;j++)
                {
                    G[s][v[j]] += G[t][v[j]];
                    G[v[j]][s] += G[v[j]][t];
                }
                v[k] = v[--n];
            }
            pre = k;
        }
    }
    return min_cut;
}
int main()
{
    while(scanf("%d %d",&n,&m) == 2)
    {
        memset(G,0,sizeof(G));
        for(int i = 0;i < m;i++)
        {
            int u,v,ww;
            scanf("%d %d %d",&u,&v,&ww);
            G[u][v] += ww;
            G[v][u] += ww;
        }
        int min_cut = sw(n);
        printf("%d\n",min_cut);
    }
    return 0;
}

你可能感兴趣的:(全局最小割模板 poj2914)