zoukankan      html  css  js  c++  java
  • poj 1273 最大流入门+dinic()

    题目链接:(Drainage Ditches)[http://poj.org/problem?id=1273]

    Drainage Ditches
    Time Limit: 1000MS Memory Limit: 10000K
    Total Submissions: 78773 Accepted: 30724
    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
    题解:最大流入门,先bfs层次遍历,然后求增广路。

    //#include<bits/stdc++.h>
    #include<iostream>
    #include<cstring>
    #include<queue>
    #define min(a,b) a<b?a:b
    #define pb push_back
    #define ll long long
    #define PI 3.14159265
    #define ls l,m,rt<<1
    #define rs m+1,r,rt<<1|1
    #define eps 1e-7
    const int inf=1e8;
    const int maxn=2e2+5;
    using namespace std;
    int dis[maxn];
    int tic[205][205],n,m;
    bool bfs()
    {
        memset(dis,0xff,sizeof(dis));
        queue<int>q;
        q.push(1);
        dis[1]=0;
        while(!q.empty())
        {
            int s=q.front();q.pop();
            for(int i=1;i<=m;i++)
            {
                if(dis[i]<0&&tic[s][i]>0)
                {
                    dis[i]=dis[s]+1;
                    q.push(i);
                }
            }
        }
        if(dis[m]>0)return true;
        else return false;
    }
    int dfs(int v,int low)
    {
        int a;
        if(v==m)return low;
        for(int i=1;i<=m;i++)
        {
            if(tic[v][i]>0&&dis[i]==dis[v]+1&&(a=dfs(i,min(low,tic[v][i]))))
            {
                tic[v][i]-=a;
                tic[i][v]+=a;
                return a;
            }
        }
        return 0;
    }
    int main()
    {
        std::ios::sync_with_stdio(false);
    	cin.tie(0);
    	cout.tie(0);
    	while(cin>>n>>m)
        {
            memset(tic,0,sizeof(tic));
            for(int i=0;i<n;i++)
            {
                int a,b,c;
                cin>>a>>b>>c;
                tic[a][b]+=c;
            }
            int ans=0;
            while(bfs())
            {
                int tmp;
                while(tmp=dfs(1,inf))ans+=tmp;
            }
            cout<<ans<<endl;
        }
    
    
    
    }
    
    
  • 相关阅读:
    2017-2018-1 课表
    所编裴书练习参考解答封面 [购买了书的同志记得一定要邮件联系, 并加我微信, 方便更正错误. 这里更新有时会慢, 或者懒得弄.]
    人工智能图片放大
    猜15个名人
    Excel 当前行高亮
    2014年至今的博文目录(更新至2019年1月7日,2017篇)
    拓扑学中凝聚点的几个等价定义
    江苏省2017年高等数学竞赛本二试题(含解答)
    裴礼文数学分析中的典型问题与方法第4章一元函数积分学练习
    2017年华东师范大学数学竞赛(数学类)试题
  • 原文地址:https://www.cnblogs.com/lhclqslove/p/7805439.html
Copyright © 2011-2022 走看看