AC日记——草地排水 codevs 1993

1993 草地排水

 

USACO

 时间限制: 2 s
 空间限制: 256000 KB
 题目等级 : 钻石 Diamond
 
 
 
题目描述 Description

在农夫约翰的农场上,每逢下雨,Bessie最喜欢的三叶草地就积聚了一潭水。这意味着草地被水淹没了,并且小草要继续生长还要花相当长一段时间。因此,农夫约翰修建了一套排水系统来使贝茜的草地免除被大水淹没的烦恼(不用担心,雨水会流向附近的一条小溪)。作为一名一流的技师,农夫约翰已经在每条排水沟的一端安上了控制器,这样他可以控制流入排水沟的水流量。

农夫约翰知道每一条排水沟每分钟可以流过的水量,和排水系统的准确布局(起点为水潭而终点为小溪的一张网)。需要注意的是,有些时候从一处到另一处不只有一条排水沟。

根据这些信息,计算从水潭排水到小溪的最大流量。对于给出的每条排水沟,雨水只能沿着一个方向流动,注意可能会出现雨水环形流动的情形。

输入描述 Input Description

第1行: 两个用空格分开的整数N (0 <= N <= 200) 和 M (2 <= M <= 200)。N是农夫John已经挖好的排水沟的数量,M是排水沟交叉点的数量。交点1是水潭,交点M是小溪。

第二行到第N+1行: 每行有三个整数,Si, Ei, 和 Ci。Si 和 Ei (1 <= Si, Ei <= M) 指明排水沟两端的交点,雨水从Si 流向Ei。Ci (0 <= Ci <= 10,000,000)是这条排水沟的最大容量。

输出描述 Output Description

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

样例输入 Sample Input
5 4
1 2 40
1 4 20
2 4 20
2 3 30
3 4 10
样例输出 Sample Output

50

数据范围及提示 Data Size & Hint
 
思路:
  最大流;
 
 
来,上代码:
#include <cstdio>
#include <iostream>

using namespace std;

struct EdgeType {
    int to,next,flow;
};
struct EdgeType edge[600];

int if_z,n,m,cnt=1,que[2000],ans,deep[600],head[600];

char Cget;

inline void in(int &now)
{
    now=0,if_z=1,Cget=getchar();
    while(Cget>'9'||Cget<'0')
    {
        if(Cget=='-') if_z=-1;
        Cget=getchar();
    }
    while(Cget>='0'&&Cget<='9')
    {
        now=now*10+Cget-'0';
        Cget=getchar();
    }
    now*=if_z;
}

int flowing(int now,int flow)
{
    if(flow==0||now==m) return flow;
    int oldflow=0;
    for(int i=head[now];i;i=edge[i].next)
    {
        if(edge[i].flow<=0||deep[edge[i].to]!=deep[now]+1) continue;
        int pos=flowing(edge[i].to,min(flow,edge[i].flow));
        flow-=pos;
        oldflow+=pos;
        edge[i].flow-=pos;
        edge[i^1].flow+=pos;
        if(flow==0) return oldflow;
    }
    return oldflow;
}

int main()
{
    in(n),in(m);int u,v,w;
    for(int i=1;i<=n;i++)
    {
        in(u),in(v),in(w);
        edge[++cnt].to=v,edge[cnt].next=head[u],edge[cnt].flow=w,head[u]=cnt;
        edge[++cnt].to=u,edge[cnt].next=head[v],edge[cnt].flow=0,head[v]=cnt;
    }
    while(1)
    {
        for(int i=1;i<=m;i++) deep[i]=-1;
        int h=0,tail=0;que[tail++]=1,deep[1]=0;
        bool if_break=false;
        while(h<tail)
        {
            for(int i=head[que[h]];i;i=edge[i].next)
            {
                if(edge[i].flow&&deep[edge[i].to]<0)
                {
                    deep[edge[i].to]=deep[que[h]]+1;
                    if(edge[i].to==m)
                    {
                        if_break=true;
                        break;
                    }
                    que[tail++]=edge[i].to;
                }
            }
            if(if_break) break;
            h++;
        }
        if(deep[m]<0) break;
        ans+=flowing(1,0x7ffffff);
    }
    cout<<ans;
    return 0;
}

 

posted @ 2017-03-04 10:51  IIIIIIIIIU  阅读(184)  评论(0编辑  收藏  举报