}
}
for(int i=2;i<=n;i++){
System.out.println(i+"节点的最短距离是:"+dist[i]+";前驱点是:"+p[i]);
}
}
public static void main(String[] args) {
System.out.println("请输入图顶点的个数:");
Scanner sc = new Scanner(System.in);
String line = sc.nextLine();
int n = Integer.parseInt(line);
System.out.println("请输入图的路径长度:");
float[][] a = new float[n+1][n+1];//下标从 1 开始,以下都是
float[] dist = new float[n+1];
int[] prev = new int[n+1];
for(int i=0;i
a[i+1][j+1]=Float.parseFloat(ds[j]);
}
}
int v =1;//顶点从 1 开始
shortest(a,v,dist,prev);
}
}
/**
* 以下为输入输出
*
* 输入:
5
-1,10,-1,30,100
-1,-1,50,-1,-1
-1,-1,-1,-1,10
-1,-1,20,-1,60
-1,-1,-1,-1,-1
* 输出:
7
2 节点的最短距离是:10.0;前驱点是:1
3 节点的最短距离是:50.0;前驱点是:4
4 节点的最短距离是:30.0;前驱点是:1
5 节点的最短距离是:60.0;前驱点是:3
*/
实验结果:
8