zoukankan      html  css  js  c++  java
  • poj-1273 Drainage Ditches(最大流基础题)

    题目链接:

    Drainage Ditches

    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 67475   Accepted: 26075

    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


    题意:

    就是求一个1到n的最大流,入门题;

    思路:

    简直就是模板,有个wa点就是有重边;

    AC代码:

    //#include <bits/stdc++.h>
    #include <iostream>
    #include <queue>
    #include <cmath>
    #include <cstring>
    #include <algorithm>
    #include <cstdio>
    using namespace std;
    #define Riep(n) for(int i=1;i<=n;i++)
    #define Riop(n) for(int i=0;i<n;i++)
    #define Rjep(n) for(int j=1;j<=n;j++)
    #define Rjop(n) for(int j=0;j<n;j++)
    #define mst(ss,b) memset(ss,b,sizeof(ss));
    typedef long long LL;
    const LL mod=1e9+7;
    const double PI=acos(-1.0);
    const int inf=0x3f3f3f3f;
    const int N=2e4+5;
    int n,m,flow[250],path[250],cap[250][250];
    queue<int>qu;
    int bfs()
    {
        while(!qu.empty())qu.pop();
        mst(path,-1);
        path[1]=0;
        flow[1]=inf;
        qu.push(1);
        while(!qu.empty())
        {
            int fr=qu.front();
            qu.pop();
            Riep(m)
            {
                if(i!=1&&cap[fr][i]&&path[i]==-1)
                {
                    path[i]=fr;
                    flow[i]=min(cap[fr][i],flow[fr]);
                    qu.push(i);
                }
            }
        }
        if(path[m]==-1)return -1;
        return flow[m];
    }
    int maxflow()
    {
        int sum=0;
        int temp,now,pre;
        while(1)
        {
            temp=bfs();
            if(temp==-1)break;
            sum+=temp;
            now=m;
            while(now!=1)
            {
                pre=path[now];
                cap[pre][now]-=temp;
                cap[now][pre]+=temp;
                now=pre;
            }
        }
        return sum;
    }
    int main()
    {
        while(scanf("%d%d",&n,&m)!=EOF)
        {
            int u,v,w;
            mst(cap,0);
            Riep(n)
            {
                scanf("%d%d%d",&u,&v,&w);
                cap[u][v]+=w;
            }
            printf("%d
    ",maxflow());
        }
    
        return 0;
    }


  • 相关阅读:
    边走边学Nodejs (基础入门篇)
    Android应用打包安装过程具体解释
    ubuntu与centos安装软件的不同点总结
    你好,C++(12)怎样管理多个类型同样性质同样的数据?3.6 数组
    oracle暂时表空间 ORA-01652:无法通过16(在表空间XXX中)扩展 temp 字段
    iOS中sqlite3操作
    sparkSQL1.1入门之二:sparkSQL执行架构
    [NHibernate]视图处理
    [NHibernate]立即加载
    [NHibernate]延迟加载
  • 原文地址:https://www.cnblogs.com/zhangchengc919/p/5479702.html
Copyright © 2011-2022 走看看