• (网络流 模板 Edmonds-Karp)Drainage Ditches --POJ --1273


    链接:

    http://poj.org/problem?id=1273

    Drainage Ditches
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 63763   Accepted: 24613

    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

    代码:

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <queue>
    #include <vector>
    using namespace std;
    #define N 210
    #define INF 0x3f3f3f3f
    
    int n, m, G[N][N], pre[N];
    
    bool BFS(int Start, int End)
    {
        int p;
    
        queue<int>Q;
        Q.push(Start);
    
        memset(pre, 0, sizeof(pre));
    
        while(Q.size())
        {
            p = Q.front(), Q.pop();
    
            if(p==End) return true;
    
            for(int i=1; i<=End; i++)
            {
                if(!pre[i] && G[p][i])
                {
                    pre[i] = p;
                    Q.push(i);
                }
            }
        }
        return false;
    }
    
    int EM(int Start, int End)
    {
        int Max=0;
        while(BFS(Start, End)==true)
        {
            int Min=INF;
    
            for(int i=End; i!=Start; i=pre[i])
                Min = min(Min, G[pre[i]][i]);
             for(int i=End; i!=Start; i=pre[i])
             {
                 G[pre[i]][i] -= Min;
                 G[i][pre[i]] += Min;
             }
    
             Max += Min;
        }
        return Max;
    }
    
    int main()
    {
        while(scanf("%d%d", &n, &m)!=EOF)
        {
            int i, u, v, p;
    
            memset(G, 0, sizeof(G));
            for(i=0; i<n; i++)
            {
                scanf("%d%d%d", &u, &v, &p);
                G[u][v] += p;
            }
    
            printf("%d
    ", EM(1, m));
        }
        return 0;
    }
    勿忘初心
  • 相关阅读:
    在注册表中添加windows鼠标右键菜单
    关于MVC中无法将类型为“System.Int32”的对象强制转换为类型“System.String”的问题。
    dns
    ntp
    研究比对搞定博客 canvas-nest.js
    linux 添加ssh和开启ssh服务apt管理的ubuntu
    xshll 连接ubuntu出现 ssh服务器拒绝了密码
    yum和rpm
    服务器无法远程连接原因分析
    关于服务器卡顿的几个原因
  • 原文地址:https://www.cnblogs.com/YY56/p/4723433.html
Copyright © 2020-2023  润新知