USACO Drainage Ditches

USACO Drainage Ditches

洛谷传送门

JDOJ传送门

Description

在农夫约翰的农场上,每逢下雨,贝茜最喜欢的三叶草地就积聚了一潭水.这意味着草地被水淹没了,并且小草要继续生长还要花相当长一段时间.因此,农夫约翰修建了一套排水系统来使贝茜的草地免除被大水淹没的烦恼(不用担心,雨水会流向附近的一条小溪).作为一名一流的技师,农夫约翰
已经在每条排水沟的一端安上了控制器,这样他可以控制流入排水沟的水流量.
农夫约翰知道每一条排水沟每分钟可以流过的水量,和排水系统的准确布局(起点为水潭而终点为小溪的一张网).需要注意的是,有些时候从一处到另一处不只有一条排水沟.
根据这些信息,计算从水潭排水到小溪的最大流量.对于给出的每条排水沟,雨水只能沿着一个方向流动,注意可能会出现雨水环形流动的情形.

Input

第1 行: 两个用空格分开的整数N (0 <= N <= 200) 和 M (2 <= M <= 200).N 是农夫约翰已经挖好的排水沟的数量,M 是排水沟交叉点的数量.交点1 是水潭,交点M 是小溪.
第二行到第N+1 行: 每行有三个整数,Si, Ei, 和 Ci.Si 和 Ei (1 <= Si, Ei <= M) 指明排水沟两端的交点,雨水从Si 流向Ei.Ci (0 <= Ci <= 10,000,000)是这条排水沟的最大容量.

Output

输出一个整数,即排水的最大流量.

Sample Input

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

Sample Output

50


题解:

网络流裸题。

详见:浅谈网络最大流

代码:

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#define int long long
using namespace std;
const int maxn=210;
const int maxm=5010;
const int INF=1e18;
int n,m,s,t;
int tot=1,to[maxm<<1],nxt[maxm<<1],head[maxn],val[maxm<<1];
int flow[maxn],pre[maxn];
bool v[maxn];
void add(int x,int y,int z)
{
    to[++tot]=y;
    nxt[tot]=head[x];
    val[tot]=z;
    head[x]=tot;
}
bool bfs()
{
    memset(pre,-1,sizeof(pre));
    queue<int> q;
    q.push(s);
    flow[s]=INF;
    while(!q.empty())
    {
        int x=q.front();
        q.pop();
        if(x==t)
            break;
        for(int i=head[x];i;i=nxt[i])
        {
            int y=to[i];
            if(val[i]==0||pre[y]>0)
                continue;
            pre[y]=i;
            flow[y]=min(flow[x],val[i]);
            q.push(y);
        }
    }
    return pre[t]!=-1;
}
signed main()
{
    scanf("%lld%lld",&m,&n);
    s=1,t=n;
    for(int i=1;i<=m;i++)
    {
        int x,y,z;
        scanf("%lld%lld%lld",&x,&y,&z);
        add(x,y,z);
        add(y,x,0);
    }
    int ans=0;
    while(bfs())
    {
        ans+=flow[t];
        for(int i=t;i!=s;i=to[pre[i]^1])
        {
            val[pre[i]]-=flow[t];
            val[pre[i]^1]+=flow[t];
        }
    }
    printf("%lld\n",ans);
    return 0;
}
posted @ 2020-11-28 16:55  Seaway-Fu  阅读(92)  评论(0编辑  收藏  举报