简介
Dijkstra算法是典型的单源最短路径算法,用于计算一个节点到其他所有节点的最短路径。主要特点是以起始点为中心向外层层扩展,直到扩展到终点为止。Dijkstra算法是很有代表性的最短路径算法,在很多专业课程中都作为基本内容有详细的介绍,如数据结构,图论,运筹学等等。注意该算法要求图中不存在负权边。
问题描述:在无向图 G=(V,E) 中,假设每条边 E[i] 的长度为 w[i],找到由顶点 V0 到其余各点的最短路径。(单源最短路径)。
模板
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:
1 2 3 4 5 6 7 8 9 |
5 6 0 2 1 2 1 5 3 0 1 1 0 2 2 0 3 1 1 2 1 2 4 1 3 4 1 |
Sample Output:
1 |
2 4 |
C++代码:
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 |
#include <iostream> #include <algorithm> using namespace std; int n, m, c1, c2; int e[510][510], weight[510], dis[510], num[510], w[510]; //e为边到边的距离,weight每个节点的权重,dis出发点到i的距离, //num表示从出发点到i的最短路径条数,w代表出发点到i的点权之和 bool visit[510]; const int inf = 0x7fffffff; int main(){ cin>>n>>m>>c1>>c2; for(int i = 0; i < n; i++){ cin>>weight[i]; } fill(e[0], e[0] + 510*510, inf); fill(dis, dis + 510, inf); int a, b, c; for(int i = 0; i < m; i++){ cin>>a>>b>>c; e[a][b] = e[b][a] = c; } dis[c1] = 0; w[c1] = weight[c1]; num[c1] = 1; for(int i = 0; i < n; i++){ int u = -1, minn = inf; for(int j = 0; j < n; j++){ if(visit[j] == false && dis[j] < minn){ u = j; minn = dis[j]; } }//找到到出发点最短的距离 if(u == -1)break; visit[u] = true; for(int v = 0; v < n; v++){ if(visit[v] == false && e[u][v] != inf){ if(dis[u] + e[u][v] < dis[v]){ dis[v] = dis[u] + e[u][v]; num[v] = num[u]; w[v] = w[u] + weight[v]; }else if(dis[u] + e[u][v] == dis[v]){ num[v] = num[v] + num[u]; if(w[u] + weight[v] > w[v]) w[v] = w[u] + weight[v]; } } } } cout<<num[c2]<<" "<<w[c2]<<endl; return 0; } |
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:
1 2 |
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 7 |
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 |
C++代码:
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 |
#include<iostream> #include<vector> #define _MAX 510 using namespace std; int e[_MAX][_MAX], dis[_MAX], cost[_MAX][_MAX], discost[_MAX], next2[_MAX]; bool visit[_MAX]; const int inf = 0x7fffffff; int main(){ int n, m, s, d; cin>>n>>m>>s>>d; int a, b, tdis, tcost; fill(e[0], e[0]+_MAX*_MAX, inf); fill(dis, dis+_MAX, inf); for(int i = 0; i < _MAX; i++){ next2[i] = i; } for(int i = 0 ; i < m; i++){ cin>>a>>b>>tdis>>tcost; e[a][b] = e[b][a] = tdis; cost[a][b] = cost[b][a] = tcost; } dis[s] = 0; discost[s] = 0; for(int i = 0; i < n; i++){ int u = -1, minn = inf; for(int j = 0; j < n; j++){ if(!visit[j] && dis[j] < minn){ u = j; minn = dis[j]; } } if(u == -1)break; visit[u] = true; for(int v = 0; v < n; v++){ if(!visit[v] && e[u][v] < inf){ if(dis[u] + e[u][v] < dis[v]){ dis[v] = dis[u] + e[u][v]; discost[v] = discost[u] + cost[u][v]; next2[u] = v; }else if(dis[u] + e[u][v] == dis[v]){ if(discost[u] + cost[u][v] < discost[v]){ discost[v] = discost[u] + cost[u][v]; next2[u] = v; } } } } } int i = s; cout<<s<<" "; while(next2[i] != d){ cout<<next2[i]<<" "; i = next2[i]; } cout<<d<<" "; cout<<dis[d]<<" "<<discost[d]<<endl; return 0; } |