POJ2914 Minimum Cut(最小割模板题)

本文介绍了一种解决无向图中最小割问题的方法。通过多次搜索来逐步减少顶点数并合并相邻顶点,最终找到使得图断开所需的最少边数。输入包括顶点数、边数及连接信息,输出为最小割大小。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Minimum Cut
Time Limit: 10000MS Memory Limit: 65536K
Total Submissions: 9122 Accepted: 3820
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 ≤ MN × (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, AB, 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
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
#define maxn 1005
#define inf 1000000007
int mp[maxn][maxn];
int N, M;
bool combine[maxn];
int minC = inf;
void Search(int &s, int &t){
    bool vis[maxn];
    int w[maxn];
    memset(vis, 0, sizeof vis);
    memset(w, 0, sizeof w);
    int tmpj;
    for(int i = 0;i < N;i++){
        int MAX = -inf;
        for(int j = 0;j < N;j++){
            if(!vis[j]&&!combine[j]&&MAX<w[j]){
                MAX = w[j];
                tmpj = j;
            }
        }
        if(t==tmpj) {
            minC=w[t];
            return ;
        }
        vis[tmpj] = 1;
        s = t, t = tmpj;
        for(int j = 0;j < N;j++){
            if(!vis[j]&&!combine[j])
                w[j]+=mp[t][j];
        }
    }
    minC=w[t];
}

int mincut(){
    int ans = inf;
    int s, t;
    memset(combine, 0, sizeof combine);
    for(int i = 0;i < N-1;i++){
        s = t = -1;
        Search(s, t);
        combine[t] = true;
        ans=ans>minC?minC:ans;
        for(int j = 0;j < N;j++){
            mp[s][j] += mp[t][j];
            mp[j][s] += mp[j][t];
        }
    }
    return ans;
}
int main()
{
    while(~scanf("%d %d", &N, &M)){
        memset(mp, 0, sizeof mp);
        int u, v, w;
        while(M--){
            scanf("%d %d %d", &u, &v, &w);
            mp[u][v]+=w;
            mp[v][u]+=w;
        }
        printf("%d\n", mincut());
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值