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


  • 相关阅读:
    时间复杂度理解
    elementUI表单校验汇总
    严选促销中心价格计算体系的建设之路
    sqlserver日志文件太大解决方法
    数据分析的 5 种细分方法
    批处理记录电脑磁盘剩余容量并输出到txt中
    关于sqlserver收缩数据库(引起的问题、可以半途停止吗)
    Sql Server 数据库总是显示“正在恢复、恢复挂起”的解决办法
    数据库“xxx”的事务日志已满,原因为“LOG_BACKUP”
    数据库分库分表策略的具体实现方案
  • 原文地址:https://www.cnblogs.com/zhangchengc919/p/5479702.html
Copyright © 2011-2022 走看看