簡體   English   中英

Java的最短路徑和距離算法?

[英]Shortest path and distance algorithm for 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() {
    }
}

計算最短路徑的標准方法是A*

我可以通過兩種方式來解釋您的問題:您要么想評估成對城市之間的每條可能路徑(這在技術上是無限的,除非您限制重新訪問城市),或者您想找到最短距離/最便宜的出行方式城市到城市。

我不想試圖弄清楚第一個,我很確定你的意思是第二個,所以我會這樣做。

問題的解決方案是 model 您的城市以及它們之間的路線使用圖,其中每個頂點是一個城市,每個邊是兩個城市之間的直接路徑。 邊緣根據城市之間的距離或旅行成本加權。

然后,您可以應用Dijkstra 算法(或其變體之一)以找到任何源頂點(出發城市)到所有其他頂點(目的地城市)之間總權重最小(即最小距離或最低成本)的路徑。 如果您依次使用每個頂點作為源應用此算法,您將構建任意兩個城市之間最便宜的路線的完整 model。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM