zoukankan      html  css  js  c++  java
  • POJ 1273 Drainage Ditches(最大流 SAP 模板)

                                                                                                          

    Drainage Ditches

                       

    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

    模板题 参考了大佬的模板 http://blog.csdn.net/lianai911/article/details/44962653
    代码如下:
    #include<iostream>
    #include<algorithm>
    #include<cstdio>
    #include<cstring>
    #include<queue>
    using namespace std;
    const int MAXN = 220;
    const int MAXM = MAXN*MAXN;
    const int INF = 0xffffff0;
    
    struct EdgeNode
    {
        int to;
        int w;
        int next;
    }Edges[MAXM];
    int Head[MAXN],tol;
    void init()
    {
         tol = 0;
         memset(Head,-1,sizeof(Head));
    }
    void add_edge(int u,int v,int w)
    {
        Edges[tol].to = v;
        Edges[tol].w = w;
        Edges[tol].next = Head[u];
        Head[u] = tol++;
        Edges[tol].to = u;
        Edges[tol].w = 0;
        Edges[tol].next = Head[v];
        Head[v] = tol++;
    }
    
    int Numh[MAXN],h[MAXN],curedges[MAXN],pre[MAXN];
    
    void BFS(int end,int N)
    {
        memset(Numh,0,sizeof(Numh));
        for(int i = 1; i <= N; ++i)
            Numh[h[i]=N]++;
        h[end] = 0;
        Numh[N]--;
        Numh[0]++;
        queue<int> Q;
        Q.push(end);
    
        while(!Q.empty())
        {
            int v = Q.front();
            Q.pop();
            int i = Head[v];
            while(i != -1)
            {
                int u = Edges[i].to;
    
                if(h[u] < N)
                {
                    i = Edges[i].next;
                    continue;
                }
    
                h[u] = h[v] + 1;
                Numh[N]--;
                Numh[h[u]]++;
                Q.push(u);
                i = Edges[i].next;
            }
        }
    }
    
    int SAP(int start,int end,int N)
    {
        int Curflow,FlowAns = 0,temp,neck;
        memset(h,0,sizeof(h));
        memset(pre,-1,sizeof(pre));
        for(int i = 1; i <= N; ++i)
            curedges[i] = Head[i];
    
        BFS(end,N);
        int u = start;
        while(h[start] < N)
        {
            if(u == end)
            {
                Curflow = INF;
                for(int i = start; i != end; i = Edges[curedges[i]].to)
                {
                    if(Curflow > Edges[curedges[i]].w)
                    {
                        neck = i;
                        Curflow = Edges[curedges[i]].w;
                    }
                }
                for(int i = start; i != end; i = Edges[curedges[i]].to)
                {
                    temp = curedges[i];
                    Edges[temp].w -= Curflow;
                    Edges[temp^1].w += Curflow;
                }
                FlowAns += Curflow;
                u = neck;
            }
            int i;
            for(i = curedges[u]; i != -1; i = Edges[i].next)
                if(Edges[i].w && h[u]==h[Edges[i].to]+1)
                    break;
            if(i != -1)
            {
                curedges[u] = i;
                pre[Edges[i].to] = u;
                u = Edges[i].to;
            }
            else
            {
                if(0 == --Numh[h[u]])
                    break;
                curedges[u] = Head[u];
                for(temp = N,i = Head[u]; i != -1; i = Edges[i].next)
                    if(Edges[i].w)
                        temp = min(temp,h[Edges[i].to]);
                h[u] = temp + 1;
                ++Numh[h[u]];
                if(u != start)
                    u = pre[u];
            }
        }
        return FlowAns;
    }
    
    int main()
    {
        int m,n,x,y,v;
        while(scanf("%d%d",&m,&n)!=EOF)
        {
            init();
            for(int i=0;i<m;i++)
            {
                scanf("%d%d%d",&x,&y,&v);
                  add_edge(x,y,v);
            }
            printf("%d
    ",SAP(1,n,n));
        }
    
        return 0;
    }
    

      

  • 相关阅读:
    C#文件拖放至窗口的ListView控件获取文件类型
    android内存释放处理
    赵雅智_运用Bitmap和Canvas实现图片显示,缩小,旋转,水印
    POJ 3070 Fibonacci 矩阵高速求法
    poj 3261 后缀数组 找反复出现k次的子串(子串能够重叠)
    Codeforces Round #313 C. Gerald&#39;s Hexagon(放三角形)
    HTTP服务端JSON服务端
    iPad popView封装
    OpenCv 人脸检測的学习
    《深入理解java虚拟机》:类的初始化
  • 原文地址:https://www.cnblogs.com/a249189046/p/6877598.html
Copyright © 2011-2022 走看看