网络流最大流模板

EK算法

#include<cstdio>
#include<vector>
#include<cstring>
#include<algorithm>
#include<queue>
#define REP(i, a, b) for(int i = (a); i < (b); i++)
using namespace std;

const int MAXN = 212;
struct Edge
{
	int from, to, cap, flow;
	Edge(int from = 0,int to = 0,int cap = 0,int flow = 0):from(from),to(to),cap(cap),flow(flow){}
};
vector<Edge> edges;
vector<int> g[MAXN];
int p[MAXN], a[MAXN], n, m; //p数组用来找到路径而修改流量的,a数组是从起点可以有的流量 

void AddEdge(int from, int to, int cap)
{
	edges.push_back(Edge(from, to, cap, 0));
	edges.push_back(Edge(to, from, 0, 0)); 
	g[from].push_back(edges.size() - 2);
	g[to].push_back(edges.size() - 1);
} 

int maxflow(int s, int t)
{
	int flow = 0;
	while(1)
	{
		memset(a, 0, sizeof(a));
		queue<int> q;
		q.push(s);
		a[s] = 1e9; //初始有无限流量 
		
		while(!q.empty())
		{
			int x = q.front(); q.pop();
			REP(i, 0, g[x].size())
			{
				Edge& e = edges[g[x][i]];
				if(!a[e.to] && e.cap > e.flow) //没有访问过且还可以增加流量 
				{
					p[e.to] = g[x][i]; //储存路径 
					a[e.to] = min(a[x], e.cap - e.flow); //注意这里取min,本身的限制已经可以达到的流量 
					q.push(e.to);
				}
			}
			if(a[t]) break;
		}
		
		if(!a[t]) break;
		flow += a[t];
		for(int u = t; u != s; u = edges[p[u]].from)
		{
			edges[p[u]].flow += a[t];
			edges[p[u] ^ 1].flow -= a[t]; //反向边流量减少,后面可以后悔, 精华在这 
		}
	}
	
	return flow;
}

int main()
{
	scanf("%d%d", &m, &n);
	while(m--)
	{
		int from, to, cap;
		scanf("%d%d%d", &from, &to, &cap);
		AddEdge(from, to, cap);
	}
	printf("%d\n", maxflow(1, n));
	return 0;	
} 

Dinic算法
#include<cstdio>
#include<vector>
#include<cstring>
#include<algorithm>
#include<queue>
#define REP(i, a, b) for(int i = (a); i < (b); i++)
using namespace std;

const int MAXN = 212;
struct Edge
{
	int from, to, cap, flow;
	Edge(int from = 0,int to = 0,int cap = 0,int flow = 0):from(from),to(to),cap(cap),flow(flow){}
};
vector<Edge> edges;
vector<int> g[MAXN];
int h[MAXN], cur[MAXN]; 
int n, m, s, t;  

void AddEdge(int from, int to, int cap)
{
	edges.push_back(Edge(from, to, cap, 0));
	edges.push_back(Edge(to, from, 0, 0)); 
	g[from].push_back(edges.size() - 2);
	g[to].push_back(edges.size() - 1);
} 

bool bfs()
{
	memset(h, 0, sizeof(h));
	queue<int> q;
	q.push(s);
	h[s] = 1;
	
	while(!q.empty())
	{
		int x = q.front(); q.pop();
		REP(i, 0, g[x].size())
		{
			Edge& e = edges[g[x][i]];
			if(!h[e.to] && e.cap > e.flow) //记住考虑的是残量网络内的图 
			{
				h[e.to] = h[x] + 1;
				q.push(e.to);	
			} 
		}
	}
	
	return h[t];
}

int dfs(int x, int a)
{
	if(x == t || a == 0) return a;
	int flow = 0, f;
	for(int& i = cur[x]; i < g[x].size(); i++) //牛逼的优化,从上次考虑的弧开始做, 每次i++, cur[x]也++ 
		{
			Edge& e = edges[g[x][i]];
			if(h[x] + 1 == h[e.to] && (f = dfs(e.to, min(e.cap - e.flow, a))) > 0)
			{
				e.flow += f;
				edges[g[x][i] ^ 1].flow -= f;
				flow += f;
				a -= f;
				if(a == 0) break;
			}
		}	
	return flow;
}

int main()
{
	scanf("%d%d", &m, &n);
	s = 1; t = n;
	while(m--)
	{
		int from, to, cap;
		scanf("%d%d%d", &from, &to, &cap);
		AddEdge(from, to, cap);
	}
	int ans = 0;
	while(bfs()) memset(cur, 0, sizeof(cur)), ans += dfs(s, 1e9); 
	printf("%d\n", ans);
	return 0;	
} 


猜你喜欢

转载自blog.csdn.net/qq_34416123/article/details/80460476