第一次接触网络流,感觉像是一堆的算法扑面而来,三天就要过去了,才刚刚对几个算法有了点初步的理解,感觉上离要熟练的做出题还很遥远,这里先给出小编对几个算法的模板总结。
1.Edmond-Karp算法
这种算法是最好理解的,网络上也有很多的对此算法的讲解,这里小编就不给出详细的讲解。算法的关键就是不停的在残留网络中找到增广路径,并不停的修改残留网络中的值,最后知道找不到增广路径为止,得到最大流。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn = 200+2;
const int INF = 0xfffffff;
int capacity[maxn][maxn],flow[maxn],pre[maxn],n,m;
queue<int> q;
int BFS(int src,int des)
{
int i,j;
while(!q.empty())
q.pop();//队列清空
for(i=1; i<=m; i++)
pre[i] = -1;
pre[src] = 0;
flow[src] = INF;
q.push(src);
while(!q.empty())
{
int index = q.front();
q.pop();
if(index == des) break;//找到了增广路径
for(i=1; i<=m; i++)
{
if(i!=src && capacity[index][i]>0 && pre[i]==-1)
{
pre[i] = index;//记录前驱
flow[i] = min(capacity[index][i],flow[index]);
q.push(i);
}
}
}
if(pre[des] == -1) return -1;//残留图中不再存在增广路径
else return flow[des];
}
int maxFlow(int src,int des)
{
int increasement = 0;
int sumflow = 0;
while((increasement = BFS(src,des)) != -1)
{
int k = des;//利用前驱寻找路径
while(k != src)
{
int last = pre[k];
capacity[last][k] -= increasement;//改变正向边的容量
capacity[k][last] += increasement;//改变反向边的容量
k = last;
}
sumflow += increasement;
}
return sumflow;
}
int main()
{
int i,j;
int start,end,ci;
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(capacity,0,sizeof(capacity));
memset(flow,0,sizeof(flow));
for(i=0; i<n; i++)
{
scanf("%d%d%d",&start,&end,&ci);
if(start == end) continue;
capacity[start][end] += ci;
}
printf("%d\n",maxFlow(1,m));
}
return 0;
}
2.SPA
网络流的算法大都可以分成两种方式书写,这里小编给出的是SPA的邻接图的算法,有关SPA,一个重要的优化就是gap优化,达到寻找断路停止搜索的目的。
#include <cstdio>//网络流SPA算法(邻接矩阵)
#include <iostream>
#include <cstring>
using namespace std;
const int maxn = 222;
const int INF = 0xfffffff;
int map[maxn][maxn];
int pre[maxn];
int level[maxn];
int gap[maxn];
int NV,NE;
int SAP(int vs,int vt)
{
memset(pre,-1,sizeof(pre));
memset(level,0,sizeof(level));
memset(gap,0,sizeof(gap));
gap[0] = vt;
int v,u=pre[vs]=vs,maxflow=0,aug=INF;
while(level[vs] < vt)
{//寻找可行弧
for(v=1; v<=vt; v++)
if(map[u][v]>0 && level[u]==level[v]+1)
break;
if(v <= vt)
{
pre[v] = u;
u = v;
if(v == vt)
{
aug = INF;
for(int i=v; i!=vs; i=pre[i])
if(aug > map[pre[i]][i]) aug = map[pre[i]][i];
maxflow += aug;
for(int i=v; i!=vs; i=pre[i])
{
map[pre[i]][i] -= aug;
map[i][pre[i]] += aug;
}
u = vs;
}
}
else
{
int minlevel = vt;
for(v=1; v<=vt; v++)
if(map[u][v]>0 && minlevel>level[v])
minlevel = level[v];
gap[level[u]]--;
if(gap[level[u]] == 0) break;
level[u] = minlevel+1;
gap[level[u]]++;
u = pre[u];
}
}
return maxflow;
}
int main()
{
int n,m,u,v,cap;
while(scanf("%d%d",&m,&n)!=EOF)
{
memset(map,0,sizeof(map));
for(int i=1; i<=m; i++)
{
scanf("%d%d%d",&u,&v,&cap);
map[u][v] += cap;
}
printf("%d\n",SAP(1,n));
}
return 0;
}
3.Dinic算法
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
const int maxn = 400;
using namespace std;
struct node
{
int u, v, next, cap;
}edge[maxn*maxn];
int next[maxn], head[maxn], layer[maxn], Q[maxn*2], mark[maxn];
int ecnt;
void init()
{
ecnt= 0;
memset(head,-1,sizeof(head));
}
void addEdge(int u, int v, int c)
{
edge[ecnt].u = u;
edge[ecnt].v = v;
edge[ecnt].cap = c;
edge[ecnt].next = head[u];
head[u] = ecnt++;
edge[ecnt].u = v;
edge[ecnt].v = u;
edge[ecnt].cap = 0;
edge[ecnt].next = head[v];
head[v] = ecnt++;
}
bool BFS(int begin, int end)
{
int i, l, h, k, y;
for (i = 0; i <= end; i++) layer[i] = -1;
layer[begin] = 0;
l = h = 0;
Q[l++] = begin;
while (h < l)
{
k = Q[h++];
for (i = head[k]; i != -1; i = edge[i].next)
{
y = edge[i].v;
if (edge[i].cap > 0 && layer[y] == -1)
{
layer[y] = layer[k] + 1;
if (y == end)
return true;
Q[l++] = y;
}
}
}
return false;
}
int DFS(int x, int exp, int end)
{
mark[x] = 1;
if (x == end)return exp;
int y, temp, i;
for (i = next[x]; i != -1; i = edge[i].next, next[x] = i)
{
y = edge[i].v;
if (edge[i].cap > 0 && layer[y] == layer[x] + 1 && !mark[y])
{
if ((temp = (DFS(y, min(exp, edge[i].cap), end))) > 0)
{
edge[i].cap -= temp;//流完后正向流表示剩余流量
edge[i^1].cap += temp;//流完后反向流表示正向流的流量
return temp;
}
}
}
return 0;
}
int Dinic_flow(int begin, int end)
{
int i, ans = 0, flow;
while (BFS(begin, end))
{
for (i = 0; i <= end; i++)next[i] = head[i];
while (true) {
for (i = 0; i <= end; i++) mark[i] = 0;
flow = DFS(begin, INT_MAX, end);
if (flow == 0)break;
ans += flow;
}
}
return ans;
}
int main()
{
int n,m;
while(scanf("%d%d", &m, &n) !=EOF)
{
init();
for(int i=0; i<m; i++)
{
int a,b,c;
scanf("%d%d%d", &a,&b,&c);
addEdge(a,b,c);
}
printf("%d\n", Dinic_flow(1,n));
}
}