求流网络的一组最小割边集

算法说明

跑一遍dinic算法求出最大流后。在残量网络中,容量大于0表示可以走。
将从源点出发可以到达的点看作S集,剩下的看作T集。如果边(u,v)满足u属于S集,v属于T集,那么该边就是最小割边集中的边。

而确定S集和边集可以通过一次DFS或BFS标记得到。

例题

UVA-10480 Sabotage

AC代码

//跑一遍dinic算法后,
//在残量网络中,从源点出发能到达的点看作S集
//剩下的看作T集,若一条边(u,v),u属于S集,v属于T集。
//那么该边就是最小割边集的边
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <map>
#include <vector>
#include <string>
#define INF 0x3f3f3f3f
using namespace std;

int S,T;//源点和汇点
const int maxe=1e5+1000;
const int maxv=1100;
struct edge
{
    int to,w,next;
}e[maxe<<1];
int head[maxv<<1],depth[maxv],cnt;
void init()
{
    memset(head,-1,sizeof(head));
    cnt=-1;
}
void add_edge(int u,int v,int w)
{
    e[++cnt].to=v;
    e[cnt].w=w;
    e[cnt].next=head[u];
    head[u]=cnt;
}
void _add(int u,int v,int w)
{
    add_edge(u,v,w);
    add_edge(v,u,w);
}

bool bfs()
{
    queue<int> q;
    while(!q.empty()) q.pop();
    memset(depth,0,sizeof(depth));
    depth[S]=1;
    q.push(S);

    while(!q.empty())
    {
        int u=q.front();q.pop();
        for(int i=head[u];i!=-1;i=e[i].next)
        {
            if(!depth[e[i].to] && e[i].w>0)
            {
                depth[e[i].to]=depth[u]+1;
                q.push(e[i].to);
            }
        }
    }
    if(!depth[T]) return false;
    return true;
}
int dfs(int u,int flow)
{
    if(u==T) return flow;
    int ret=0;
    for(int i=head[u];i!=-1 && flow;i=e[i].next)
    {
        if(depth[u]+1==depth[e[i].to] && e[i].w!=0)
        {
            int tmp=dfs(e[i].to,min(e[i].w,flow));
            if(tmp>0)
            {
                flow-=tmp;
                ret+=tmp;
                e[i].w-=tmp;
                e[i^1].w+=tmp;
            }
        }
    }
    return ret;
}
int Dinic()
{
    int ans=0;
    while(bfs())
    {
        ans+=dfs(S,INF);
    }
    return ans;
}
struct Gedge
{
    int u,v;
}a[550];//图的边集
int tot,vis[55];
void DFS(int u)
{
    vis[u]=1;//为1表示是S集的点
    for(int i=head[u];i!=-1;i=e[i].next)
    {
        int v=e[i].to;
        if(vis[v] || e[i].w<=0) continue;
        DFS(v);
    }
}
int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        if(n==0 && m==0) break;
        init();
        tot=0;
        S=1,T=2;
        while(m--)
        {
            int u,v,w;
            scanf("%d%d%d",&u,&v,&w);//给的是一个无向图
            _add(u,v,w);//注意反向边容量和正向边容量一致
            a[tot].u=u,a[tot++].v=v;
        }
        Dinic();
        memset(vis,0,sizeof(vis));
        DFS(S);//确定S集和T集
        for(int i=0;i<tot;i++)
        {
            int u=a[i].u,v=a[i].v;
            if(vis[u]&&!vis[v] || vis[v]&&!vis[u])
                printf("%d %d\n",u,v);
        }
        putchar(10);
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值