根据该博客的算法进行修改:http://sbp810050504.blog.51cto.com/2799422/690803/
不确定是否有错误,如有错误,望指正
public int dijkstra(int[][] W, int start, int end) {
if(start == end){
return 0;
}
// 是否已经标号
boolean[] isLabel = new boolean[W[start].length];
// 所有标号的下标集合
int[] indexs = new int[W[start].length];
// 标记indexs集合最新元素的位置
int i_count = -1;
// 从start到各点的最小值
int[] distance = W[start].clone();
// 对当前最小点标号,初始为起点start
int index = start;
// 把已标号的下标存入下标集合中
indexs[++i_count] = index;
isLabel[index] = true;
while (i_count < distance.length) {
int min = Integer.MAX_VALUE;
for (int i = 0; i < W[start].length; i++) {
if (distance[i] != -1 && !isLabel[i]) {
if (distance[i] < min) {
min = distance[i];
index = i;
}
}
}
if (index == end) {
break;
}
// 将通过遍历找出的到index路径的最小值标号
isLabel[index] = true;
// 设置该下标为已标号
indexs[++i_count] = index;
for (int j = 0; j < W[index].length; j++) {
if (distance[j] == -1 && W[index][j] != -1 ||W[index][j] != -1
&& distance[j] > distance[index] + W[index][j] ) {
distance[j] = distance[index] + W[index][j];
}
}
return distance[end] - distance[start];
}