• POJ 1273 Drainage Ditches(网络流,最大流)


    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

    Source

    网络流咩,看了好久了,今天一并刷了
    E-K算法重复寻找源点s到汇点t之间的增广路径,若有。找出增广路径上每一段[容量-流量]的最小值d,若无,则结束。在寻找增广路径时,能够用BFS来找。而且更新残留网络的值(涉及到反向边)。
    而找到d后。则使最大流(maxflow)值加上d,更新为当前的最大流值
    <pre name="code" class="cpp">#include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<queue>
    using namespace std;
    const int INF=0x7ffffff;
    const int maxn=220;
    int N,M;
    int r[maxn][maxn];
    int pre[maxn];
    bool visit[maxn];
    bool bfs(int s,int t)
    {
        queue<int>q;
        memset(pre,-1,sizeof(pre));
        memset(visit,false,sizeof(visit));
    
        pre[s]=s;
        visit[s]=true;
        q.push(s);
    
        int p;
        while(!q.empty())
        {
            p=q.front();
            q.pop();
            for(int i=1;i<=M;i++)
            {
                if(r[p][i]>0&&!visit[i])
                {
                    pre[i]=p;
                    visit[i]=true;
                    if(i==t)
                        return true;
                    q.push(i);
                }
            }
        }
        return false;
    }
    int solve(int s,int t)
    {
       int d,maxflow=0;
       while(bfs(s,t))
       {
           d=INF;
           for(int i=t;i!=s;i=pre[i])
              d=min(d,r[pre[i]][i]);
           for(int i=t;i!=s;i=pre[i])
           {
               r[pre[i]][i]-=d;
               r[i][pre[i]]+=d;
           }
           maxflow+=d;
       }
       return maxflow;
    }
    int main()
    {
        while(cin>>N>>M)
        {
            memset(r,0,sizeof(r));
            int s,e,c;
            for(int i=0;i<N;i++)
            {
                cin>>s>>e>>c;
                r[s][e]+=c;
            }
            cout<<solve(1,M)<<endl;
        }
        return 0;
    }


    
    
    
    Dinic算法:依据残留网络计算层次图,在层次图中进行DFS增广。

    详见:Comzyh的博客(凝视具体,解说易懂)

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<limits.h>
    typedef long long LL;
    using namespace std;
    int mp[250][250];
    int dis[250];
    int q[2000],h,r;
    int n,m,ans;
    int bfs()
    {
        int i,j;
        memset(dis,-1,sizeof(dis));
        dis[1]=0;
        h=0;r=1;
        q[1]=1;
        while(h<r)
        {
            j=q[++h];
            for(i=1;i<=n;i++)
            {
                if(dis[i]<0&&mp[j][i]>0)
                {
                    dis[i]=dis[j]+1;
                    q[++r]=i;
                }
            }
        }
        if(dis[n]>0)   return 1;
        else   return 0;
    }
    int find(int x,int low)
    {
        int a;
        if(x==n)  return low;
        for(int i=1;i<=n;i++)
        {
            if(mp[x][i]>0&&dis[i]==dis[x]+1&&(a=find(i,min(low,mp[x][i]))))
            {
                mp[x][i]-=a;
                mp[i][x]+=a;
                return a;
            }
        }
        return 0;
    }
    int main()
    {
        int flow,tans;
        int s,t;
        while(~scanf("%d%d",&m,&n))
        {
            memset(mp,0,sizeof(mp));
            for(int i=1;i<=m;i++)
            {
                scanf("%d%d%d",&s,&t,&flow);
                mp[s][t]+=flow;
            }
            ans=0;
            while(bfs())
            {
                if(tans=find(1,0x7ffffff))
                   ans+=tans;
            }
            printf("%d
    ",ans);
        }
        return 0;
    }
    



  • 相关阅读:
    网站、数据库的衍变之路(三)
    脚本嵌入式抓取引擎
    db4objects 7.4应用笔记
    网站、数据库的衍变之路(二)
    IIS连接数实验——Web开发必读
    攻破WebService,WCF的改进
    C#使用BerkeleyDB操作简介
    面试两个星期来的一点体会
    Lucene.Net 2.3.1开发介绍 —— 四、搜索(三)
    轻量级爬虫+全文检索解决方案项目——NukeLite(20081114 更新r24版 引入新线程机制)
  • 原文地址:https://www.cnblogs.com/zfyouxi/p/5134441.html
Copyright © 2020-2023  润新知