POJ - 1273(最大流模板)

本文深入探讨了三种流量网络算法:Edmonds-Karp算法、Ford-Fulkerson算法和Dinic算法。通过实例讲解每种算法的工作原理及实现过程,并提供完整的代码示例。

Drainage Ditches

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

题意:……..

思路:模板

EK

#include <iostream>
#include <fstream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <cmath>
#include <algorithm>
#include <functional>
#define inf 0X3f3f3f3f
using namespace std;
typedef long long ll;
const int MAXN=1e5+10;
const int MAX=300+10;
const double eps=1e-6;

int n,m;
int G[MAX][MAX];
int pre[MAX],vis[MAX];
int flow[MAX];

void updata(int node,int flow){
    while(pre[node]!=-1){
        G[pre[node]][node]-=flow;
        G[node][pre[node]]+=flow;
        node=pre[node];
    }
}

int bfs(int s,int t){
    memset(vis,0,sizeof(vis));
    memset(pre,-1,sizeof(pre));
    vis[s]=1;
    int minn=inf;
    queue<int>q;
    q.push(s);
    flow[s]=inf;
    while(q.size()){
        int u=q.front();
        q.pop();
        if(u==t)    break;
        for(int i=1;i<=n;i++){
            if(!vis[i]&&G[u][i]){
                q.push(i);
                flow[i]=min(G[u][i],flow[u]);
                pre[i]=u;
                vis[i]=1;
            }
        }
    }
    if(pre[t]==-1)
        return 0;
    else
        return flow[t];
}

int edmonds_karp(int s,int t){
    int temp=0;
    int ans=0;
    do{
        temp=bfs(s,t);
        updata(t,temp);
        ans+=temp;
    }while(temp);
    return ans;
}

int main(){
    #ifdef ONLINE_JUDGE
    #else
    freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    #endif

    while(cin>>m>>n){
        memset(flow,0,sizeof(flow));
        memset(G,0,sizeof(G));
        for(int i=1;i<=m;i++){
            int u,v,c;
            cin>>u>>v>>c;
            if(u==v)    continue;
            G[u][v]+=c;
        }
        cout<<edmonds_karp(1,n)<<endl;
    }
    return 0;
}

Ford-Fulkerson

#include <iostream>
#include <fstream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <cmath>
#include <algorithm>
#include <functional>
#define inf 0X3f3f3f3f
using namespace std;
typedef long long ll;
const int MAXN=1e5+10;
const int MAX=500+10;
const double eps=1e-6;

int n,m;
struct EDGE{
    int v,c,rev;
};
vector<EDGE>G[MAX];
bool vis[MAX];

void init(){
    for(int i=1;i<=n;i++)
        G[i].clear();
} 

void addedge(int u,int v,int c){
    G[u].push_back((EDGE){v,c,G[v].size()});
    G[v].push_back((EDGE){u,0,G[u].size()-1});
}

int dfs(int v,int t,int f){
    if(v==t)    return f;
    vis[v]=1;
    for(int i=0;i<G[v].size();i++){
        EDGE &e=G[v][i];
        if(!vis[e.v]&&e.c>0){
            int d=dfs(e.v,t,min(f,e.c));
            if(d>0){
                e.c-=d;
                G[e.v][e.rev].c+=d;
                return d;
            }
        }
    }
    return 0;
}

int max_flow(int s,int t){
    int flow=0;
    while(1){
        memset(vis,0,sizeof(vis));
        int f=dfs(s,t,inf);
        if(!f)  return flow;
        flow+=f;
    }
}

int main(){
    #ifdef ONLINE_JUDGE
    #else
    freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    #endif
    while(cin>>m>>n){
        init();
        int u,v,c;
        for(int i=1;i<=m;i++){
            cin>>u>>v>>c;
            addedge(u,v,c);
        }
        cout<<max_flow(1,n)<<endl;
    }
    return 0;
}

Dinic

#include <iostream>
#include <fstream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <cmath>
#include <algorithm>
#include <functional>
#define inf 0X3f3f3f3f
using namespace std;
typedef long long ll;
const int MAXN=1e5+10;
const int MAX=500+10;
const double eps=1e-6;

int n,m;
struct EDGE{
    int v,c,rev;
};
vector<EDGE>G[MAX];
int level[MAX];//定点到原点的距离标号
int iter[MAX];//当前弧

void init(){
    for(int i=1;i<=n;i++)
        G[i].clear();
}

void addedge(int u,int v,int cap){
    G[u].push_back((EDGE){v,cap,G[v].size()});
    G[v].push_back((EDGE){u,0,G[u].size()-1});
}

//BFS计算从源点出发的距离标号
void bfs(int s){
    memset(level,-1,sizeof(level));
    queue<int>q;
    level[s]=0;
    q.push(s);
    while(q.size()){
        int u=q.front();q.pop();
        for(int i=0;i<G[u].size();i++){
            EDGE &e=G[u][i];
            if(e.c>0&&level[e.v]<0){
                level[e.v]=level[u]+1;
                q.push(e.v);
            }
        }
    }
}

//dfs寻找增广路
int dfs(int v,int t,int f){
    if(v==t)    return f;
    for(int &i=iter[v];i<G[v].size();i++){
        EDGE &e=G[v][i];
        if(e.c>0&&level[v]<level[e.v]){
            int d=dfs(e.v,t,min(f,e.c));
            if(d>0){
                e.c-=d;
                G[e.v][e.rev].c+=d;
                return d;
            }
        }
    }
    return 0;
}

int max_flow(int s,int t){
    int flow=0;
    while(1){
        bfs(s);
        if(level[t]<0)  return flow;
        memset(iter,0,sizeof(iter));
        int f;
        while((f=dfs(s,t,inf))>0)
            flow+=f;
    }
}

int main(){
    #ifdef ONLINE_JUDGE
    #else
    freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    #endif
    while(cin>>m>>n){
        init();
        int u,v,c;
        for(int i=1;i<=m;i++){
            scanf("%d%d%d",&u,&v,&c);
            addedge(u,v,c);
        }
        cout<<max_flow(1,n)<<endl;
    }
    return 0;
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值