Ford-Fulkerson算法 java实现

来源:互联网 发布:电子相册软件免费 编辑:程序博客网 时间:2024/05/29 14:28

/*
for each edge(u,v) 属于 G,E
(u,v).f = 0;
while there exists a path p from s to t in the residual network Gf
cf(p) = min{cf(u,v):(u,v) is in p};
for each edge(u,v) in p
if(u,v)属于E
(u,v).f += cf(p)
else
(u,v).f -= cf(p);
*/

用数组实现栈来进行深度遍历的地方(dfs函数)需要尤其注意。

import java.util.*;class Ford_Fulkerson{static int[][] res;//残留矩阵static int[] pre;static boolean[] used;static int[] stack; //用于dfs,底部为0,顶部为topstatic int ver;static int edge;static int ford_Fulkerson(int s,int t){//res默认初始化为0int cfp = Integer.MAX_VALUE;int maxflow = 0;while(dfs(s,t)){int d = Integer.MAX_VALUE;for(int i = t; i != s; i = pre[i])  d = Math.min(d, res[pre[i]][i]); for(int i=t; i != s; i = pre[i])  {  res[pre[i]][i] -= d;  res[i][pre[i]] += d;  }maxflow += d;}return maxflow;}static boolean dfs(int s ,int t){for(int i=0;i<=ver;i++) used[i] = false;used[s] = true;int top = 0;stack[0] = s; top++;int cur = s,i = 1;int[] rec = new int[ver+5]; //记录第i个节点遍历到的位置boolean flag = false;//采用非递归的形式,找到一条增广路径后立刻终止while(top != 0){ //stack 不为空flag = false;cur = stack[top-1]; //相当于peek操作for(i=rec[cur]+1;i <= ver;i++){//取与其相邻的并且没有访问过的值大于0的点入栈if(used[i]|| res[cur][i] <= 0) continue;flag = true;pre[i] = cur;used[i] = true;if(i == t) return true; //找到目标节点,返回truestack[top++] = i;//入栈rec[cur] = i;break;}////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////if(!flag) {used[stack[--top]] = false;}//回溯过程恢复设置,并pop}return false;}static void init(){res = new int[ver+5][ver+5];used = new boolean[ver+5];pre = new int[ver+5];stack = new int[ver+5];}static void input(){Scanner cin = new Scanner(System.in);System.out.println("请输入 点数 边数");ver = cin.nextInt();edge = cin.nextInt();int s,e,w;init();System.out.println("起点 终点 权值");for(int i=0;i<edge;i++){s = cin.nextInt();e = cin.nextInt();w = cin.nextInt();res[s][e] = w;}}public static void main(String[] args){input();System.out.println(ford_Fulkerson(1,4));}}


1 0
原创粉丝点击