zoukankan      html  css  js  c++  java
  • nefu 473 Drainage DitchesHal Burch 最大流

    Drainage DitchesHal Burch

    Time Limit 1000ms

    Memory Limit 65536K

    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. Note however, that there can be more than one ditch between two intersections.
    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

    Input file contains multiple test cases. 
    In a test case:
    Line 1:	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.
    Line 2..N+1:	Each of 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,One line with 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

    最大流模板题目

    #include <iostream>
    
    using namespace std;
    
    const int OO=1e9;//无穷大
    const int maxm=111111;//边的最大数量,为原图的两倍
    const int maxn=999;//点的最大数量
    
    int node,src,dest,edge;//node节点数,src源点,dest汇点,edge边数
    int head[maxn],work[maxn],dis[maxn],q[maxn];//head链表头,work临时表头,dis计算距离
    
    struct edgenode{
        int to;//边的指向
        int flow;//边的容量
        int next;//链表的下一条边
    }edges[maxm];
    
    //初始化链表及图的信息
    void prepare(int _node,int _src,int _dest)
    {
        node=_node;
        src=_src;
        dest=_dest;
        for (int i=0;i<node;i++) head[i]=-1;
        edge=0;
    }
    
    //添加一条从u到v容量为c的边
    void addedge(int u,int v,int c)
    {
        edges[edge].flow=c;edges[edge].to=v;edges[edge].next=head[u];head[u]=edge++;
        edges[edge].flow=0;edges[edge].to=u;edges[edge].next=head[v];head[v]=edge++;
    }
    
    //广搜计算出每个点与源点的最短距离,如果不能到达汇点说明算法结束
    bool Dinic_bfs()
    {
        int u,v,r=0;
        for (int i=0;i<node;i++) dis[i]=-1;
        q[r++]=src;
        dis[src]=0;
        for (int l=0;l<r;l++)
        {
            u=q[l];
            for (int i=head[u];i!=-1;i=edges[i].next)
            {
                v=edges[i].to;
                if (edges[i].flow&&dis[v]<0)
                {//这条边必须要有剩余流量
                    q[r++]=v;
                    dis[v]=dis[u]+1;
                    if (v==dest) return true;
                }
            }
        }
        return false;
    }
    
    //寻找可行流的增广路算法,按节点的距离来找,加快速度
    int Dinic_dfs(int u,int exp)
    {
        int v,tmp;
        if (u==dest) return exp;
        //work是临时链表头,这里用 i引用它,这样寻找过的边不再寻找
        for (int &i=work[u];i!=-1;i=edges[i].next)
        {
            v=edges[i].to;
            if (edges[i].flow&&dis[v]==dis[u]+1&&(tmp=Dinic_dfs(v,min(exp,edges[i].flow)))>0)
            {
                edges[i].flow-=tmp;
                edges[i^1].flow+=tmp;
                //正反向边容量改变
                return tmp;
            }
        }
        return 0;
    }
    
    //求最大流直到没有可行流
    int Dinic_flow()
    {
        int ret=0,tmp;
        while (Dinic_bfs())
        {
            for (int i=0;i<node;i++) work[i]=head[i];
            while ( tmp=Dinic_dfs(src,OO) ) ret+=tmp;
        }
        return ret;
    }
    
    int main()
    {
        int n,m,u,c,v;
        while (cin>>n>>m)
        {
            prepare(m+1,1,m);
            while (n--)
            {
                cin>>u>>v>>c;
                addedge(u,v,c);
            }
            cout<<Dinic_flow()<<endl;
        }
        return 0;
    }
    





  • 相关阅读:
    网站架构探索(3)负载均衡的方式
    架构师之路(6)OOD的开闭原则
    也谈IT人员流失问题 王泽宾
    技术体系的选择之Java篇
    网站架构探索(2)CDN基本常识
    设计模式之单例模式
    网站架构探索(1)序言 王泽宾
    架构师之路(39)IoC框架
    发展之道:简单与专注
    修me30打印机
  • 原文地址:https://www.cnblogs.com/cyendra/p/3038449.html
Copyright © 2011-2022 走看看