以下に記述したコードに Dijkstras Algorithm を書き込もうとしています。しかし、これを開始する方法がわかりません。オンラインの情報源から少し見直しましたが、実際に機能させる方法はまだわかりません. 私はそれを評価パスメソッドに配置することを好みます。次に、メニュー オプションでこのメソッドを呼び出し、並べ替えアルゴリズムを実行します。
ご参考までに。都市 A から都市 B への最短経路をマイルと価格で並べ替えています。
以下は私が持っているコードです。
import java.io.*;
import java.util.*;
public class CityCalcultor {
static LinkedList<String> cities = new LinkedList<String>();
static LinkedList<Integer> distance = new LinkedList<Integer>();
static LinkedList<Integer> price = new LinkedList<Integer>();
public static void main(String[] args)throws IOException
{
Scanner input = new Scanner(System.in);
String text;
int option = 0;
while (true)
{
System.out.println("\nWhat would you like to do:\n" +
"1. Add a city to the system\n" +
"2. Add a path to the system\n" +
"3. Evalute paths\n" +
"4. Exit\n" + "Your option: ");
text = input.nextLine();
option = Integer.parseInt(text);
switch (option)
{
case 1: EnterCity(); break;
case 2: EnterPath(); break;
case 3: EvalutePaths(); break;
case 4: return;
default: System.out.println("ERROR INVALID INPUT");
}
}
}
public static void EnterCity(){
String c = "";
LinkedList<String> cities = new LinkedList<String>(Arrays.asList(c));
Scanner City = new Scanner(System.in);
System.out.println("Please enter the city name ");
c = City.nextLine();
cities.add(c);
System.out.println("City " + c + " has been added ");
}
public static void EnterPath(){
Scanner Path = new Scanner(System.in);
int d = 0; int p = 0;
System.out.println("Enter the starting city ");
System.out.println();
System.out.println(Path.nextLine());
System.out.println("Enter the ending city ");
System.out.println(Path.nextLine());
System.out.println("Enter the distance between the two cities ");
d= Path.nextInt();
distance.add(d);
System.out.println("Enter the price between the two cities ");
p = Path.nextInt();
price.add(p);
System.out.println("The route was sucessfully added ");
}
private static void EvalutePaths(){
}
}
出力は次のようになります::
シアトルからサンフランシスコまでの最短ルートは 1290 マイルです。