CUGB图论专场2:E - Drainage Ditches 最大流EK算法与Dinic算法

E - Drainage Ditches
Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u

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 <= 200) and M (2 <= M <= 200). 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 <= 10,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.

Sample Input

5 4
1 2 40
1 4 20
2 4 20
2 3 30
3 4 10

Sample Output

50

这题比D题还容易,最大流水题……不过是很经典的题目,没有什么转化,全裸的最大流。

1.EK算法:有点像Dinic算法,因为这个是从Dinic算法中来的,把分层去掉了而已,所以代码在DFS中还是有点像的,正向边减和反向边加。

1是源点,m是汇点,直接用EK算法0ms过……因为怕结果超出int,所以结果用了long long 型。

#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <list>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <set>
#define PI acos(-1.0)
#define mem(a,b) memset(a,b,sizeof(a))
#define sca(a) scanf("%d",&a)
#define pri(a) printf("%d\n",a)
#define MM 10002
#define MN 205
#define INF 168430090
using namespace std;
typedef long long ll;
int n,m,k,t,pre[MN],vis[MN],Map[MN][MN];
ll ans;
int bfs()
{
    mem(vis,0); queue<int>q; q.push(1); vis[1]=1;
    while(!q.empty())  //找增广路
    {
        int u=q.front(); q.pop();
        for(int v=1;v<=m;v++)
        {
            if(!vis[v]&&Map[u][v])
            {
                pre[v]=u;  //记录前向结点
                if(v==m) return 1;
                q.push(v); vis[v] = 1;
            }
        }
    }
    return 0;
}
void change()
{
    int i,sum=INF;
    for(i=m; i != 1; i = pre[i])
        sum = min(sum,Map[pre[i]][i]);
    for(i=m; i != 1; i = pre[i])
    {
        Map[pre[i]][i]-=sum; //正向边减
        Map[i][pre[i]]+=sum; //反向边加
    }
    ans+=sum;
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        mem(Map,0); ans=0;
        int u,v,w,i;
        while(n--)
        {
            scanf("%d%d%d",&u,&v,&w);
            Map[u][v]+=w;
        }
        while(bfs()) change();
        pri(ans);
    }
    return 0;
}



2.Dinic算法:

学习此算法的博客网址:http://www.cnblogs.com/acSzz/archive/2012/09/13/2683820.html

在图论书中也有说明此算法的。简单来说就是分层,从源点开始从0一层一层往后推,然后大于汇点的层(包括点与边)在DFS中就没有用到了,就相当于把这些点与边去掉了,这样当然效率更高啦!!!这就是EK算法中不相同的地方,EK是Dinic简化而来的,但是效率不如Dinic好,因为未分层,所以查询了不必要的边与结点。

#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <list>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <set>
#define PI acos(-1.0)
#define mem(a,b) memset(a,b,sizeof(a))
#define sca(a) scanf("%d",&a)
#define pri(a) printf("%d\n",a)
#define MM 10002
#define MN 205
#define INF 168430090
using namespace std;
typedef long long ll;
int n,m,d[MN],Map[MN][MN];
ll ans;
int bfs()
{
    mem(d,-1);
    queue<int>q; q.push(1); d[1]=0;
    while(!q.empty())  //找增广路
    {
        int u=q.front(); q.pop();
        for(int v=1;v<=m;v++)
        {
            if(d[v]==-1&&Map[u][v])
            {
                d[v]=d[u]+1; //分层,越往后,层数越大
                q.push(v);
            }
        }
    }
    return d[m]!=-1;
}
int dfs(int u,int Min)
{
    int sum;
    if(u==m) return Min;
    for(int v=1;v<=m;v++)
        if(Map[u][v]&&d[v]==d[u]+1&&(sum=dfs(v,min(Min,Map[u][v]))))
        {
            Map[u][v]-=sum;
            Map[v][u]+=sum;
            return sum;
        }
    return 0;
}
void Dinic()
{
    int tmp;
    while(bfs())
    {
        while(tmp=dfs(1,INF))
            ans+=tmp;
    }
    pri(ans);
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        mem(Map,0); ans=0;
        int u,v,w;
        while(n--)
        {
            scanf("%d%d%d",&u,&v,&w);
            Map[u][v]+=w;
        }
        Dinic();
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值