import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
public class MaxFlow {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(
new FileReader(new File(args[0])));
String n = br.readLine();
int size = Integer.parseInt(n);
int nbrofarc;
ArrayList<Node> list = new ArrayList<Node>();
for (int i = 0; i < size; i++) {
n = br.readLine();
list.add(new Node(i));
}
n = br.readLine();
nbrofarc = Integer.parseInt(n);
for (int i = 0; i < nbrofarc; i++) {
n = br.readLine();
String[] s = n.split(" ");
int node1 = Integer.parseInt(s[0]);
int node2 = Integer.parseInt(s[1]);
int c = Integer.parseInt(s[2]);
c = (c == -1) ? Integer.MAX_VALUE : c;
// System.out.println(c);
Node a = list.get(node1);
Node b = list.get(node2);
Edge first = new Edge(a, b, c);
Edge second = new Edge(b, a, c);
first.residual = second;
second.residual = first;
a.addEdge(first);
b.addEdge(second);
// System.out.println(s[0]+" "+s[1]+" "+s[2]);
}
int dist=bfs(list.get(0),list.get(size-1),list);
dist=bfs(list.get(0),list.get(size-1),list);
bfs(list.get(0),list.get(size-1),list);
//System.out.println(dist);
}
public int augment(int f,ArrayList<Node> p){
return 0;
}
public static int bfs(Node source,Node sink,ArrayList<Node> list) {
if(source.equals(sink)){return 0;}
int dist=-1;
Boolean[] discovered = new Boolean[list.size()];
for(int i=0;i<list.size();i++){
discovered[i]=false;
}
ArrayList<ArrayList<Node>> l = new ArrayList<ArrayList<Node>>();
ArrayList<Node>node=new ArrayList<Node>();
node.add(source);
discovered[source.getid()]=true;
int i=0;
l.add(node);
while(l.get(i).size()!=0){
node=new ArrayList<Node>();
l.add(node);
ArrayList<Node>loop=l.get(i);
for(Node n:loop){
ArrayList<Edge> edge=n.list;
for(Edge e:edge){
if(e.capacity==e.flow||e.residual.flow==e.capacity){
continue;
}
if(!discovered[e.incidentB.getid()]){
//System.out.println(e.capacity+"= "+e.flow);
//System.out.println(e.capacity+"= "+e.residual.flow);
//System.out.println("B");
//System.out.println(e.incidentA.getid()+"-->"+e.incidentB.getid()+" i= "+i);
discovered[e.incidentB.getid()]=true;
e.incidentB.parrent=n;
//tree
node.add(e.incidentB);
if(e.incidentB.equals(sink)){
dist=i+1;
}
}else if(!discovered[e.incidentA.getid()]){
//System.out.println("A");
discovered[e.incidentA.getid()]=true;
e.incidentA.parrent=n;
//tree
node.add(e.incidentA);
if(e.incidentA.equals(sink)){
dist=i+1;
}
}
}
}
i++;
}
boolean found=false;
int k=2;//L[size-1] is empty
ArrayList<Node> level = null;
while(!found){
level=l.get(l.size()-k);
if(level.contains(sink)){
found=true;
}
k++;
}
Node n=level.get(level.indexOf(sink));
int bottleneck=bottleneck(n);
System.out.println("bottleneck= "+bottleneck);
while(n.parrent!=null){
Edge e=n.getEdge(n, n.parrent);
if(e.incidentA.equals(n)){
e.increaseFlow(bottleneck);
}else{
e.residual.increaseFlow(bottleneck);
}
//System.out.println(e.flow);
//System.out.println(e.residual.flow);
n=n.parrent;
}
sink.parrent=null;
return bottleneck;
}
public static int bottleneck(Node n){
int bottleneck=Integer.MAX_VALUE;
while(n.parrent!=null){
//System.out.println(n.parrent.getid()+"--_>"+n.getid());
int cap=n.getEdge(n, n.parrent).capacity;
if(cap<bottleneck){
bottleneck=cap;
}
System.out.println(n.getEdge(n, n.parrent).incidentA.getid()+" -->"+ n.getEdge(n, n.parrent).incidentB.getid());
n=n.parrent;
}
return bottleneck;
}
}
它始终找不到相同的路径 当我运行它时,我得到相同的答案流程,它在接收路径上找到的总是5。
我使用BFS。 我给了我同样的道路,任何人都可以看到问题。
我有一个Node和Edge类,如果需要,我也可以发布它们。
public class Edge {
Node incidentA;
Node incidentB;
Edge residual;
int capacity;
int flow;
public Edge(Node incidentA, Node incidentB, int capacity) {
super();
this.incidentA = incidentA;
this.incidentB = incidentB;
this.capacity = capacity;
this.flow = 0;
}
public int getCapacity() {
return capacity;
}
public Node getIncidentA() {
return incidentA;
}
public Node getIncidentB() {
return incidentB;
}
public int getFlow() {
return flow;
}
public void increaseFlow(int flow) {
this.flow = this.flow + flow;
residual.decreaseFlow(flow);
}
public void decreaseFlow(int flow) {
this.flow = this.flow - flow;
}
}
import java.util.ArrayList;
public class Node {
private int id;
Node parrent=null;
ArrayList<Edge>list;
public Node(int id){
this.id=id;
list=new ArrayList<Edge>();
}
public int getid(){
return id;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + id;
result = prime * result + ((list == null) ? 0 : list.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Node other = (Node) obj;
if (id != other.id)
return false;
if (list == null) {
if (other.list != null)
return false;
} else if (!list.equals(other.list))
return false;
return true;
}
public Edge getEdge(Node a, Node parrent){
for(Edge e:list){
if(a.equals((e.incidentA))||a.equals(e.incidentB)&&(parrent.equals(e.incidentB))||parrent.equals(e.incidentB)){
return e;
}
}
return null;
}
public void addEdge(Edge e) {
if(e.incidentA.equals(this)||e.incidentB.equals(this)){
if(!list.contains(e)){
list.add(e);
}
}
}
}
输入是一个文本文件 4 小号 ü v Ť 五 0 1 20 0 2 10 1 2 30 1 3 10 2 3 20
输入文件的语法: •在第一行,节点数n, •n行包含节点0..n-1的名称。 (这些 名称不是唯一的,因此您不能将它们用作键。) •一行包含弧数m •形式上的m行a b c,其中a和b是节点索引