我正在尝试将 Dijkstras 算法写入我在下面编写的代码中。但我不确定如何开始这样做。我确实从在线资源中对其进行了一些审查,但我仍然不确定如何让它真正发挥作用。我更喜欢将其放入评估路径方法中。然后让菜单选项调用此方法并执行排序算法。
供参考。我正在按里程和价格对从城市 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 英里。