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;
    }
    勿忘初心
  • 相关阅读:
    【C#图解教程学习笔记】第13章 委托
    TreeView树,全选,反选,平级选操作
    C# 字符串和字节数组转换
    WebServices 使用Session
    Win8.1下Flash Builder 提示: 找不到所需的Adobe Flash Player调试器版本,解决办法
    C# winform DataGridView 绑定数据的的几种方法
    .NET Core GB2312
    [WPF 学习] 7.2 模板打印
    [WPF 学习] 7.1 多页打印
    [WPF 学习] 7 打印相关的东东
  • 原文地址:https://www.cnblogs.com/YY56/p/4723433.html
Copyright © 2011-2022 走看看