网络流之最大流算法——EK算法(通俗讲解)

先放道模板题来说明网络流:
Power Network
A power network consists of nodes (power stations, consumers and dispatchers) connected by power transport lines. A node u may be supplied with an amount s(u) >= 0 of power, may produce an amount 0 <= p(u) <= p max(u) of power, may consume an amount 0 <= c(u) <= min(s(u),c max(u)) of power, and may deliver an amount d(u)=s(u)+p(u)-c(u) of power. The following restrictions apply: c(u)=0 for any power station, p(u)=0 for any consumer, and p(u)=c(u)=0 for any dispatcher. There is at most one power transport line (u,v) from a node u to a node v in the net; it transports an amount 0 <= l(u,v) <= l max(u,v) of power delivered by u to v. Let Con=Σ uc(u) be the power consumed in the net. The problem is to compute the maximum value of Con.

这里写图片描述

The label x/y of power station u shows that p(u)=x and p max(u)=y. The label x/y of consumer u shows that c(u)=x and c max(u)=y. The label x/y of power transport line (u,v) shows that l(u,v)=x and l max(u,v)=y. The power consumed is Con=6. Notice that there are other possible states of the network but the value of Con cannot exceed 6.
Input
There are several data sets in the input. Each data set encodes a power network. It starts with four integers: 0 <= n <= 100 (nodes), 0 <= np <= n (power stations), 0 <= nc <= n (consumers), and 0 <= m <= n^2 (power transport lines). Follow m data triplets (u,v)z, where u and v are node identifiers (starting from 0) and 0 <= z <= 1000 is the value of l max(u,v). Follow np doublets (u)z, where u is the identifier of a power station and 0 <= z <= 10000 is the value of p max(u). The data set ends with nc doublets (u)z, where u is the identifier of a consumer and 0 <= z <= 10000 is the value of c max(u). All input numbers are integers. Except the (u,v)z triplets and the (u)z doublets, which do not contain white spaces, white spaces can occur freely in input. Input data terminate with an end of file and are correct.
Output
For each data set from the input, the program prints on the standard output the maximum amount of power that can be consumed in the corresponding network. Each result has an integral value and is printed from the beginning of a separate line.
Sample Input
2 1 1 2 (0,1)20 (1,0)10 (0)15 (1)20
7 2 3 13 (0,0)1 (0,1)2 (0,2)5 (1,0)1 (1,2)8 (2,3)1 (2,4)7
(3,5)2 (3,6)5 (4,2)7 (4,3)5 (4,5)1 (6,0)5
(0)5 (1)2 (3)2 (4)1 (5)4
Sample Output
15
6
Hint
The sample input contains two data sets. The first data set encodes a network with 2 nodes, power station 0 with pmax(0)=15 and consumer 1 with cmax(1)=20, and 2 power transport lines with lmax(0,1)=20 and lmax(1,0)=10. The maximum value of Con is 15. The second data set encodes the network from figure 1.

先梳理一下题干:
有若干发电厂,若干用户,若干中转站,发电厂有一定的发电量,用户有一定的需求,中转线路有一定承载力,构成一个图。解题呢,可以人为的加上一个起点和汇点,形成一个完整的网络图。接下来,就来说明下EK算法思想,至于具体的证明过程,在此不述。
对于整个网络图,有许多条通路可以从原点通往汇点,这些路交错乱杂。EK算法第一步就是利用BFS找到一条从原点到汇点的通路(每次找到的都是最短路径)。在这个过程中,流经每一个节点的流量取前一节点流量和线路容量的较小值(很显然)。更新完之后,当前路的流量就存在汇点的流量数据中。接下来,对经过的这条路,正向减去该值,逆向增加该值。正向减掉是为了寻找其他增广路,也就是寻找其他路径并且使得已找到的路径不影响下一次寻找;逆向增加是给程序一个不走这条路,再转回去的机会,只有这样,才能走完所有可能路径。而这个值作为一条小溪流向最终的最大流。一旦不存在一条可以从原点通向汇点的路径,就说明已经不存在新的增广路了,也就是所有可以流向最大流的小溪已经找到,那么当前值也就是最大流。
下面附上我写的代码(由于编译器问题,有些版本可能不能直接运行):

#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
using namespace std;
#define maxsize 200
//最大节点数
#define inf 0x3f3f3f

//map存图,pre记录路径,flow记录每个节点流量
int map[maxsize][maxsize],pre[maxsize],flow[maxsize];
int start,end;

//用于bfs
queue<int> qu;

//找到一条小溪
int bfs()
{

    //先清空队列
    while(!qu.empty())
    {
        qu.pop();
    }

    //前驱结点置空即其他初始化处理
    memset(pre,-1,sizeof(pre));
    for(int i=1;i<=end;i++)
    {
        flow[i]=inf;
    }
    pre[start]=0;
    flow[start]=inf;
    qu.push(start);

    //当队列不为空,继续找下一个结点
    while(!qu.empty())
    {
        //取出队首
        int cur=qu.front();
        qu.pop();

        //如果找到汇点就结束
        if(cur==end) break;

        //寻找可能路径
        for(int i=0;i<=end;i++)
        {

            //下一个节点不能是原点,防止环,不能在其他路径中,也就是第一次访问,当然,必须连通
            if(pre[i]==-1&&i!=start&&map[cur][i])
            {

                //流量取较小值
                flow[i]=min(flow[cur],map[cur][i]);
                //记录通路
                pre[i]=cur;
                //接着将该点进栈
                qu.push(i);
            }
        }
    }
    //如果没有找到通路,返回错误
    if(pre[end]==-1) return 0;
    //找到通路,就作为小溪流到下一个需要的地方啦
    else return flow[end];
}

int EK()
{
    int maxflow=0,lf,cur;
    //循环条件是还存在通路
    while((lf=bfs())&&lf)
    {

        //小溪汇进大海
        maxflow+=lf;

        //顺藤摸瓜,找回去,正向,逆向分别更改
        for(cur=end;cur!=0;cur=pre[cur])
        {
            map[pre[cur]][cur]-=lf;
            map[cur][pre[cur]]+=lf;
        }
    }
    return maxflow;
}
int main()
{
    int node,np,nc,m;
    while(scanf("%d%d%d%d",&node,&np,&nc,&m)!=EOF)
    {
        memset(map,0,sizeof(map));
        int u,v,w,z;
        while(m--)
        {
            while(getchar()!='(');
            scanf("%d,%d)%d",&u,&v,&w);
            u++;v++;
            map[u][v]=w;
        }
        while(np--)
        {
            while(getchar()!='(');
            scanf("%d)%d",&u,&z);
            u++;
            map[0][u]=z;
        }
        while(nc--)
        {
            while(getchar()!='(');
            scanf("%d)%d",&u,&z);
            u++;
            map[u][node+1]=z;
        }
        node++;
        start=0;
        end=node;
        printf("%d\n",EK());
    }
    return 0;
}

以上是使用BFS找到增广路的最大流算法。
许多朋友可能会想,既然可用BFS,当然也可以用DFS来找通路喽。确实,还有一种算法——Dinic算法,就是借助深搜找到通路。相比于EK算法,二者适用范围的主要取决于点和边的数量关系,在数量相似时,性能区别并不大。
Dinic算法简单来说就是先用BFS给图分层,再用DFS根据层次关系找路径,只不过由于是DFS,可以同时找到若干条通路,剩下的诸如更新,正逆向处理基本类似。
推荐几篇写的很好的文章帮助大家理解:
专业讲解
两种算法复杂度对比

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值