Drainage Ditches(网络流入门)

  一道增广路网络流的模板题,但是毕竟是初学网络流,还是在一些细节上WA、T了几发。

Description 

Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover patch. This means that the clover is covered by water for awhile and takes quite a long time to regrow. Thus, Farmer John has built a set of drainage ditches so that Bessie's clover patch is never covered in water. Instead, the water is drained to a nearby stream. Being an ace engineer, Farmer John has also installed regulators at the beginning of each ditch, so he can control at what rate water flows into that ditch.
Farmer John knows not only how many gallons of water each ditch can transport per minute but also the exact layout of the ditches, which feed out of the pond and into each other and stream in a potentially complex network.
Given all this information, determine the maximum rate at which water can be transported out of the pond and into the stream. For any given ditch, water flows in only one direction, but there might be a way that water can flow in a circle.

Input 

The input includes several cases. For each case, the first line contains two space-separated integers, N (0 <= N <= 500) and M (2 <= M <= 500). N is the number of ditches that Farmer John has dug. M is the number of intersections points for those ditches. Intersection 1 is the pond. Intersection point M is the stream. Each of the following N lines contains three integers, Si, Ei, and Ci. Si and Ei (1 <= Si, Ei <= M) designate the intersections between which this ditch flows. Water will flow through this ditch from Si to Ei. Ci (0 <= Ci <= 1,000,000) is the maximum rate at which water will flow through the ditch.

Output 

For each case, output a single integer, the maximum rate at which water may emptied from the pond.

5 4
1 2 40
1 4 20
2 4 20
2 3 30
3 4 10
2 2
1 2 100
1 2 200
50
300

 

思路

  我通过BFS找增广路的方式来不断更新残余网络的流量,每次如果能从st点遍历到ed点,就说明还可以更新,直到不能更新,就是说明已经是最大流了。每次还要通过前驱节点更新正向边(减)和逆向边(加)的残余网络的流量值。

 

完整代码:

#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
using namespace std;
typedef long long ll;
const ll INF=(ll)1<<58;
const int maxN=505;
int N,M;
ll r[maxN][maxN];      //残余网络的流量
ll flow[maxN];         //到目前点的流量
int pre[maxN];          //前驱
queue<int> Q;
ll Bfs(int st, int ed)
{
    fill(flow, flow+maxN, INF);
    while(!Q.empty()) Q.pop();
    for(int i=1; i<=M; i++) pre[i]=-1;
    pre[st]=0;
    flow[st]=INF;
    Q.push(st);
    while(!Q.empty())
    {
        int u=Q.front();
        Q.pop();
        if(u==ed) break;
        for(int i=1; i<=M; i++)
        {
            if(i!=st && r[u][i]>0 && pre[i]==-1)
            {
                pre[i]=u;       //前驱
                flow[i]=min(r[u][i], flow[u]);      //增量
                Q.push(i);
            }
        }
    }
    return pre[ed]==-1?(-1):flow[ed];       //有没有增广路,有的话就加上增广路流量
}
ll maxFlow(int st, int ed)
{
    ll incr=0;
    ll sumflow=0;
    while((incr=Bfs(st, ed))!=-1)
    {
        int k=ed;       //通过前驱寻找路径
        while(k!=st)
        {
            int last=pre[k];
            r[last][k]-=incr;       //正向边
            r[k][last]+=incr;       //逆向边
            k=last;
        }
        sumflow+=incr;
    }
    return sumflow;
}
int main()
{
    while(scanf("%d%d",&N,&M)!=EOF)
    {
        memset(r, 0, sizeof(r));
        //memset(flow, 0, sizeof(flow));      //一定是初始化为0,不然二次操作会出问题(不然就是放在BFS中Fill(INF))
        //fill(flow, flow+maxN, INF);
        for(int i=1; i<=N; i++)
        {
            int e1,e2,e3;
            scanf("%d%d%d",&e1,&e2,&e3);
            if(e1==e2) continue;
            r[e1][e2]+=e3;
        }
        printf("%lld\n",maxFlow(1, M));
    }
    return 0;
}

 

再补上网络流的Dinic算法:

#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define lowbit(x) ( x&(-x) )
#define INF 1e9+7
#define pi 3.141592653589793
#define e 2.718281828459045
using namespace std;
typedef long long ll;
const int maxN=205;
int N,M;
int flow[maxN][maxN], dis[maxN];        //dis指的是到源点的层数
bool bfs()
{
    memset(dis, -1, sizeof(dis));
    dis[1]=0;
    queue<int> Q;
    Q.push(1);      //源点
    while(!Q.empty())
    {
        int u=Q.front();
        Q.pop();
        for(int i=1; i<=M; i++)
        {
            if(flow[u][i]>0 && dis[i]==-1)
            {
                dis[i]=dis[u]+1;
                Q.push(i);
            }
        }
    }
    return dis[M]!=-1;
}
int dfs(int x, int maxflow_X)       //点、路径上的最小流量
{
    if(x==M) return maxflow_X;
    int k;
    for(int i=1; i<=M; i++)
    {
        if(flow[x][i]>0 && dis[i]==dis[x]+1 && (k=dfs(i, min(maxflow_X, flow[x][i]))) )
        {
            flow[x][i]-=k;
            flow[i][x]+=k;
            return k;
        }
    }
    return 0;
}
int main()
{
    while(scanf("%d%d",&N,&M)!=EOF)
    {
        memset(flow, 0, sizeof(flow));
        int e1,e2,e3;
        for(int i=0; i<N; i++)
        {
            scanf("%d%d%d",&e1,&e2,&e3);
            flow[e1][e2]+=e3;
        }
        int ans=0, res;
        while(bfs())
        {
            while( (res=dfs(1, INF)) ) ans+=res;
        }
        printf("%d\n", ans);
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Wuliwuliii

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值