• 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;
    }
     
  • 相关阅读:
    浏览器 页面报错
    TP单字母函数
    TP系统常量、项目后台的搭建、模板中常量字符串替换
    ThinkPHP3.2的简单知识
    面向对象基本概念,关键字,类的组成,静态,三大特性,创建类和对象,设计模式
    layui(弹出层)
    头像文件上传 方法一:from表单 方法二:ajax
    上传文件
    流程增加,发起事件,流程显示,处理。
    分页,条件查找后再分页
  • 原文地址:https://www.cnblogs.com/Noevon/p/6183230.html
Copyright © 2020-2023  润新知