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;
    }
    勿忘初心
  • 相关阅读:
    计算机程序的构造和解释 1.21 寻找素数因子
    迭代法对数计算B的N次方 SICP 计算机程序的构造和解释 1.16
    斐波那契算法的对数解法 计算机程序的构造和解释 习题1.19
    MS SQL SERVER数据库简单回顾
    SICP~计算机程序的构造和解释~ 1.12 c++实现
    论5级流水32bit risc cpu设计
    mdk编译器起到的boot作用详解
    处理器boot的简单概念及误区
    搬砖两年感受
    操作系统方面的两本好书
  • 原文地址:https://www.cnblogs.com/YY56/p/4723433.html
Copyright © 2011-2022 走看看