网络流之最大流算法(Dinic算法)
上一篇博客简单记录了EK算法的思路,这个算法比较简单,原因是他的思路也很暴力,那这次介绍的EK算法就非常的棒,非常的高端,这里其实是借鉴了kuangbin的模板,加上了一些自己的理解
好了,下面开始整活
做一个总结,Dinic就是一个在分层基础上dfs寻找增广路的最大流解决方案
首先来说为甚要进行节点的分层,先说具体操作,我们使用bfs的方式给节点分层
public static boolean BFS(int start, int end, int n) {
//偷偷用linkedlist代替队列应该没人发现吧
LinkedList<Integer> queue = new LinkedList<Integer>();
dep = new int[2010];
for(int i = 0; i <= n; i++) {
dep[i] = -1;
}
dep[start] = 0;
queue.addLast(start);
while(queue.size()!=0) {
int from = queue.removeFirst();
for(int i = head[from]; i != -1; i = map[i].getNext()) {
int to = map[i].getTo();
if(map[i].getVolume()>map[i].getFlow()&&dep[to]==-1) {
dep[to] = dep[from] + 1;
if(to == end) {
return true;
}
queue.addLast(to);
}
}
}
return false;
}
在这个地图中,每个节点旁边的数字,就代表了节点的层次,我们规定,在寻找源点到汇点的路径时,我们只允许从低层次向更高层次出发
拿到这个分层的概念以后,我们就可以开始我们的搜索之路,下面梳理搜索的步骤
- 从起点开始深度优先搜索
- 落脚点必须存在残量而且位于起点的下一层,此时走出这一步,并且以落脚点为起点开始下一步搜索
- 如果遇到死路,跳回到上一步,向另一个方向搜索
- 找到起点以后,记录路径中的最大增广量,并且更新地图
- 最终无法寻找增广量,说明已经达到最大值,输出即可
至此来说,大概就是这个意思
下面是代码
import java.util.LinkedList;
import java.util.Scanner;
public class Dinic {
private static node[] map;
private static int sum, count;
private static int[] head, dep, cur, sta;
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
map = new node[1200010];
head = new int[2010];
cur = new int[2010];
sta = new int[2010];
init(n);
for(int i = 0; i < m; i++) {
int x = sc.nextInt();
int y = sc.nextInt();
int z = sc.nextInt();
addnode(x, y, z);
}
System.out.println(Dinic(1, n, n));
}
public static int Dinic(int start, int end, int n) {
int maxflow = 0;
while(BFS(start, end, n)) {
for(int i = 0; i < n; i++) {
cur[i] = head[i];
}
int u = start;
int tail = 0;
while(cur[start]!=-1) {
if(u == end) {
int tp = Integer.MAX_VALUE;
for(int i = tail-1; i >= 0; i--) {
tp = Math.min(tp, map[sta[i]].getVolume()-map[sta[i]].getFlow());
}
maxflow = maxflow + tp;
for(int i = tail-1; i >= 0; i--) {
map[sta[i]].setFlow(map[sta[i]].getFlow()+tp);
map[sta[i]^1].setFlow(map[sta[i]^1].getFlow()-tp);
if(map[sta[i]].getVolume()-map[sta[i]].getFlow()==0) {
tail = i;
}
}
u = map[sta[tail]^1].getTo();
}else if(cur[u]!=-1&&map[cur[u]].getVolume()>map[cur[u]].getFlow()&&dep[u]+1==dep[map[cur[u]].getTo()]) {
sta[tail++] = cur[u];
u = map[cur[u]].getTo();
}else {
while(u!=start&&cur[u]==-1) {
u=map[sta[--tail]^1].getTo();
}
cur[u]=map[cur[u]].getNext();
}
}
}
return maxflow;
}
public static void init(int n) {
count = 2;
for(int i = 0; i <= n; i++) {
head[i] = -1;
}
}
public static void addnode(int u, int v, int w) {
map[count] = new node(v, head[u], w, 0);
head[u] = count;
count++;
map[count] = new node(u, head[v], 0, 0);
head[v] = count;
count++;
}
public static boolean BFS(int start, int end, int n) {
LinkedList<Integer> queue = new LinkedList<Integer>();
dep = new int[2010];
for(int i = 0; i <= n; i++) {
dep[i] = -1;
}
dep[start] = 0;
queue.addLast(start);
while(queue.size()!=0) {
int from = queue.removeFirst();
for(int i = head[from]; i != -1; i = map[i].getNext()) {
int to = map[i].getTo();
if(map[i].getVolume()>map[i].getFlow()&&dep[to]==-1) {
dep[to] = dep[from] + 1;
if(to == end) {
return true;
}
queue.addLast(to);
}
}
}
return false;
}
}
class node{
private int to;
private int next;
private int volume;
private int flow;
public int getTo() {
return to;
}
public void setTo(int to) {
this.to = to;
}
public int getNext() {
return next;
}
public void setNext(int next) {
this.next = next;
}
public int getVolume() {
return volume;
}
public void setVolume(int volume) {
this.volume = volume;
}
public int getFlow() {
return flow;
}
public void setFlow(int flow) {
this.flow = flow;
}
public node(int to, int next, int volume, int flow) {
super();
this.to = to;
this.next = next;
this.volume = volume;
this.flow = flow;
}
}