Dijkstra算法实例选讲1

简介

Dijkstra算法是典型的单源最短路径算法,用于计算一个节点到其他所有节点的最短路径。主要特点是以起始点为中心向外层层扩展,直到扩展到终点为止。Dijkstra算法是很有代表性的最短路径算法,在很多专业课程中都作为基本内容有详细的介绍,如数据结构,图论,运筹学等等。注意该算法要求图中不存在负权边。
问题描述:在无向图 G=(V,E) 中,假设每条边 E[i] 的长度为 w[i],找到由顶点 V0 到其余各点的最短路径。(单源最短路径)。

模板

Dijkstra算法邻接矩阵C++及多种变换

PAT 1003 Emergency (25 分)

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.

Input Specification:

Each input file contains one test case. For each test case, the first line contains 4 positive integers:  () - the number of cities (and the cities are numbered from 0 to ),  - the number of roads,  and  - the cities that you are currently in and that you must save, respectively. The next line contains  integers, where the -th integer is the number of rescue teams in the -th city. Then  lines follow, each describes a road with three integers  and , 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  to .

Output Specification:

For each test case, print in one line two numbers: the number of different shortest paths between  and , 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:

Sample Output:

C++代码:

PAT 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 , and , where  () is the number of cities (and hence the cities are numbered from 0 to );  is the number of highways;  and  are the starting and the destination cities, respectively. Then  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.

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:

Sample Output:

C++代码:

 

留下评论

您的邮箱地址不会被公开。 必填项已用 * 标注