【问题标题】:Shortest path and distance algorithm for Java?Java的最短路径和距离算法?
【发布时间】:2011-06-28 10:27:08
【问题描述】:

在下面的代码中,我试图计算两个城市之间的距离。用户输入城市名称和城市,然后用户输入这些城市之间的距离,最后输入这些城市之间的旅行价格。我还没有对其进行评估,因为我不确定我将如何做到这一点。我的问题是我正在寻找有关如何做到这一点的指针和建议。

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 建议的那样)?这个问题就是要找到最短的游览,它只访问每个城市一次。或者您是否正在寻找任何两个城市之间的最短路线(一个更容易的问题)?请验证evaluate一词的含义。
  • 不...这只是为了查找任何两个城市之间的最短路线。

标签: java shortest-path


【解决方案1】:

我可以通过两种方式来解释您的问题:您要么想要评估成对城市之间的每条可能路径(从技术上讲,这是无限的,除非您限制重新访问城市),或者您想要找到最短距离/最便宜的方式从一个城市到另一个城市。

我不想弄清楚第一个,我很确定你是说第二个,所以我会继续。

该问题的解决方案是使用图对您的城市及其之间的路线进行建模,其中每个顶点是一个城市,每条边是两个城市之间的直接路径。边按城市之间的距离或旅行成本加权。

然后您可以应用Dijkstra's Algorithm(或其变体之一)来查找任何源顶点(出发城市)到具有最小总权重(即最小距离或最低成本)的所有其他顶点(目的地城市)之间的路径)。如果您依次使用每个顶点作为源来应用此算法,您将构建任意两个城市之间最便宜路线的完整模型。

【讨论】:

  • 是的,绝对是第二个! Dijkstra 算法是最好的。关于如何将其实施到我所拥有的任何想法?
【解决方案2】:

计算最短路径的标准方法是A*

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2023-03-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-10
  • 1970-01-01
相关资源
最近更新 更多