zoukankan      html  css  js  c++  java
  • (网络流 模板 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;
    }
    勿忘初心
  • 相关阅读:
    Android Studio 2.3.1导出jar文件不能生成release解决办法
    AndroidStudio 3.0 生成jar包的方法
    Android Studio如何打jar包
    Android Studio 如何打JAR包(修订版)
    6款程序员必备的开源中文处理工具
    Qt5.8 下链接 Mysql 错误以及解决方法(无论 Mysql 是什么版本的,64 位 Qt 要用 64 位的 Mysql 驱动,32 位的 Qt 要用 32 位的Mysql 驱动)
    Go 语言如果按这样改进,能不能火过 Java?
    基于 CSP 的设计思想和 OOP 设计思想的异同
    DELPHI下多线程编程的几个思维误区(QDAC)
    如何使用表单
  • 原文地址:https://www.cnblogs.com/YY56/p/4723433.html
Copyright © 2011-2022 走看看