最大流自用模板(例题:HDU1532)

16 篇文章 0 订阅
3 篇文章 0 订阅

三种模板:Edmonds_Karp,Dinic,SAP

例题:

Drainage Ditches(HDU1532)

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 22365    Accepted Submission(s): 10683)

 

Problem 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 

 

Source

HDU1532

题意:最大流模板题

方法一:Edmonds_Karp
 

#include <cstdio>
#include <iostream>
#include <vector>
#include <queue>
#include <cstring>
#include <algorithm>
const int maxn=205;
const int INF=0x3f3f3f3f;
using namespace std;

struct Edge
{
	int from,to,cap,flow;
	Edge(int u,int v,int c,int f):from(u),to(v),cap(c),flow(f){}
};

vector<Edge> edges;
vector<int> G[maxn];
int a[maxn];
int pre[maxn];

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

void addedge(int from,int to,int cap)
{
	edges.push_back(Edge(from,to,cap,0));
	edges.push_back(Edge(to,from,0,0));
	int m=edges.size();
	G[from].push_back(m-2);
	G[to].push_back(m-1);
}

int Edmonds_Karp(int s,int t)
{
	int flow=0;
	while(1)
	{
		memset(a,0,sizeof(a));
		queue<int> Q;
		Q.push(s);
		a[s]=INF;
		while(!Q.empty())
		{
			int x=Q.front();
			Q.pop();
			int sz=G[x].size();
			for (int i=0;i<sz;i++)
			{
				Edge& e=edges[G[x][i]];
				if (!a[e.to]&&e.cap>e.flow)
				{
					pre[e.to]=G[x][i];
					a[e.to]=min(a[x],e.cap-e.flow);
					Q.push(e.to);
				}
			}
			if (a[t])
				break;
		}
		if (!a[t])
			break;
		for (int u=t;u!=s;u=edges[pre[u]].from)
		{
			edges[pre[u]].flow+=a[t];
			edges[pre[u]^1].flow-=a[t];
		}
		flow+=a[t];
	}
	return flow;
}

int main()
{
	int n,m,u,v,f;
	while(cin >> n >> m)
	{
		init(n);
		for (int i=0;i<n;i++)
		{
			scanf("%d%d%d",&u,&v,&f);
			addedge(u,v,f);
		}
		cout << Edmonds_Karp(1,m) << endl;
	}
	return 0;
}

方法二:Dinic

#include <cstdio>
#include <iostream>
#include <vector>
#include <queue>
#include <cstring>
#include <algorithm>
const int maxn=205;
const int INF=0x3f3f3f3f;
using namespace std;

struct Edge
{
    int from,to,flow;
    Edge(int u,int v,int f):from(u),to(v),flow(f){}
};

vector<Edge> edges;
vector<int> G[maxn];

int dis[maxn];
int cur[maxn];

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


void addedge(int from,int to,int flow)
{
    edges.push_back(Edge(from,to,flow));
    edges.push_back(Edge(to,from,0));
    int m=edges.size();
    G[from].push_back(m-2);
    G[to].push_back(m-1);
}

bool bfs(int s,int t)
{
    queue<int> Q;
    Q.push(s);
    memset(dis,-1,sizeof(dis));
    dis[s]=0;
    while(!Q.empty())
    {
        int x=Q.front();
        Q.pop();
        int sz=G[x].size();
        for (int i=0;i<sz;i++) 
        {
            Edge& e=edges[G[x][i]];
            if (e.flow>0) 
            {
                if (dis[e.to]<0) 
                {
                    dis[e.to]=dis[x]+1;
                    Q.push(e.to);
                }
            }
        }
    }
    return bool(~dis[t]);
}

int dfs(int s,int t,int maxflow) 
{
    if (s==t)
        return maxflow;
    int sz=int(G[s].size());
    for (int i=cur[s],num;num=G[s][i],i<sz;i++)
    {
        cur[s]=i;
        Edge &e=edges[num];
        if (dis[e.to]==dis[s]+1 && e.flow>0)
        {
            int flow=dfs(e.to,t,min(maxflow,e.flow));
            if (flow!=0)
            {
                e.flow-=flow;
                edges[num^1].flow+=flow;
                return flow;
            }
        }
    }
    return 0;
}

int Dinic(int s,int t)
{
    int ans=0;
    while(bfs(s,t))
    {
        int flow;
        memset(cur,0,sizeof(cur));
        while((bool)(flow=dfs(s,t,INF)))
            ans+=flow;
    }
    return ans;
}

int main()
{
    int n,m,u,v,f;
    while(cin >> n >> m)
    {
        init(n);
        for (int i=0;i<n;i++)
        {
            scanf("%d%d%d",&u,&v,&f);
            addedge(u,v,f);
        }
        cout << Dinic(1,m) << endl;
    }
    return 0;
}

方法三:SAP

#include <cstdio>
#include <iostream>
#include <vector>
#include <queue>
#include <cstring>
#include <algorithm>
const int maxn=205;
const int maxm=maxn*maxn;
const int INF=0x3f3f3f3f;
using namespace std;

struct Edge
{  
    int v,w,next;  
}edge[maxm];

int dis[maxn],pre[maxn],rec[maxn],head[maxn],gap[maxn],now[maxn];  
int n,m,no,up;
queue<int> q;

void addedge(int u,int v,int w)
{
    edge[no].v=v; edge[no].w=w;
    edge[no].next=head[u]; head[u]=no++;
    edge[no].v=u; edge[no].w=0;
    edge[no].next=head[v]; head[v]=no++;
}

void pre_init()  
{  
    no=0; up=n;
    memset(head,-1,sizeof(head));  
}

void init(int s,int t)
{
    for(int i=0;i<=up;i++) 
    {
    	now[i]=head[i];
    	gap[i]=0;
        dis[i]=INF;
	}

    while(!q.empty())
        q.pop();

    dis[t]=0; q.push(t);
    while(!q.empty())
    {
        int tp=q.front();
        q.pop();
        gap[dis[tp]]++;
        int k=head[tp];
        while(k!=-1)
        {
            if(dis[edge[k].v]==INF && edge[k^1].w)  
            {
                dis[edge[k].v]=dis[tp]+1;  
                q.push(edge[k].v);  
            }
            k=edge[k].next;  
        }
    }
}

int SAP(int s,int t)  
{  
    int ans=0,flow=INF,top=s;  
    pre[s]=s;
    init(s,t);  
    while(dis[s]<up)   
    {  
        if(top==t)  
        {  
            ans+=flow;  
            while(top!=s)
            {  
                edge[rec[top]].w-=flow;  
                edge[rec[top]^1].w+=flow;  
                top=pre[top];  
            }
            flow=INF;  
        }  
        int k=now[top];  
        while(k!=-1)  
        {  
            int v=edge[k].v;  
            if(edge[k].w && dis[top]==dis[v]+1)  
            {  
                flow=min(flow, edge[k].w);  
                pre[v]=top; rec[v]=k;  
                now[top]=k;   
                top=v;  
                break;  
            }  
            k=edge[k].next;  
        }  
        if(k==-1)
        {  
            int mind=n;  
            if(--gap[dis[top]]==0) break;
            int k=now[top]=head[top];
            while(k!=-1)  
            {  
                if(edge[k].w && mind>dis[edge[k].v])
                    mind=dis[edge[k].v];  
                k=edge[k].next;  
            }  
            gap[dis[top]=mind+1]++;  
            top=pre[top];  
        }
    }
    return ans;  
}

int main()
{
    int u,v,f;
    while(cin >> n >> m)
    {
        pre_init();
        for (int i=0;i<n;i++)
        {
            scanf("%d%d%d",&u,&v,&f);
            addedge(u,v,f);
        }
        cout << SAP(1,m) << endl;
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值