PAT 1030 Travel Plan (30分) Dijstra +Dfs
2021-02-16 10:19
标签:iostream must 起点 out uniq one tput clear path 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: City1 City2 Distance Cost Output Specification: Sample Input: 给出由n个节点和m条边组成的无向图,给出m条边的距离和代价,给定起点和终点,要求输出从起点到终点的最短路径、最短距离、以及最小代价。 这个题,本质上和 PAT 1018 Public Bike Management 是一样的,无非就是 注释写的比较详细,思路说的太多反而容易迷糊,建议多看看 PAT 1030 Travel Plan (30分) Dijstra +Dfs 标签:iostream must 起点 out uniq one tput clear path 原文地址:https://www.cnblogs.com/codervivi/p/12975145.html题目
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:
where the numbers are all integers no more than 500, and are separated by a space.
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.
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题目解读
在Dijstra
求最短距离的过程中保存最短路径上的前驱,再跟据DFS结合前驱得到全部最短路径,在此过程中计算每一条最短路径的代价,选出最小的那条。思路分析
Dijstra
,但题目说了,可能会有多条最短路径,我们要找到全部路径怎么办?把每一个路径都保存下来吗?可以,但没必要。我们只需要记录最短路径上每个节点的直接前驱就可以了,而这个,在Dijstra
算法中,只需要在更新过程中(d[v] = d[u] + e[u][v]
)加上一条语句就可以了(prev[v].push_back(u)
),非常简单。如果它有多个最短路径,那么它就有多个直接前驱,因此,每个节点都应该有一个vector
来保存它的全部最短路径的直接前驱,最终,就是需要一个vector
数组。
dfs
就出场了,因为我们保存的是节点的全部直接前驱,所以需要从终点往起点去寻找。每一次从终点找到起点的路径都是一条最短路径,这其实就是一个DFS
,计算这条路径的代价,如果更小,则更新最小代价并选择这条路径。Dijstra
找到全部最短路径,dfs
遍历每个最短路径并找到代价最小的那个。满分代码
dfs
,这个还是很常用的。#include
文章标题:PAT 1030 Travel Plan (30分) Dijstra +Dfs
文章链接:http://soscw.com/index.php/essay/56050.html