zoukankan      html  css  js  c++  java
  • HDU1532 Drainage Ditches 网络流EK算法

    Drainage Ditches

    Problem 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
     
    题意:最大流EK算法,一直找增广路径(BFS),假如有,记录增广路的最小值k,ans +=k ,并更新网络的值(要用反向边)。复杂度O(V*E^2)
    题解:最大流模版
    #include<bits/stdc++.h>
    #define N 205
    #define mes(x) memset(x, 0, sizeof(x));
    #define ll __int64
    const long long mod = 1e9+7;
    const int MAX = 0x7ffffff;
    using namespace std;
    int plat[N][N], n, dir[N], pre[N];
    int bfs(int s,int e){
        int t, i;
        queue<int>q;
        memset(dir,0,sizeof(dir));
        memset(pre,-1,sizeof(pre));
        pre[s] = s;
        dir[s] = 1;
        q.push(s);
        while(!q.empty()){
            t = q.front();
            q.pop();
            for(i=1;i<=n;i++){
                if(plat[t][i] > 0&&!dir[i]){//全为正且未走过即扩展 
                    pre[i] = t;
                    dir[i] = 1;
                    if(i == e) return 1;//扩展到终点为止 
                    q.push(i);
                }
            }
        }
        return 0;//找不到    
    }
    int EK(int s,int e){
        int ans=0,minn,i;
        while(bfs(s,e)){
            minn = MAX; 
            for(i=e;i!=s;i=pre[i])
                if(minn > plat[pre[i]][i])
                    minn = plat[pre[i]][i];
            for(i=e;i!=s;i=pre[i]){
                plat[pre[i]][i] -= minn;
                plat[i][pre[i]] += minn;
            }
            ans += minn;
            
        }
        return ans;
    }
    int main()
    {
        int m, a, b, c;
        while(~scanf("%d%d", &m, &n)){
            mes(plat);
            while(m--){
                scanf("%d%d%d", &a, &b, &c);
                plat[a][b] += c;
            }
            printf("%d
    ", EK(1,n));;
        }
        return 0;
    }
     
  • 相关阅读:
    android代码控制seekbar的样式
    在Android中显示GIF动画
    一个带动画效果的颜色选择对话框控件AnimatedColorPickerDialog
    史上最强Android 开启照相或者是从本地相册选中一张图片以后先裁剪在保存并显示的讲解附源码
    Linux System Programming note 8 ——File and Directory Management
    Spring它不支持依赖注入static静态变量
    举例说,在命令模式(Command Pattern)
    log4j 日志大小限制 分成30一个 不按日期分日志 按大小分成 按生产日期
    JavaScript获取路径
    awk与sed:关于多行的样本
  • 原文地址:https://www.cnblogs.com/Noevon/p/6183230.html
Copyright © 2011-2022 走看看