今天看到了华山大师兄的博客中的Floyd算法,自己用java动手写了一个,发现存在一些问题,特此在这里写出来,跟大家分享一下:
java代码:
public static final int UNREACH = Integer.MAX_VALUE;//设置不可到达点权值
public static void main(String[] args) {
// 邻接矩阵
int[][] A = { { 0, 5, UNREACH, 7 }, { UNREACH, 0, 4, 2 },
{ 3, 3, 0, 2 }, { UNREACH, UNREACH, 1, 0 } };
for (int k = 0; k < A.length; k++) {
// 行计算:
for (int i = 0; i < A.length; i++) {
for (int j = 0; j < A.length; j++) {
// 源代码:
// if (A[i][j] > A[i][k] + A[k][j]) {
// 修改以后的代码
if (i != j && i != k && A[i][j] - A[i][k] > A[k][j]) {
A[i][j] = A[i][k] + A[k][j