zoukankan      html  css  js  c++  java
  • POJ 1273 Drainage Ditches (网络流Dinic模板)

    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

    网络流Dinic模板
    #include <iostream>
    #include <cstdio>
    #include <cmath>
    #include <algorithm>
    #include <queue>
    #include <string>
    #include <cstring>
    using namespace std;
    const int inf = 0x3f3f3f3f;
    const int maxn = 220;
    int c[maxn][maxn];
    int dep[maxn];
    int cur[maxn];
    int n,m;
    void pt()
    {
        for (int i=1;i<=n;++i){
                for (int j=1;j<=n;++j)
                    printf("%d ",c[i][j]);
                printf("
    ");
            }
        printf("==================
    ");
    }
    int bfs (int s,int t)
    {
        memset(dep,-1,sizeof dep);
        queue<int> q;
        while (!q.empty()) q.pop();
        dep[s] = 0;
        q.push(s);
        while (!q.empty()){
            int u = q.front();
            q.pop();
            for (int v=1;v<=n;++v){
                if (c[u][v]>0&&dep[v]==-1){//能到达该节点的条件是这条边有流量,而且这个点没有被访问
                    dep[v] = dep[u]+1;
                    q.push(v);
                }
            }
        }
        return dep[t]!=-1;
    }
    int dfs (int u,int mi,int t)
    {
        if (u==t)
            return mi;
        int tmp;
        for (int &v=cur[u];v<=n;++v){//
            if (c[u][v]>0&&dep[v]==dep[u]+1&&(tmp=dfs(v,min(mi,c[u][v]),t))){//下一节点的深度是当前节点+1
                c[u][v]-=tmp;
                c[v][u]+=tmp;
                return tmp;
            }
        }
        return 0;//别忘写返回0!!!
    }
    int dinic ()
    {
        int ans = 0;
        int tmp;
        while (bfs(1,n)){//每次按照深度建立分层图,这样每次dfs的时候下一节点的深度是当前节点+1
            while (1){
                for (int i=0;i<maxn;++i) cur[i]=1;//当前弧优化
                tmp = dfs(1,inf,n);
                //printf("%d
    ",tmp);
                if (tmp==0)
                    break;
                //pt();
                ans+=tmp;
            }
        }
        return ans;
    }
    int main()
    {
        //freopen("de.txt","r",stdin);
        while (~scanf("%d%d",&m,&n)){
            memset(c,0,sizeof c);
            for (int i=0;i<m;++i){
                int u,v,cap;
                scanf("%d%d%d",&u,&v,&cap);
                c[u][v]+=cap;
            }
            printf("%d
    ",dinic());
        }
        return 0;
    }
    
    
    
     

    #include <iostream>#include <cstdio>#include <cmath>#include <algorithm>#include <queue>#include <string>#include <cstring>using namespace std;const int inf = 0x3f3f3f3f;const int maxn = 220;int c[maxn][maxn];int dep[maxn];int cur[maxn];int n,m;void pt(){    for (int i=1;i<=n;++i){            for (int j=1;j<=n;++j)                printf("%d ",c[i][j]);            printf(" ");        }    printf("================== ");}int bfs (int s,int t){    memset(dep,-1,sizeof dep);    queue<int> q;    while (!q.empty()) q.pop();    dep[s] = 0;    q.push(s);    while (!q.empty()){        int u = q.front();        q.pop();        for (int v=1;v<=n;++v){            if (c[u][v]>0&&dep[v]==-1){//能到达该节点的条件是这条边有流量,而且这个点没有被访问                dep[v] = dep[u]+1;                q.push(v);            }        }    }    return dep[t]!=-1;}int dfs (int u,int mi,int t){    if (u==t)        return mi;    int tmp;    for (int &v=cur[u];v<=n;++v){//        if (c[u][v]>0&&dep[v]==dep[u]+1&&(tmp=dfs(v,min(mi,c[u][v]),t))){//下一节点的深度是当前节点+1            c[u][v]-=tmp;            c[v][u]+=tmp;            return tmp;        }    }    return 0;//别忘写返回0!!!}int dinic (){    int ans = 0;    int tmp;    while (bfs(1,n)){//每次按照深度建立分层图,这样每次dfs的时候下一节点的深度是当前节点+1        while (1){            for (int i=0;i<maxn;++i) cur[i]=1;//当前弧优化            tmp = dfs(1,inf,n);            //printf("%d ",tmp);            if (tmp==0)                break;            //pt();            ans+=tmp;        }    }    return ans;}int main(){    //freopen("de.txt","r",stdin);    while (~scanf("%d%d",&m,&n)){        memset(c,0,sizeof c);        for (int i=0;i<m;++i){            int u,v,cap;            scanf("%d%d%d",&u,&v,&cap);            c[u][v]+=cap;        }        printf("%d ",dinic());    }    return 0;}

  • 相关阅读:
    【c++】中文设置
    《谁动了我的奶酪》读后感
    KMP算法的C++实现
    我也说说中文分词(上:基于字符串匹配)
    删除字符串中的空格
    linux jdk bin安装
    笔试题汇总
    栈的压入、弹出序列
    顺序打印矩阵
    二叉树镜像
  • 原文地址:https://www.cnblogs.com/agenthtb/p/7450555.html
Copyright © 2011-2022 走看看