任何人都可以帮助java中的bellman ford算法来计算源顶点的最短路径.
我还希望在遍历所有边缘之后为每个节点打印最终更新的前任节点,并且在所有迭代之后这是我的代码
import java.io.*;
import java.util.*;
public class BellmanFord {
LinkedList<Edge> edges;
int d[];
int n,e,s;
final int INFINITY=999;
private static class Edge {
int u,v,w;
public Edge(int a, int b, int c) {
u=a;
v=b;
w=c;
}
}
BellmanFord() throws IOException {
int item;
edges = new LinkedList<Edge>();
BufferedReader inp = new BufferedReader (new InputStreamReader(System.in));
System.out.print("Enter number of vertices ");
n = Integer.parseInt(inp.readLine());
System.out.println("Cost Matrix");
for(int i=0;i<n;i++) {
for(int j=0;j<n;j++) {
item = Integer.parseInt(inp.readLine());
if(item != 0)
edges.add(new …Run Code Online (Sandbox Code Playgroud) java ×1