NYOJ323 Drainage Ditches 最大流

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. 

题意:下雨的时候会形成池塘淹没Farmer John的三叶草。于是他准备开个沟渠排水。1是起点,m是汇点,他希望求出最大的水流过沟渠的速率。

题解:根据题意是求最大网络流。没有什么陷阱,用EK或者DINIC都可解。

EK算法:

#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<queue>
#define INF 99999
using namespace std;
int a[202][202];
int b[202][202];
int n,m,ans;
int EK(int src,int tar)
{
    int ret=0;
    int pre[202];
    int mm[202],vit[202];
    memset(mm,0x3f3f,sizeof(vit));
    while(1)
    {
        memset(pre,-1,sizeof(pre));
        mm[src]=INF;
        memset(vit,0,sizeof(vit));
        queue<int>q;
        q.push(src);
        while(!q.empty())
        {
            int t=q.front();
            q.pop();
            if(vit[t])
                continue;
            vit[t]=1;
            for(int i=1;i<=m;i++)
            {
                if(!vit[i]&&a[t][i]-b[t][i]>0)
                {
                    pre[i]=t;
                    mm[i]=min(a[t][i]-b[t][i],mm[t]);
                    q.push(i);
                }
            }
        }
            if(pre[tar]!=-1)
            {
                for(int i=tar;pre[i]!=-1;i=pre[i])
                {
                    b[pre[i]][i]+=mm[tar];
                    b[i][pre[i]]-=mm[tar];
                }
            }


        if(pre[tar]==-1)
            break;
        else
            ret+=mm[tar];
    }
    return ret;
}
int main()
{
    while(cin>>n>>m)
    {
        ans=0;
        memset(b,0,sizeof(b));
        memset(a,0,sizeof(a));
        for(int i=1;i<=n;i++)
        {
            int x,y,z;
            cin>>x>>y>>z;
            a[x][y]+=z;
        }
        ans=EK(1,m);
        cout<<ans<<endl;
    }
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值