1030. Travel Plan

题目

https://pintia.cn/problem-sets/994805342720868352/problems/994805464397627392

1030 Travel Plan (30)(30 分)
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.

Input Specification:

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:

City1 City2 Distance Cost

where the numbers are all integers no more than 500, and are separated by a space.

Output Specification:

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.

Sample Input

1
2
3
4
5
6
4 5 0 3
0 1 1 20
1 3 2 30
0 3 4 10
0 2 2 20
2 3 1 20

Sample Output

1
0 2 3 3 40

想法

很经典的一道题

Dijkstra,需要记录路径,distance+cost

记下可以时常温习

答案

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
//
// Created by Zhao Xiaodong on 2018/8/24.
//
#include <iostream>
#include <queue>
#include <vector>
#include <algorithm>
#include <climits>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N, M, start, dst;
cin >> N >> M >> start >> dst;
vector<vector<int>> g(N, vector<int>(N, INT_MAX));
vector<vector<int>> cost(N, vector<int>(N, INT_MAX));
for (int i = 0; i < M; i++) {
int c1, c2, dist, c;
cin >> c1 >> c2 >> dist >> c;
g[c1][c2] = g[c2][c1] = dist;
cost[c1][c2] = cost[c2][c1] = c;
}
vector<bool> done(N, false);
vector<int> prev(N, -1);
vector<int> minDist(N, INT_MAX);
vector<int> minCost(N, INT_MAX);
minDist[start] = 0;
minCost[start] = 0;
while (true) {
int minD = INT_MAX;
int curr = 0;
for (int i = 0; i < N; i++) {
if (!done[i] && minDist[i] < minD) {
minD = minDist[i];
curr = i;
}
}
done[curr] = true;
if (done[dst])
break;
for (int i = 0; i < N; i++) {
if (g[curr][i] == INT_MAX)
continue;
else {
if (g[curr][i] + minDist[curr] < minDist[i]) {
minDist[i] = g[curr][i] + minDist[curr];
prev[i] = curr;
minCost[i] = minCost[curr] + cost[curr][i];
} else if (g[curr][i] + minDist[curr] == minDist[i]) {
if (minCost[curr] + cost[curr][i] < minCost[i]) {
prev[i] = curr;
minCost[i] = minCost[curr] + cost[curr][i];
}
}
}
}
}
vector<int> path;
for (int p = dst; p != start; p = prev[p]) {
path.push_back(p);
}
path.push_back(start);
reverse(path.begin(), path.end());
cout << path[0];
for (int i = 1; i < path.size(); i++) {
cout << " " << path[i];
}
cout << " " << minDist[dst] << " " << minCost[dst];
return 0;
}