• POJ 1273 Drainage Ditches


    Drainage Ditches
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 54766   Accepted: 20880

    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


    哈哈,第一道网络流!!!

    模板一次敲对。基础理解差点儿相同了吧。

    在代码中解释一下吧!

    本来就仅仅有Edmonds-karp算法,如今补上Dinic算法。


    AC代码例如以下:


    Edmonds-karp算法例如以下:


    #include<iostream>
    #include<cstring>
    #include<queue>
    #define inf 100000000
    using namespace std;
    
    int n,m;
    int map[205][205],p[205];//map存网络图。p[]记录下标的流的来源。
    int f[205];//用于增广路的最大增流量统计
    
    int bfs(int s,int e)
    {
        int i,j;
        queue <int > q;
        for(i=1;i<=n;i++)
        {p[i]=-1;}
        f[1]=inf;
        int a,b;
        q.push(s);
        while(!q.empty())
        {
            b=q.front();
            q.pop();
            if(b==e)//找到终点
                break;
            for(i=2;i<=n;i++)
            {
                if(map[b][i]>0&&p[i]==-1)
                {
                    p[i]=b;
                    f[i]=min(f[b],map[b][i]);
                    q.push(i);
                }
            }
        }
        if(p[e]==-1)//没有增广路了
            return -1;
        else return f[e];
    }
    
    int maxflow(int s,int e)
    {
        int i,j;
        int in;
        int ans=0;
        in=bfs(s,e);//用BFS找增广路
        while(in!=-1)
        {
            int k=e;
            while(k!=s)
            {
                map[p[k]][k]-=in;//增减时满足正向和反向的转化关系
                map[k][p[k]]+=in;
                k=p[k];
            }
            ans+=in;//统计增流
            in=bfs(s,e);
        }
        return ans;
    }
    
    int main()
    {
        int i,j;
        int a,b,c;
        while(cin>>m>>n)
        {
            memset(map , 0 , sizeof map );
            for(i=1;i<=m;i++)
            {
                cin>>a>>b>>c;
                map[a][b]+=c;//可能存在同等性质的流道,累加即可了
            }
            cout<<maxflow(1,n)<<endl;
        }
        return 0;
    }
    


    Dinic算法例如以下:



    #include<iostream>
    #include<cstring>
    #include<queue>
    #define inf 100000000
    #define min(a,b) (a<b?

    a:b) using namespace std; int n,m; int map[205][205],cs[205]; int bfs () { int i,a,b; queue <int > q; memset(cs , -1 , sizeof cs); cs[1]=0; q.push(1); while(!q.empty()) { a=q.front (); q.pop(); for(i=1;i<=n;i++) { if(cs[i]<0&&map[a][i]>0) { cs[i]=cs[a]+1; q.push(i); } } } if(cs[n]==-1) return 0; else return 1; } int dfs(int x,int mf) { int i,j; int a; if(x==n) return mf; for(i=1;i<=n;i++) { if(cs[i]==cs[x]+1&&map[x][i]>0&&(a=dfs(i,min(mf,map[x][i])))) { map[x][i]-=a; map[i][x]+=a; return a; } } return 0; } int main() { int i,j; int a,b,c; int sum; while(cin>>m>>n) { memset(map,0,sizeof map); for(i=1;i<=m;i++) { cin>>a>>b>>c; map[a][b]+=c; } int ans=0; while(bfs())//不断构建层次图 { while(sum=dfs(1,inf))//搜索路径 ans+=sum; } cout<<ans<<endl; } return 0; }





  • 相关阅读:
    crawler_URL编码原理详解
    linux_常用压缩,解压缩命令
    myql_链接丢失异常_mybaits _等框架_报错_The last packet successfully
    linux_shell_类似sql的orderby 取最大值
    php_cawler_html嵌套标签清洗
    vim_编码配置文件_utf8乱码解决
    python_random随机
    linux_shell_轮询触发启动脚本
    crawler_http关闭连接
    linux_mac_配置itrem2 rz sz_bug处理
  • 原文地址:https://www.cnblogs.com/brucemengbm/p/6763846.html
Copyright © 2020-2023  润新知