有了前面理论的基础,贴代码吧!
- package com.xh.Dinic;
- import java.util.Arrays;
- import java.util.LinkedList;
- import java.util.Queue;
- import java.util.Stack;
- public class Dinic01 {
- private int[][] capacity;// 容量
- private int[][] flow;// 流量
- private boolean[] visit;// 是否访问
- private int nodes;// 点的个数
- private int[] layers;// 层数
- private int[] pre;
- static int[][] map={ {0,5,4,3,0,0,0,0},//三条边
- {0,0,0,0,5,3,0,0},//两条边
- {0,0,0,0,0,3,2,0},//两条边
- {0,0,0,0,0,0,2,0},//一条边
- {0,0,0,0,0,0,0,4},//一条边
- {0,0,0,0,0,0,0,3},//一条边
- {0,0,0,0,0,0,0,5},//一条边
- {0,0,0,0,0,0,0,0}, };
- public Dinic01(int[][] capacity, int nodes) {
- this.nodes = nodes;
- this.capacity = capacity;
- this.flow = new int[nodes][nodes];
- this.visit = new boolean[nodes];
- this.layers = new int[nodes];
- this.pre = new int[nodes];// 记录路径
- }
- public boolean layerNetwork(int src, int des) {
- Queue<Integer> queue = new LinkedList<Integer>();
- queue.add(src);
- visit[src] = true;
- layers[src] = 0;
- int node = 0;
- while (!queue.isEmpty()) {
- node = queue.poll();
- for (int i = 0; i < nodes; i++) {
- if (!visit[i] && capacity[node][i] - flow[node][i] > 0) {
- queue.add(i);
- visit[i] = true;
- layers[i] = layers[node] + 1;
- }
- }
- }
- if (node == des){
- return true;
- }
- return false;
- }
- public boolean DFS(int src, int des) {
- Stack<Integer> stack = new Stack<Integer>();
- stack.push(src);
- stack.push(src);
- visit[src] = true;
- int node=src;
- while (!stack.isEmpty()) {
- int i;
- for ( i= 0; i < nodes; i++) {
- if (!visit[i]&&(layers[i]==layers[node]+1)
- && capacity[node][i] - flow[node][i] > 0) {
- stack.push(i);
- visit[i]=true;
- pre[i]=node;
- break;
- }
- }
- if(i>=nodes){//没有点满足情况
- node=stack.pop();
- }else{
- node=stack.peek();
- }
- if(node==des){
- return true;
- }
- }
- return visit[des];
- }
- public int processAugmentingPaths(int src,int des){
- int maxflow=0;
- while (true) {
- Arrays.fill(visit, false);
- pre[src] = -1;
- if (!DFS(src,des)) {// the BFS
- break;
- }
- int increment=Integer.MAX_VALUE;
- for (int i = des; pre[i] >= src; i = pre[i]) {
- // find the min flow of the path
- increment = Math.min(increment, capacity[pre[i]][i]
- - flow[pre[i]][i]);
- }
- for (int i = des; pre[i] >= src; i = pre[i]) {
- // find the min flow of the path
- flow[pre[i]][i]+=increment;
- flow[i][pre[i]]-=increment;
- }
- maxflow+=increment;
- }
- return maxflow;
- }
- public int DinicAlgorithm(int src,int des){
- int maxflow=0;
- while (layerNetwork(src, des)) {
- maxflow+=processAugmentingPaths(src, des);
- Arrays.fill(visit, false);
- Arrays.fill(layers, 0);
- }
- return maxflow;
- }
- public static void main(String[] args) {
- int[][] capacity = new int[map[0].length][map[0].length];
- int nodes = map[0].length;
- for (int i = 0; i < map[0].length; i++) {
- for (int j = 0; j < map[0].length; j++) {
- if (map[i][j] != 0) {
- capacity[i][j] = map[i][j];
- }
- }
- }
- Dinic01 dinic01 = new Dinic01(capacity, nodes);
- System.out.println(dinic01.DinicAlgorithm(0, nodes-1));
- // System.out.println(dinic01.layerNetwork(0, nodes - 1));
- }
- }
尽管代码写出来了,但是我感觉其实依然无法摆脱2F算法的影子。而只是外面披了一层这个算法的皮而已,而其本质好像并没有变化多少……先不去理会那么多了!学习最小费用最大流吧!