/// <summary> /// 在剩余网络中,利用BFS寻找增广路径 /// </summary> private bool hasAugmentingPath(FlowNetwork G, int s, int t) { marked = new bool[G.v()]; edgeTo = new FlowEdge[G.v()]; Queue <int> q = new Queue <int>(); marked[s] = true; q.Enqueue(s); while (q.Count > 0) { int v = q.Dequeue(); foreach (var edge in G.adj(v)) { int w = edge.other(v); if (!marked[w] && edge.residualCapacityTo(w) > 0) { edgeTo[w] = edge; marked[w] = true; q.Enqueue(w); } } } return(marked[t]); }
private double value;//最大流量 public FordFulkerson(FlowNetwork G, int s, int t) { while (hasAugmentingPath(G, s, t)) { //Stack<int> stack = new Stack<int>(); double bottle = double.MaxValue; for (int v = t; v != s; v = edgeTo[v].other(v)) { bottle = Math.Min(bottle, edgeTo[v].residualCapacityTo(v));//该条增广路径的最大流量(取决于容量最小的那条边) } for (int v = t; v != s; v = edgeTo[v].other(v)) { edgeTo[v].addReaidualFlowTo(v, bottle);//改变增广路径中的每条边的剩余容量 } //for (int v = t; v != s; v = edgeTo[v].other(v)) // stack.Push(v); value += bottle; //while (stack.Count > 0) //{ // int pop = stack.Pop(); // if (pop != 0 && pop != 13) // Console.WriteLine(pop); //} } }
/// <summary> /// 在剩余网络中,利用BFS寻找增广路径 /// </summary> private bool hasAugmentingPath(FlowNetwork G, int s, int t) { marked = new bool[G.v()]; edgeTo = new FlowEdge[G.v()]; Queue<int> q = new Queue<int>(); marked[s] = true; q.Enqueue(s); while (q.Count > 0) { int v = q.Dequeue(); foreach (var edge in G.adj(v)) { int w = edge.other(v); if (!marked[w] && edge.residualCapacityTo(w) > 0) { edgeTo[w] = edge; marked[w] = true; q.Enqueue(w); } } } return marked[t]; }
private double value; //最大流量 #endregion Fields #region Constructors public FordFulkerson(FlowNetwork G, int s, int t) { while (hasAugmentingPath(G, s, t)) { //Stack<int> stack = new Stack<int>(); double bottle = double.MaxValue; for (int v = t; v != s; v = edgeTo[v].other(v)) bottle = Math.Min(bottle, edgeTo[v].residualCapacityTo(v));//该条增广路径的最大流量(取决于容量最小的那条边) for (int v = t; v != s; v = edgeTo[v].other(v)) edgeTo[v].addReaidualFlowTo(v, bottle);//改变增广路径中的每条边的剩余容量 //for (int v = t; v != s; v = edgeTo[v].other(v)) // stack.Push(v); value += bottle; //while (stack.Count > 0) //{ // int pop = stack.Pop(); // if (pop != 0 && pop != 13) // Console.WriteLine(pop); //} } }