網絡流之最大流算法(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;
}
}