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;
    }


  • 相关阅读:
    windows7 下 apache2.4 和 php5.5 及 mysql5.6 的安装与配置
    JavaScript高级编程 (1)
    关于解决乱码问题的一点探索之二(涉及Unicode(utf-16)和GBK)
    关于解决乱码问题的一点探索之一(涉及utf-8和GBK)
    Windows上安装、配置MySQL的常见问题
    解析DXF图形文件格式
    vue三十一:vue配置反向代理
    vue三十:eslint修复错误和打包用于生产
    vue二十九:多个文件组件集成
    vue二十八:Vue-Cli之环境搭建之node安装脚手架和使用脚手架创建vue项目
  • 原文地址:https://www.cnblogs.com/zhangchengc919/p/5479702.html
Copyright © 2011-2022 走看看