import java.util.*; public class 1806 { static int n; static int[] t = new int[10]; static int[] telegraph = new int[50005]; static int[] dis = new int[50005]; static int[] pre = new int[50005]; static boolean[] vis = new boolean[50005]; static ArrayList<Integer> path = new ArrayList<Integer>(); public static void main(String[] args) { Scanner sc = new Scanner(System.in); n = sc.nextInt(); for (int i = 0; i < 10; i++) { t[i] = sc.nextInt(); } for (int i = 1; i <= n; i++) { telegraph[i] = sc.nextInt(); } dijkstra(1); if (dis[n] == Integer.MAX_VALUE) { System.out.println("-1"); } else { System.out.println(dis[n]); getPath(n); System.out.println(path.size()); for (int i = 0; i < path.size(); i++) { System.out.print(path.get(i) + " "); } } } private static void dijkstra(int s) { Arrays.fill(dis, Integer.MAX_VALUE); dis[s] = 0; for (int i = 1; i <= n; i++) { pre[i] = i; } for (int k = 0; k < n; k++) { int u = -1; int minDis = Integer.MAX_VALUE; for (int i = 1; i <= n; i++) { if (!vis[i] && dis[i] < minDis) { u = i; minDis = dis[i]; } } if (u == -1) { break; } vis[u] = true; for (int i = 1; i <= n; i++) { if (!vis[i]) { int w = getWeight(telegraph[u], telegraph[i]); if (dis[u] + w < dis[i]) { dis[i] = dis[u] + w; pre[i] = u; } } } } } private static int getWeight(int a, int b) { int weight = 0; String s1 = String.valueOf(a); String s2 = String.valueOf(b); int len = Math.min(s1.length(), s2.length()); for (int i = 0; i < len; i++) { if (s1.charAt(i) != s2.charAt(i)) { weight = t[i]; break; } } return weight; } private static void getPath(int u) { if (u != 1) { getPath(pre[u]); } path.add(u); } }解释一下该代码的运行过程
时间: 2024-04-27 07:23:17 浏览: 15
这段代码实现了一个基于Dijkstra算法的最短路径问题求解,输入的数据包括电报码的位数n,每个数字所需的时间t[i],以及n个电报码。程序首先调用dijkstra函数,计算从起点1到终点n的最短路径,同时记录每个节点的前驱节点pre[i]和到起点的距离dis[i]。然后判断是否存在从起点1到终点n的路径,如果不存在则输出-1,否则输出最短路径的长度以及路径上的节点。
其中,dijkstra函数的核心是循环n次,每次选择距离起点最近的未访问节点u,并更新与u相邻且未访问的节点i的距离和前驱节点。getWeight函数用于计算两个电报码之间的距离,即两个数字的码位不同的位置的时长之和。getPath函数用于回溯最短路径的前驱节点,得到完整的路径。最后,程序输出路径时采用了ArrayList存储路径节点,便于遍历输出。
相关问题
import java.util.*; import java.util.Collections; public class Test01 { public static void main(String[] args) { Scanner scanner=new Scanner(System.in); List<Integer> list=new ArrayList<>(); for(int i=0;i<list.size();i++) list.add(scanner.nextInt()); Collection.sort(list); } }
在你的代码中,你导入了`java.util.*`和`java.util.Collections`,这是正确的。然而,在调用`Collections.sort(list)`时出现了问题。
问题在于你使用了错误的类名。正确的方法是`Collections.sort(list)`而不是`Collection.sort(list)`。
请将代码中的`Collection.sort(list)`更改为`Collections.sort(list)`,然后重新尝试运行。这样应该就能够正确地对列表进行排序了。
以下是修正后的代码示例:
```java
import java.util.*;
import java.util.Collections;
public class Test01 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
List<Integer> list = new ArrayList<>();
for (int i = 0; i < list.size(); i++)
list.add(scanner.nextInt());
Collections.sort(list);
}
}
```
请注意,你还需要在读取输入值之前确定列表的大小,否则列表将始终为空。可以使用`list.size()`来获取列表的大小,并且在循环之前添加元素。
import java.util.*; public class CenterTree { public static final int N = 5; // 中心树加上最近的四个点一共有5个点 public static void main(String[] args) { // 从控制台读取中心点坐标 Scanner scanner = new Scanner(System.in); System.out.print("请输入中心点
的横坐标:"); int centerX = scanner.nextInt(); System.out.print("请输入中心点的纵坐标:"); int centerY = scanner.nextInt(); // 存储每个点的距离 double[] distances = new double[N]; // 从控制台读取每个点的坐标,并计算它们与中心点的距离 for (int i = 0; i < N; i++) { System.out.print("请输入第" + (i+1) + "个点的横坐标:"); int x = scanner.nextInt(); System.out.print("请输入第" + (i+1) + "个点的纵坐标:"); int y = scanner.nextInt(); distances[i] = Math.sqrt(Math.pow(x-centerX, 2) + Math.pow(y-centerY, 2)); } // 找到距离中心点最近的四个点的下标 int[] nearestIndices = new int[4]; for (int i = 0; i < 4; i++) { int nearestIndex = -1; double nearestDistance = Double.MAX_VALUE; for (int j = 0; j < N; j++) { if (distances[j] < nearestDistance && !isIndexUsed(j, nearestIndices)) { nearestIndex = j; nearestDistance = distances[j]; } } nearestIndices[i] = nearestIndex; } // 输出结果 System.out.print("中心树为中心点(" + centerX + "," + centerY + ")和最近的四个点:"); for (int i = 0; i < 4; i++) { int index = nearestIndices[i]; System.out.print("(" + index + "," + distances[index] + ") "); } } /** * 判断一个下标是否已经被使用 */ private static boolean isIndexUsed(int index, int[] indices) { for (int i = 0; i < indices.length; i++) { if (indices[i] == index) { return true; } } return false; } }
请问这段Java代码的作用是什么?
相关推荐
![text/x-java](https://img-home.csdnimg.cn/images/20210720083646.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)