在下面的代碼我試圖計算兩個城市之間的距離。用戶將進入一個城市名和一個城市,那麼用戶將進入距離這些城市之間,終於進入這些城市之間旅行的價格。我沒有得到它評估,因爲我不確定我將如何做到這一點。我的問題是我正在尋找關於如何做這件事的指針和建議。Java的最短路徑和距離算法?
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();
for (d = 0; d > 0; d++) {
distance.add(d);
}
System.out.println("Enter the price between the two cities ");
p = Path.nextInt();
for (p = 0; p > 0; p++) {
price.add(p);
}
System.out.println("The route was sucessfully added ");
}
private static void EvalutePaths() {
}
}
這就引出了一個問題:您是否在尋找一個解決旅行推銷員(如@trashgod建議)?這個問題全部是關於尋找一次訪問每個城市的最短旅程。或者,你是否正在尋找任何兩個城市之間的最短路線(一個更容易的問題)?請驗證您的術語*評估*的含義。 –
又見[旅行推銷員問題](http://en.wikipedia.org/wiki/Travelling_salesman_problem)。 – trashgod
那麼...這只是查找任何兩個城市之間的最短路線。 – allencoded