|
| 1 | +#### [1030 Travel Plan](https://pintia.cn/problem-sets/994805342720868352/exam/problems/994805464397627392) |
| 2 | + |
| 3 | +A traveler's map gives the distances between cities along the highways, together with the cost of each highway. Now you are supposed to write a program to help a traveler to decide the shortest path between his/her starting city and the destination. If such a shortest path is not unique, you are supposed to output the one with the minimum cost, which is guaranteed to be unique. |
| 4 | + |
| 5 | +### Input Specification: |
| 6 | + |
| 7 | +Each input file contains one test case. Each case starts with a line containing 4 positive integers *N*, *M*, *S*, and *D*, where *N* (≤500) is the number of cities (and hence the cities are numbered from 0 to *N*−1); *M* is the number of highways; *S* and *D* are the starting and the destination cities, respectively. Then *M* lines follow, each provides the information of a highway, in the format: |
| 8 | + |
| 9 | +``` |
| 10 | +City1 City2 Distance Cost |
| 11 | +``` |
| 12 | + |
| 13 | +where the numbers are all integers no more than 500, and are separated by a space. |
| 14 | + |
| 15 | +### Output Specification: |
| 16 | + |
| 17 | +For each test case, print in one line the cities along the shortest path from the starting point to the destination, followed by the total distance and the total cost of the path. The numbers must be separated by a space and there must be no extra space at the end of output. |
| 18 | + |
| 19 | +### Sample Input: |
| 20 | + |
| 21 | +```in |
| 22 | +4 5 0 3 |
| 23 | +0 1 1 20 |
| 24 | +1 3 2 30 |
| 25 | +0 3 4 10 |
| 26 | +0 2 2 20 |
| 27 | +2 3 1 20 |
| 28 | +``` |
| 29 | + |
| 30 | +### Sample Output: |
| 31 | + |
| 32 | +```out |
| 33 | +0 2 3 3 40 |
| 34 | +``` |
| 35 | + |
| 36 | +--- |
| 37 | + |
| 38 | +Dijkstra算法:边有两种代价的情况,另外再使用一个数组记录最小代价的路径,`a[2] = 3` 意为最短路径是由节点 `3` 指向节点 `2` 的,采用方式是因为在求最小代价时**一个点可以有多条出边,但是只能有一条入边**。得到最小代价后深度优先搜索即可。 |
| 39 | + |
| 40 | +```java |
| 41 | +import java.util.*; |
| 42 | +public class Main{ |
| 43 | + private static final int INF = Integer.MAX_VALUE / 2; |
| 44 | + private static int[] dis; |
| 45 | + private static int[][] g; |
| 46 | + private static int[][] cost; |
| 47 | + private static int[] sum_cost; |
| 48 | + private static int[] a; |
| 49 | + private static boolean[] visited; |
| 50 | + private static ArrayList<Integer> arraylist = new ArrayList<>(); |
| 51 | + |
| 52 | + public static void dfs(int d, int s){ |
| 53 | + if(d == s){ |
| 54 | + arraylist.add(s); |
| 55 | + return; |
| 56 | + } |
| 57 | + dfs(a[d], s); |
| 58 | + arraylist.add(d); |
| 59 | + } |
| 60 | + |
| 61 | + |
| 62 | + public static void main(String[] args){ |
| 63 | + Scanner scanner = new Scanner(System.in); |
| 64 | + int N = scanner.nextInt(), M = scanner.nextInt(), S = scanner.nextInt(), D = scanner.nextInt(); |
| 65 | + dis = new int[N]; |
| 66 | + g = new int[N][N]; |
| 67 | + cost = new int[N][N]; |
| 68 | + sum_cost = new int[N]; |
| 69 | + visited = new boolean[N]; |
| 70 | + a = new int[N]; |
| 71 | + for(int i = 0; i < N; i++){ |
| 72 | + Arrays.fill(g[i], INF); |
| 73 | + Arrays.fill(cost[i], INF); |
| 74 | + } |
| 75 | + Arrays.fill(dis, INF); |
| 76 | + Arrays.fill(sum_cost, INF); |
| 77 | + |
| 78 | + for(int i = 0; i < M; i++){ |
| 79 | + int temp1 = scanner.nextInt(), temp2 = scanner.nextInt(), temp3 = scanner.nextInt(), temp4 = scanner.nextInt(); |
| 80 | + g[temp1][temp2] = temp3; |
| 81 | + g[temp2][temp1] = temp3; |
| 82 | + cost[temp1][temp2] = temp4; |
| 83 | + cost[temp2][temp1] = temp4; |
| 84 | + } |
| 85 | + |
| 86 | + dis[S] = 0; |
| 87 | + sum_cost[S] = 0; |
| 88 | + for(int i = 0; i < N; i++){ |
| 89 | + int cur = -1, min = INF; |
| 90 | + for(int j = 0; j < N; j++){ |
| 91 | + if(!visited[j] && dis[j] < min){ |
| 92 | + min = dis[j]; |
| 93 | + cur = j; |
| 94 | + } |
| 95 | + } |
| 96 | + if(cur == -1) return; |
| 97 | + visited[cur] = true; |
| 98 | + for(int j = 0; j < N; j++){ |
| 99 | + if(visited[j] == false && g[cur][j] != INF){ |
| 100 | + if(g[cur][j] + dis[cur] < dis[j]){ |
| 101 | + dis[j] = dis[cur] + g[cur][j]; |
| 102 | + sum_cost[j] = cost[cur][j] + sum_cost[cur]; |
| 103 | + a[j] = cur; |
| 104 | + } |
| 105 | + else if(g[cur][j] + dis[cur] == dis[j] && cost[cur][j] + sum_cost[cur] < sum_cost[j]){ |
| 106 | + sum_cost[j] = cost[cur][j] + sum_cost[cur]; |
| 107 | + a[j] = cur; |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + } |
| 113 | + dfs(D, S); |
| 114 | + for(int i = 0; i < arraylist.size(); i++) System.out.print(arraylist.get(i) + " "); |
| 115 | + System.out.println(dis[D] + " " + sum_cost[D]); |
| 116 | + |
| 117 | + } |
| 118 | +} |
| 119 | +``` |
| 120 | + |
0 commit comments