Flow Problem 网络流dinic算法 模板题

初学网络流,有很多地方还是不太懂。

借用一下大佬的讲解:https://www.cnblogs.com/SYCstudio/p/7260613.html

 

题目:

Network flow is a well-known difficult problem for ACMers. Given a graph, your task is to find out the maximum flow for the weighted directed graph.

输入:

The first line of input contains an integer T, denoting the number of test cases. 
For each test case, the first line contains two integers N and M, denoting the number of vertexes and edges in the graph. (2 <= N <= 15, 0 <= M <= 1000) 
Next M lines, each line contains three integers X, Y and C, there is an edge from X to Y and the capacity of it is C. (1 <= X, Y <= N, 1 <= C <= 1000)

输出:

For each test cases, you should output the maximum flow from source 1 to sink N.

样例输入:

2
3 2
1 2 1
2 3 1
3 3
1 2 1
2 3 1
1 3 1

样例输出:

Case 1: 1
Case 2: 2

有N 个点,M条边,问从第一个点到第N个点的最大流,用dinic算法求解。

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <algorithm>
using namespace std;
const int maxn=1005;
const int INF =0x7fffffff;
int dis[maxn],head[maxn];
int n,m;
int cnt ;
struct edge
{
    int u,v,f,c,next;
}node[2*maxn];
void add(int u,int v,int c,int f)//用邻接表建图
{
    node[cnt].u=u;
    node[cnt].v=v;
    node[cnt].c=c;//c表示从u到v的容量
    node[cnt].f=f;//f表示从u到v的反向弧
    node[cnt].next=head[u];
    head[u]=cnt++;
}
int bfs()//求增广路
{
    queue<int>q;
    memset(dis,0,sizeof(dis));
    q.push(1);
    dis[1]=1;
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        for(int i=head[u];i!=-1;i=node[i].next)
        {
            edge e=node[i];
            if(!dis[e.v]&&e.c>e.f)//e.c>e.v相当于e.c-e.v>0,意思是这条路的容量减去已经使用
            {                     //过的流量要大于0,也就是说这条路可分配的容量要大于0,否则就
                                  //没有意义了 
                dis[e.v]=dis[e.u]+1;//构建层次图
                q.push(e.v);
            }
        }
    }
    if(dis[n]!=0)return 1;
    else return 0;
}
int dfs(int u,int mx)//求这条增广路各段中的最小值,因为只有这个最小值才能流过整条路
{
    if(u==n) return mx;
    for(int i=head[u];i!=-1;i=node[i].next)
    {
        edge e=node [i];
        if(dis[e.v]==dis[e.u]+1&&e.c>e.f)
        {
            int v=dfs(e.v,min(mx,e.c-e.f));//mx是已经遍历过的最小值,e.c-e.v是要遍历的值,一            
                                           //直求最小
            if(v>0)
            {
                node[i].c-=v;//流量减去这个v
                node[i^1].c+=v;//反向弧加上这个v
                return v;
            }
        }
    }
    return 0;
}
int dinic()
{
    int maxflow=0;
    while(bfs())//如果存在增广路
    {
        while(int res=dfs(1,INF))//就找这条增广路的最小值,加到maxflow中
            maxflow+=res;
    }
    return maxflow;
}
int main()
{
    int T;
    int S,E,C;
    scanf("%d",&T);
    int cas=1;
    while(T--)
    {
        memset(head,-1,sizeof(head));
        cnt=0;
        scanf("%d%d",&n,&m);
        for(int i=0;i<m;i++)
        {
            scanf("%d%d%d",&S,&E,&C);
            add(S,E,C,0);
            add(E,S,0,0);
        }
        cout<<"Case "<<cas++<<": "<<dinic()<<endl;
    }
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值