Dinic 算法模板
Dinic算法是一种比较容易实现的,相对比较快的最大流算法。
求最大流的本质,就是不停的寻找增广路径。直到找不到增广路径为止。
对于这个一般性的过程,Dinic算法的优化如下:
(1)Dinic算法首先对图进行一次BFS,然后在BFS生成的层次图中进行多次DFS。
层次图的意思就是,只有在BFS树中深度相差1的节点才是连接的。
这就切断了原有的图中的许多不必要的连接。很牛逼!
这是需要证明的,估计证明也很复杂。
(2)除此之外,每次DFS完后,会找到路径中容量最小的一条边。
在这条边之前的路径的容量是大于等于这条边的容量的。
那么从这条边之前的点,可能引发出别的增广路径。
比如说 S -> b -> c -> d -> T 是一条增广路径,容量最小的边是 b -> c。
可能存在一条 S -> b -> e -> f -> g -> T 这样的增广路径。
这样的话,在找到第一条增广路径后,只需要回溯到 b 点,就可以继续找下去了。
这样做的好处是,避免了找到一条路径就从头开始寻找另外一条的开销。
也就是再次从 S 寻找到 b 的开销。
这个过程看似复杂,但是代码实现起来很优雅,因为它的本质就是回溯!
(3)在同一次 DFS 中。如果从一个点引发不出任何的增广路径,就将这个点在层次图中抹去。
poj1273 链接:http://poj.org/problem?id=1273
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
#include"stdio.h" #include"string.h" #define N 605 #define min(a,b) (a<b?a:b) const int inf=0x7fffffff; struct node { int u,v,w,next; }map[N*4]; int cnt,n,m,s,t,head[N],q[N],dis[N]; void add(int u,int v,int w) { map[cnt].u=u; map[cnt].v=v; map[cnt].w=w; map[cnt].next=head[u]; head[u]=cnt++; map[cnt].u=v; map[cnt].v=u; map[cnt].w=0; map[cnt].next=head[v]; head[v]=cnt++; } int bfs() { int i,x,v,t1,t2; memset(dis,0,sizeof(dis)); //节点的高度标号 dis[s]=1; t1=t2=0; q[t2++]=s; //模拟队列 while(t1<t2) { x=q[t1++]; for(i=head[x];i!=-1;i=map[i].next) { v=map[i].v; if(map[i].w&&!dis[v]) { dis[v]=dis[x]+1; if(v==n) return 1; q[t2++]=v; } } } return 0; } int dfs(int s,int lim) { int i,v,tmp,cost=0; if(s==t) return lim; for(i=head[s];i!=-1;i=map[i].next) //枚举该点连通的所有边 { v=map[i].v; if(map[i].w&&dis[s]==dis[v]-1) { tmp=dfs(v,min(lim-cost,map[i].w)); if(tmp>0) { map[i].w-=tmp; //利用反向边的奇偶性,增加反向边的流量 map[i^1].w+=tmp; cost+=tmp; if(lim==cost) break; } else //在同一次 DFS 中。如果从一个点引发不出任何的增广路径,就将这个点在层次图中抹去。 dis[v]=-1; } } return cost; } int dinic() { int ans=0; while(bfs()) ans+=dfs(s,inf); return ans; } int main() { int u,v,w; while(~scanf("%d%d",&m,&n)) { cnt=0; memset(head,-1,sizeof(head)); while(m--) { scanf("%d%d%d",&u,&v,&w); add(u,v,w); //建边,反向边流量为零 } s=1;t=n; printf("%d\n",dinic()); } return 0; }
dinic算法学习(以poj1273为例)