图的 Dijkstra 算法,在算法基础上增加最大点权和以及最短路径条数的记录。
As an emergency rescue team leader of a city, you are given a special map of your country. The map shows several scattered cities connected by some roads. Amount of rescue teams in each city and the length of each road between any pair of cities are marked on the map. When there is an emergency call to you from some other city, your job is to lead your men to the place as quickly as possible, and at the mean time, call up as many hands on the way as possible.
Problem: PAT-A 1003 Emergency
Input Specification
Each input file contains one test case. For each test case, the first line contains 4 positive integers: N (≤ 500) - the number of cities (and the cities are numbered from 0 to N − 1), M - the number of roads, $C_1$ and $C_2$ - the cities that you are currently in and that you must save, respectively. The next line contains N integers, where the i-th integer is the number of rescue teams in the i-th city. Then M lines follow, each describes a road with three integers $c_1$, $c_2$ and L, which are the pair of cities connected by a road and the length of that road, respectively. It is guaranteed that there exists at least one path from $C_1$ to $C_2$.
Output Specification
For each test case, print in one line two numbers: the number of different shortest paths between $C_1$ and $C_2$, and the maximum amount of rescue teams you can possibly gather. All the numbers in a line must be separated by exactly one space, and there is no extra space allowed at the end of a line.
Sample Input
1 | 5 6 0 2 |
Sample Output
1 | 2 4 |
Analysis
单源最短路径问题,用 Dijkstra 算法。根据题意,每个城市的救援队数量为点权,城市之间的距离为边权。由于题目要求输出结果为最短路径数和最大点权和,添加 num 数组记录起点至各顶点的最短路径数,添加 w 数组记录起点至各个顶点的最大点权和,在计算 d 的同时更新上述两个数组,详情见代码注释。
Code
1 |
|
Tsukkomi
Dijkstra 基本快忘了,思想是明白的,但写出来有点费劲。之前用到的时候也是直接比着模板写的,但为了准备 PAT 考试,就硬学呗。代码参考了胡凡的《算法笔记》,书上正好有这道题的讲解,很是详细。