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;
        }
    
    
    
    }
    
    
  • 相关阅读:
    Java中的synchronized以及读写锁
    Java中的HashMap低层实现原理
    AOP
    PageRank算法
    Python基础
    RF创建测试库
    RF-RequestsLibrary
    selenium webdriver
    RF开发关键字(四)
    RF关键字(三)
  • 原文地址:https://www.cnblogs.com/lhclqslove/p/7805439.html
Copyright © 2011-2022 走看看