zoukankan      html  css  js  c++  java
  • POJ1273(最大流入门)

    Drainage Ditches
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 70333   Accepted: 27336

    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 <cstdio>
    #include <cstring>
    #include <queue>
    #include <algorithm>
    using namespace std;
    const int MAXN=205;
    const int INF=0x3f3f3f3f;
    int arc[MAXN][MAXN];
    int n,m;
    int level[MAXN];
    bool bfs(int src,int ter)
    {
        memset(level,-1,sizeof(level));
        queue<int> que;
        que.push(src);
        level[src]=0;
        while(!que.empty())
        {
            int u=que.front();que.pop();
            for(int i=1;i<=n;i++)
            {
                if(level[i]<0&&arc[u][i]>0)
                {
                    que.push(i);
                    level[i]=level[u]+1;
                }    
            }
        }
        if(level[ter]>0)    return true;
        else    return false;    
    }
    
    int dfs(int u,int ter,int f)
    {
        if(u==ter)    return f;
        for(int i=1;i<=n;i++)
        {
            int d;
            if(arc[u][i]>0&&level[i]==level[u]+1&&(d=dfs(i,ter,min(arc[u][i],f))))
            {
                arc[u][i]-=d;
                arc[i][u]+=d;
                return d;
            }
        }
        return 0;
    }
    int max_flow(int src,int ter)
    {
        int ans=0;
        int d;
        while(bfs(src,ter))
        {
            while(d=dfs(src,ter,INF))    ans+=d;
        }
        return ans;
    }
    int main()
    {
    //    freopen("input.in","r",stdin);
        while(scanf("%d%d",&m,&n)!=EOF)
        {
            memset(arc,0,sizeof(arc));
            for(int i=0;i<m;i++)
            {
                int from,to,w;
                scanf("%d%d%d",&from,&to,&w);
                arc[from][to]+=w;
            }    
            int res=max_flow(1,n);
            printf("%d
    ",res);
        }
        return 0;
    }
  • 相关阅读:
    js备忘录3
    js备忘录2
    js备忘录1
    Java-URLEncoder.encode 什么时候才是必须的
    Oracle中的SQL分页查询原理和方法详解
    servlet 和 threadlocal 与 web容器(理解threadlocal)
    技术汇总:第十二章:技术总览
    MongoDB :第五章:MongoDB 插入更新删除查询文档
    MongoDB :第六章:Java程序操作MongoDB
    MongoDB :第二章:系统归纳
  • 原文地址:https://www.cnblogs.com/program-ccc/p/5880637.html
Copyright © 2011-2022 走看看