• poj1273


                                                                                                  Drainage Ditches
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 49349   Accepted: 18689

    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 <cstdio>
    #include <cstring>
    #define MAXN 210
    
    struct Matrix
    {
        int c, f;    //容量,流量
    };
    Matrix Edge[MAXN][MAXN]; //流及容量(邻接矩阵)
    int M, N;    //读入的排水沟(即弧)数目,和汇合结点(即顶点)数目
    int s, t;    //源点(结点1)、汇点(结点N)
    int residual[MAXN][MAXN];    //残留网络
    int qu[MAXN*MAXN], qs, qe;    //队列、队列头和尾
    int pre[MAXN];    //pre[i]为增广路上顶点i前面的顶点序号
    int vis[MAXN];    //BFS算法中各顶点的访问标志
    int maxflow, min_augment;    //最大流流量、每次增广时的可改进量
    
    void find_augment_path( )    //BFS求增广路
    {
        int i, cu;    //cu为队列头顶点
        memset( vis, 0, sizeof(vis) );
        qs = 0;  qu[qs] = s;    //s入队列
        pre[s] = s;  vis[s] = 1;  qe = 1;
        memset( residual, 0, sizeof(residual) );  memset( pre, 0, sizeof(pre) );
        while( qs<qe && pre[t]==0 )
        {
            cu = qu[qs];
            for( i=1; i<=N; i++ )
            {
                if( vis[i]==0 )
                {
                    if( Edge[cu][i].c - Edge[cu][i].f >0 )
                    {
                        residual[cu][i] = Edge[cu][i].c - Edge[cu][i].f;
                        pre[i] = cu;  qu[qe++] = i;  vis[i] = 1;
                    }
                    else if( Edge[i][cu].f>0 )
                    {
                        residual[cu][i] = Edge[i][cu].f;
                        pre[i] = cu;  qu[qe++] = i;  vis[i] = 1;
                    }
                }
            }
            qs++;
        }
    }
    
    void augment_flow( )    //计算可改进量
    {
        int i = t, j;    //t为汇点
        if( pre[i]==0 )
        {
            min_augment = 0;  return;
        }
        j = 0x7fffffff;
        while( i!=s )    //计算增广路上可改进量的最小值
        {
            if( residual[pre[i]][i]<j )  j = residual[pre[i]][i];
            i = pre[i];
        }
        min_augment = j;
    }
    
    void update_flow( )    //调整流量
    {
        int i = t;    //t为汇点
        if( pre[i]==0 )  return;
        while( i!=s )
        {
            if( Edge[pre[i]][i].c - Edge[pre[i]][i].f > 0 )
                Edge[pre[i]][i].f += min_augment;
            else if( Edge[i][pre[i]].f >0 )
                Edge[pre[i]][i].f+=min_augment;
            i = pre[i];
        }
    }
    
    void solve( )
    {
        s = 1;  t = N;
        maxflow = 0;
        while( 1 )
        {
            find_augment_path( ); //BFS寻找增广路
            augment_flow( );    //计算可改进量
            maxflow += min_augment; //
            if( min_augment>0 )  update_flow( ); //更新流
            else return;
        }
    }
    
    int main( )
    {
        int i;
        int u, v, c;
        while( scanf("%d %d",&M,&N)!=EOF )
        {
            memset( Edge, 0, sizeof(Edge) );
            for( i=0; i<M; i++ )
            {
                scanf( "%d %d %d", &u, &v, &c );
                Edge[u][v].c += c;
            }
            solve( );
            printf( "%d
    ", maxflow );
        }
        return 0;
    }
  • 相关阅读:
    React native 之 图标库ECharts的使用
    使用jquery给html标签加点击事件
    React native 之 async/await
    CSS布局之flexbox
    Swiper 的引入
    给浏览器设置一张背景图,并且拉动浏览器大小时图片不要被压缩变形
    境界的彼方_lduoj_bfs宽搜
    2021美国大学生数学建模大赛==ABCDEF+思路解析==
    3045 Lcm与Gcd构造
    对拍程序
  • 原文地址:https://www.cnblogs.com/Deng1185246160/p/3261407.html
Copyright © 2020-2023  润新知