所以我们的教授要求我们使用邻接矩阵来实现Dijkstra的实现。
在多次出界之后,我有点不知道该怎么办。我知道Dijkstra可以用2D阵列完成,但我真的遇到了问题。
我可以在这里找到出界的异常,但是我有什么根本性的错误吗?这是代码。请注意,输入是古怪的(教授有一个奇怪的标准输入),但为了您的理智,输入:
3
2
7
1 2 50
3 1 20
3 2 60
5 1 40
3 4 30
4 2 20
0 4 8
如果你想测试它。这将创建一个6x6数组,将3部分输入中给出的点与结束数字的权重链接在一起。
import java.util.Scanner;
import java.util.StringTokenizer;
public class cmsc401 {
public static void main(String[] args) {
Scanner read = new Scanner(System.in);
int intersections = read.nextInt();
int friends = read.nextInt();
int links = read.nextInt();
int breakpoint = friends + 1;
int total = friends + intersections + 1;
int[][] matrix = new int[total][total];
Scanner in = new Scanner(System.in);
String next = "";
int from = 0;
int to = 0;
int distance = 0;
for(int x = 0; x < matrix.length; x++){
for(int y = 0; y < matrix[x].length; y++){
//matrix[x][y] = Integer.MAX_VALUE;
}
}
for(int x = 0; x < links; x++){
next = in.nextLine();
StringTokenizer strToken = new StringTokenizer(next);
from = Integer.parseInt((String)strToken.nextElement());
to = Integer.parseInt((String)strToken.nextElement());
distance = Integer.parseInt((String)strToken.nextElement());
matrix[from][to] = distance;
matrix[to][from] = distance;
}
for(int x = 1; x < breakpoint; x++){
System.out.println(shortestRoute(x,0,matrix));
}
}
public static boolean contains(int[] B, int a){
for(int x = 0; x< B.length; x++){
if (B[x] == a){
return true;
}
}
return false;
}
public static int findShortest(int start, int[][] A, int[] visited, int[][] distances){
int potential = Integer.MAX_VALUE;
int potentialAddress = -1;
for(int x = 0; x < A[start].length; x++){
if(A[start][x] != 0 && contains(visited,x) == false){
if(distances[x][1] < potential){
potential = A[start][x];
potentialAddress = x;
}
}
}
return potentialAddress;
}
public static int DistanceTo(int a, int b, int[][]A, int[][] distances){
int potential = distances[a][1] + A[a][b];
if(potential < distances[b][1]){
return potential;
}else{
return distances[b][1];
}
}
public static int backUp(int[] visited, int[][] distances){
int potential = Integer.MAX_VALUE;
int potentialAddress = -1;
for(int x = 0; x < distances.length; x++){
if(distances[x][1] < potential && contains(visited,x) == false){
potentialAddress = x;
potential = distances[x][1];
}
}
return potentialAddress;
}
public static int shortestRoute(int start, int finish, int[][] A){
int[] visited = new int[A.length];
int numVisited = 0;
for(int x = 0; x < visited.length; x++){
visited[x] = -1;
}
int[][] distances = new int[A.length][2];
for(int x = 0; x < distances.length; x++){
distances[x][0] = x;
distances[x][1] = Integer.MAX_VALUE;
}
int current = start;
int next = 0;
while(numVisited < visited.length){
if(numVisited == visited.length - 1){
visited[numVisited] = current;
continue;
}
for(int x = 0; x < A[current].length; x++){
int position = A[current][x];
if (position != 0 && contains(visited,x) == false){
distances[x][1] = DistanceTo(current,x,A,distances);
}
}
if(findShortest(current,A,visited,distances) == -1){
if(backUp(visited,distances) == -1){
break;
}else{
backUp(visited,distances);
}
}
visited[numVisited] = current;
numVisited++;
next = findShortest(current,A,visited,distances);
current = next;
}
return distances[finish][1];
}
}