PowerOJ 1679: Drainage Ditches(网络流入门) 最大流模板

来源:互联网 发布:奇 视频编辑软件 编辑:程序博客网 时间:2024/05/21 10:53

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.
The input includes several cases. For each case, the first line contains two space-separated integers, N (0 <= N <= 500) and M (2 <= M <= 500). 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 <= 1,000,000) is the maximum rate at which water will flow through the ditch.
For each case, output a single integer, the maximum rate at which water may emptied from the pond.
5 41 2 401 4 202 4 202 3 303 4 102 21 2 1001 2 200
50300
power

题意:n个点,m条边,给一个有向图,源点1,汇点n,求一个最大流

记录两个模板

红书模板,适合普通的只求流量的最大流,效率较高

#include <bits/stdc++.h>using namespace std;typedef long long LL;const int N = 500+10;const int INF = 2e9;struct node{    int v,f,next;    node(){}    node(int v,int f,int next):        v(v),f(f),next(next){}}E[N*10];int s,t;int n,m,top;int head[N];int d[N];int vis[N];void Init(){    top = 0;    memset(head,-1,sizeof head);}void add(int u,int v,int flow){    E[top] = node(v,flow,head[u]);    head[u] = top++;    E[top] = node(u,0,head[v]);    head[v] = top++;}bool bfs(){    memset(vis,0,sizeof vis);    d[s] = 0;    vis[s] = 1;    queue<int>q;    q.push(1);    while(!q.empty()){        int u = q.front();        q.pop();        for(int i = head[u];i != -1;i = E[i].next){            int v = E[i].v;            if(vis[v] || E[i].f == 0) continue;            vis[v] = 1;            d[v] = d[u]+1;            q.push(v);        }    }    return vis[t];}int dfs(int u,int a){    if(u == t || a == 0){        return a;    }    int flow = 0;    for(int i = head[u];i != -1;i = E[i].next){        int v = E[i].v;        if(d[v] != d[u]+1) continue;        int f = dfs(v,min(a,E[i].f));        if(f > 0){            E[i].f -= f;            E[i^1].f += f;            a -= f;   ///a是从这个点还能流出去的最大流量,流多少减多少            flow += f;        }    }    return flow;}int max_flow(){    int flow = 0;    while(bfs()){        flow += dfs(1,INF);    }    return flow;}int main(void){    while(scanf("%d%d",&m,&n) != EOF){        Init();        for(int i = 1;i <= m;i++){            int u,v,w;            scanf("%d%d%d",&u,&v,&w);            add(u,v,w);        }        s = 1,t = n;        printf("%d\n",max_flow());    }    return 0;}
火山哥给的模板,有其他要求的,比如要求最小割,可以使用这个(未写出最小割求法),效率较上一个差一点

#include<iostream>#include<cstring>#include<algorithm>#include<queue>#include<cstdio>#include<cstdlib>#define maxn 1111#define maxm 200010#define inf 1e8using namespace std;int num,s,t,p[maxm],cur[maxm],d[maxn],v[maxn];int n,m;struct edge{    int u,v,cap,flow,next;    edge(){}    edge(int u,int v,int cap,int flow,int next):u(u),v(v),cap(cap),flow(flow),next(next){}}E[maxm];void init(){    num=0;    memset(p,-1,sizeof(p));}void add(int u,int v,int cap){    E[num]=(edge){u,v,cap,0,p[u]};    p[u]=num++;    E[num]=(edge){v,u,0,0,p[v]};    p[v]=num++;}bool bfs(){    for(int i=s;i<=t;i++) v[i]=0;    v[s]=1;    d[s]=0;    queue<int> q;    q.push(s);    while(!q.empty())    {        int x=q.front();        q.pop();        for(int i=p[x];i!=-1;i=E[i].next)        {            edge e=E[i];            if(!v[e.v]&&e.cap>e.flow)            {                v[e.v]=1;                d[e.v]=d[x]+1;                q.push(e.v);            }        }    }    return v[t];}int dfs(int x,int a){    if(x==t||!a) return a;    int flow=0;    for(int &i=cur[x];i!=-1;i=E[i].next)    {        edge &e=E[i];        if (d[e.v]!=d[x]+1) continue;        int f=dfs(e.v,min(a,e.cap-e.flow));        if(f)        {            e.flow+=f;            E[i^1].flow-=f;            flow+=f;            a-=f;            if(!a) break;        }    }    return flow;}int dinic(){    int flow=0;    while(bfs())    {        for(int i=s;i<=t;i++) cur[i]=p[i];        flow+=dfs(s,inf);    }    return flow;}int main(){    while(scanf("%d%d",&m,&n) != EOF){        init();        for(int i = 1;i <= m;i++){            int u,v,w;            scanf("%d%d%d",&u,&v,&w);            add(u,v,w);        }        s = 1,t = n;        printf("%d\n",dinic());    }}




0 0
原创粉丝点击