Drainage Ditches POJ - 1273 (入门网络流最大流 EK算法)

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到m,求最大的流出量,中间有很多的通道

        思路:套EK算法的模板

    

#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<vector>
#include<map>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
const int mod=1e9+7;
const int N=1000+10;
struct node
{
    int v,c,r;//r表示相反的边:u->v,v->u,对称性
};
vector<node>g[N];
int vis[N];
void add(int u,int v,int c)
{
    g[u].push_back((node){v,c,g[v].size()});//存正边,
    g[v].push_back((node){u,0,g[u].size()-1});//存反边
}
int dfs(int s,int t,int f)
{
    if(s==t)//到达汇点结束搜索
        return f;
    vis[s]=1;
    for(int i=0;i<g[s].size();i++)
    {
        node &temp=g[s][i];
        if(!vis[temp.v]&&temp.c>0)
        {
            int d=dfs(temp.v,t,min(f,temp.c));//找到最小的可流量,流量守恒
            if(d>0)
            {
                temp.c-=d;  //减少容量
                g[temp.v][temp.r].c+=d;//相反的边增加容量
                return d;
            }
        }
    }
    return 0;
}
int solve(int s,int t)
{
    int flow=0;
    while(1)
    {
        memset(vis,0,sizeof(vis));
        int f=dfs(s,t,inf); //每次寻找增广路,找到可以增加的流量
        if(f==0)            //没有增广路的时候结束
            return flow;
        flow+=f;
    }
}
int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        for(int i=0;i<=n;i++) g[i].clear();
        for(int i=0;i<n;i++)
        {
            int u,v,c;
            scanf("%d%d%d",&u,&v,&c);
            add(u,v,c);
        }
        printf("%d\n",solve(1,m));
    }
}

发布了10 篇原创文章 · 获赞 1 · 访问量 358

猜你喜欢

转载自blog.csdn.net/hl_1997/article/details/80024943