1030 Travel Plan(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:
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:
0 2 3 3 40
PS:这是本人第一次自己快速采用Dijkstra+DFS完成这样的题。并且一次AC成功,在此小小的开心一下~~
解题思路: 这题其实很简单,按照题目要求输入数据,其中cost采用二维数组存储,和edge一样。
接着用Dijkstra来找到每个点距离起点的最小距离,并更新disp,并且用pre记录每个结点的前一个结点.(注意每一次有最短距离出现时,都要先清空pre,再添加上前一结点。当出现有相同最短距离时,直接将前一结点加入对应pre).
最后用DFS来找到最小花费的路径.注意每次DFS调用过程后都跟着一个pop_back,作用是能正确记录一条完整的path。
当找到起点时再比较出最小mincost,并记录最小路径(且花费最小)res = path.
代码实现如下:
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
const int N = 501;
const int inf = 0x7fffffff;
int edge[501][501], cost[501][501],disp[501];
bool vst[501];
vector<int> pre[501];
vector<int> path, res;
int mincost = inf;
void dfs(int i) {
path.push_back(i);
if (pre[i].size() == 0) {
int sum = 0;
for (int j = 0; j < path.size() - 1; j++) {
int a = path[j];
int b = path[j + 1];
sum += cost[a][b];
}
if (mincost > sum) {
mincost = sum;
res = path;
}
}
for (int j = 0; j < pre[i].size(); j++) {
dfs(pre[i][j]);
path.pop_back();
}
}
int main() {
fill(edge[0], edge[0] + N*N, inf);
fill(disp, disp + N, inf);
fill(cost[0], cost[0] + N*N, inf);
fill(vst, vst + N, false);
int n, m, s, d;
cin >> n >> m >> s >> d;
for (int i = 0; i<m; i++) {
int a, b, c, d;
cin >> a >> b >> c >> d;
edge[a][b] = edge[b][a] = c;
cost[a][b] = cost[b][a] = d;
}
disp[s] = 0;
for (int i = 0; i<n; i++) {
int u = -1, min = inf;
for (int j = 0; j<n; j++) {
if (disp[j] < min && vst[j] == false)
{
min = disp[j];
u = j;
}
}
if (u == -1)
break;
vst[u] = true;
for (int v = 0; v<n; v++) {
if(edge[u][v] != inf){//这句一定要保证
if (disp[v] > disp[u] + edge[u][v] && vst[v] == false) {
disp[v] = disp[u] + edge[u][v];
pre[v].clear();//若找到最新最短路径,先清空
pre[v].push_back(u);
}
else if (disp[v] == disp[u] + edge[u][v] && vst[v] == false) {
disp[v] = disp[u] + edge[u][v];
pre[v].push_back(u);
}
}
}
}
dfs(d);
for (int i = res.size() - 1; i>=0; i--)
printf("%d ", res[i]);
printf("%d %d", disp[d], mincost);
return 0;
}