• POJ 1273 Drainage Ditches【最大流EK算法模板题】


    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

    题目大意:很容易理解,求出开始点到终点的最大流。
    思路: 1》:从源点出发,广搜一条最短的可行路,每次到达一点用数组pre[]记下是由哪条边来的;
        2》:到达汇点时,按照pre[]记录的从可行路径找出容量最少的容量;
        3》:重复1》, 2》, 知道无法到达汇点;
    概念: 1》:点连通:N个点的图中, 去点k-1个顶点,子图仍然连通, 去点k个顶点后不连通, 则是k连通图,连通度为k;
        2》:独立轨:这能经过一次的边,即独立轨的个数等于连通度;
        3》:最小点割集:从源点到汇点至少删去多少个点使得图不连通, 这个问题就是点连通度,又叫最小点割集;
    等价关系: 1》:求边的连通度给每条边赋值为1, 任意选择源点, 枚举汇点, 依次求最小割, 取其中的最小值;
          2》:求最小割要用到最大流最小割定理,即一个图的最小割等于其源点与汇点之间的最大流;
    代码如下:
    View Code
    #include<iostream>
    #include<string.h> 
    #include<queue>
    #include<stdio.h> 
    using namespace std;
    #define N 201
    int MAX=0x7fffffff;
    int map[N][N], flow[N], pre[N], n,m;
    queue<int>Q;
    int BFS(int st,int end)
    {
        int i,j;
        while(!Q.empty())  
            Q.pop();
        for(i=1;i<m+1;++i)
            pre[i]=-1;
        pre[st]=0;
        flow[st]=MAX;
        Q.push(st);
        while(!Q.empty())
        {
            int cux=Q.front();
            Q.pop();
            if(cux==end)       
                break;
            for(i=1; i<m+1; ++i)
            {
                if(i!=st && map[cux][i]>0 && pre[i]==-1)
                {
                     pre[i]=cux;
                     flow[i]=min(map[cux][i],flow[cux]); 
                     Q.push(i);               
                }
            }
        } 
        if(pre[end]==-1)  
            return -1;
        else
            return flow[end];
    } 
    void maxFlow(int st, int end)
    {
        int in=0;
        int sum=0;
        while((in=BFS(st, end))!=-1)
        {
             int k=end;          
             while(k!=st)
             {
                  int last=pre[k];
                  map[last][k]-=in;
                  map[k][last]+=in; 
                  k=last;
             }
             sum+=in;
        }    
        printf("%d\n", sum); 
    }
    int main()
    {
        int i,j;
        int st, end, c;
        while(scanf("%d%d", &n, &m)!=EOF) 
        { 
            memset(map, 0, sizeof(map)); 
            memset(flow,0,sizeof(flow));
            for(i=0;i<n;++i)
            {
                scanf("%d%d%d", &st, &end, &c);
                map[st][end]+=c;     
            }
            maxFlow(1, m);
        }
        return 0;
    }
  • 相关阅读:
    XP系统下快速切换ip的bat脚本配置
    Spring学习札记
    hibernate防止sql注入
    重载,继承,重写和多态的区别:
    Oracle Sql基础
    Android开发——利用Cursor+CursorAdapter实现界面实时更新
    Android开发——09Google I/O之让Android UI性能更高效(1)
    Android开发——MediaProvider源码分析(2)
    Android开发——Android搜索框架(二)
    [转]activity的启动方式(launch mode)
  • 原文地址:https://www.cnblogs.com/Hilda/p/2653259.html
Copyright © 2020-2023  润新知