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;
    }
  • 相关阅读:
    struts2文件下载的编写步骤(文件导出)和输入流转换的方法
    spring的applicationContext.xml配置SessionFactory抛异常
    引用第三方高德地图接口---使用js脚本进行开发地图定位的步骤
    登陆时不同浏览器获取session存在的相关疑问?
    统一的异常处理和自定义的全局异常处理器的配置使用
    国际化的工具类ognl utils
    oracle中decode的用法(例子)
    PLSQL连接本机oracle 11g 64 数据库的步骤
    处理oracle 报ORA-12505 信息:listener does not currently know of SID given in connect descriptor...
    spring容器的配置和springmvc的配置
  • 原文地址:https://www.cnblogs.com/program-ccc/p/5880637.html
Copyright © 2011-2022 走看看