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() {
}
}
In the code below I am trying to calculate the distance between two cities. The user will enter a city name and a city, then the user will enter the distance between those cities, and finally enter the price to travel between those cities. I am not getting it to evaluate yet as I am unsure exactly how I am going to do this. My question is I am looking for pointers and suggestions on how one would do this.
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() {
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我可以通过两种方式解释你的问题:你要么想评估城市对之间的每条可能的路径(从技术上讲,这是无限的,除非你限制重新访问城市),要么你想找到最短的距离/最便宜的方式城市到城市。
我不想试图弄清楚第一个,而且我很确定你指的是第二个,所以我就这么做。
该问题的解决方案是使用图表对城市及其之间的路线进行建模,其中每个顶点都是一个城市,每条边是两个城市之间的直接路径。边缘根据城市之间的距离或旅行成本进行加权。
然后,您可以应用 Dijkstra 算法(或其变体之一)来查找任意路径之间的路径源顶点(出发城市)到所有其他顶点(目的地城市)的总权重最小(即最小距离或最低成本)。如果您依次使用每个顶点作为源来应用此算法,您将构建任意两个城市之间最便宜路线的完整模型。
There are two ways I can interpret your question: you either want to evaluate every possible path between pairs of cities (which is technically infinite, unless you restrict revisiting of cities), or you want to find the shortest distance/cheapest way to get from city to city.
I don't feel like trying to figure out the first one, and I'm pretty sure you mean the second, so I'm going with that.
The solution to the problem is to model your cities and the routes between them using a graph, where each vertex is a city, and each edge is a direct path between two cities. The edges are weighted either by the distance between the cities, or by the cost of travel.
You can then apply Dijkstra's Algorithm (or one of its variants) to find the path between any source vertex (city of departure) to all other vertices (destination cities) with the smallest total weight (i.e. smallest distance or lowest cost). If you apply this algorithm using each vertex in turn as the source, you will have built a complete model of the cheapest routes between any two cities.
计算最短路径的几乎标准方法是 A*
The pretty much standard way of calculating shortest paths is A*