ユーザーにはどのようなパスと距離が求められます。その後、Dijkstraのアルゴリズムを使って最短距離を計算します。問題はある程度の距離であり、パスは間違っています。 2147483647の距離があり、パスはnullです。アルゴはゼロを出発点として完全に機能します。どうすれば修正できますか?
ユーザ入力Dijkstraの最終的な答えの距離として2147483647を出力
Enter first path: 0
Enter second path: 1
Enter distance: 1
Do you want to add path again? y
Enter first path: 1
Enter second path: 3
Enter distance: 2
Do you want to add path again? y
Enter first path: 3
Enter second path: 5
Enter distance: 4
Do you want to add path again? y
Enter first path: 1
Enter second path: 2
Enter distance: 3
Do you want to add path again? y
Enter first path: 0
Enter second path: 2
Enter distance: 8
Do you want to add path again? y
Enter first path: 0
Enter second path: 4
Enter distance: 9
Do you want to add path again? n
V D P
0 2147483647null //this part should be 1 and the path is 1-0
1 0null
2 31 - 2
3 21 - 3
4 2147483647null // this part should be 10 and the path is 0-4
5 63 - 5
もし===================================== ====================================
import java.util.*;
public class DijkstraAlgo {
final static int VERTICES = 6;
public static int minDistance(int distance[], Boolean shortestPath[]){
int minDist = Integer.MAX_VALUE;
int minIndex = -1;
for(int i = 0;i < VERTICES;i++){
if(shortestPath[i] == false && distance[i] <= minDist){
minDist = distance[i];
minIndex = i;
}
}
return minIndex;
}
public static void dijkstra(int path[][], int startingPoint){
int shortestDist[] = new int[VERTICES];
Boolean shortestPath[] = new Boolean[VERTICES];
String paths[] = new String[VERTICES];
for(int i = 0;i < VERTICES;i++){
shortestDist[i] = Integer.MAX_VALUE;
shortestPath[i] = false;
}
shortestDist[startingPoint] = 0;
for(int ctr = 0;ctr < VERTICES - 1;ctr++){
int index = minDistance(shortestDist, shortestPath);
shortestPath[index] = true;
for(int j = 0;j < VERTICES;j++){
if(!shortestPath[j] && path[index][j] != 0 && shortestDist[index] != Integer.MAX_VALUE && shortestDist[index] + path[index][j] < shortestDist[j]){
shortestDist[j] = shortestDist[index] + path[index][j];
paths[j] = Integer.toString(index) + " - " + " " + Integer.toString(j);
System.out.println(shortestDist[j]);
}
}
}
printAnswer(shortestDist, VERTICES, paths);
}
public static void printAnswer(int distance[], int vertices, String paths[]){
System.out.println("V D P");
for(int i = 0; i < VERTICES; i++)
System.out.println(i + " " + distance[i] + paths[i]);
}
public static void main(String args[]){
int start;
int end;
int path[][] = new int[6][6];
int distance;
Scanner input = new Scanner(System.in);
String choose;
boolean ans = true;
while(ans == true){
System.out.print("Enter first path: ");
start = input.nextInt();
System.out.print("Enter second path: ");
end = input.nextInt();
System.out.print("Enter distance: ");
distance = input.nextInt();
path[start][end] = distance;
System.out.print("Do you want to add path again? ");
choose = input.next();
if(choose.equals("y") || choose.equals("Y"))
ans = true;
else if(choose.equals("n") || choose.equals("N"))
ans = false;
else
System.out.println("Invalid input!");
}
dijkstra(path, 1);
}
}
これはデバッガの仕事です。ここではEclipseの手順を説明します。 Eclipse以外のものを使用している場合は、Googleに答えがあります。 http://www.vogella.com/tutorials/EclipseDebugging/article.html – Ironcache