hdu-6214 Smallest Minimum Cut(最小割的最少边)

本文介绍了一个网络中寻找最小割的方法,通过使用dinic算法来计算源点到汇点的最小割大小。文章提供了完整的C++代码实现,并通过样例输入输出展示了算法的应用。

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

Problem Description
Consider a network G=(V,E) with source s and sink t. An s-t cut is a partition of nodes set V into two parts such that s and t belong to different parts. The cut set is the subset of E with all edges connecting nodes in different parts. A minimum cut is the one whose cut set has the minimum summation of capacities. The size of a cut is the number of edges in the cut set. Please calculate the smallest size of all minimum cuts.

Input
The input contains several test cases and the first line is the total number of cases T (1≤T≤300).
Each case describes a network G, and the first line contains two integers n (2≤n≤200) and m (0≤m≤1000) indicating the sizes of nodes and edges. All nodes in the network are labelled from 1 to n.
The second line contains two different integers s and t (1≤s,t≤n) corresponding to the source and sink.
Each of the next m lines contains three integers u,v and w (1≤w≤255) describing a directed edge from node u to v with capacity w.

Output
For each test case, output the smallest size of all minimum cuts in a line.

Sample Input
2
4 5
1 4
1 2 3
1 3 1
2 3 1
2 4 1
3 4 2
4 5
1 4
1 2 3
1 3 1
2 3 1
2 4 1
3 4 3

Sample Output
2
3

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#define LL long long
#define inf 0x3f3f3f3f

using namespace std;
const int maxn=210;
struct node
{
    int u,v,w,next;
}edge[maxn*20];
int n,m,sp,tp,k;
int head[maxn];
int d[maxn];

void addedge(int u,int v,int w)
{
    edge[k].u=u;
    edge[k].v=v;
    edge[k].w=w;
    edge[k].next=head[u];
    head[u]=k++;

    edge[k].u=v;
    edge[k].v=u;
    edge[k].w=0;
    edge[k].next=head[v];
    head[v]=k++;
}

int bfs()
{
    queue<int> q;
    memset(d,-1,sizeof(d));
    d[sp]=0;
    q.push(sp);
    while(!q.empty())
    {
        int a=q.front();
        q.pop();
        for(int i=head[a];i!=-1;i=edge[i].next)
        {
            int b=edge[i].v;
            if(d[b]==-1&&edge[i].w>0)
            {
                d[b]=d[a]+1;
                q.push(b);
            }
        }
    }
    return d[tp]!=-1;
}

LL dfs(int a,LL b)
{
    LL r=0;
    if(a==tp)return b;
    for(int i=head[a];i!=-1&&r<b;i=edge[i].next)
    {
        int u=edge[i].v;
        if(edge[i].w>0&&d[u]==d[a]+1)
        {
            int x=min((LL)edge[i].w,b-r);
            x=dfs(u,x);
            r+=x;
            edge[i].w-=x;
            edge[i^1].w+=x;
        }
    }
    if(!r)d[a]=-2;
    return r;
}

LL dinic(int sp,int tp)
{
    LL sum=0,t;
    while(bfs())
    {
        while(t=dfs(sp,inf))
        {
            sum+=t;
        }
    }
    return sum;
}

int main()
{
    int T;
    int u,v,w;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&m);
        scanf("%d%d",&sp,&tp);
        k=0;
        memset(head,-1,sizeof(head));
        for(int i=0;i<m;i++)
        {
            scanf("%d%d%d",&u,&v,&w);
            addedge(u,v,w*1000+1);
        }
        printf("%lld\n",dinic(sp,tp)%1000);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值