Dijkstra算法:伪代码
1 //G为图,一般设为全局变量,数组d[u]为原点到达个点的额最短路径, s为起点 2 Dijkstra(G, d[u], s){ 3 初始化; 4 for (循环n次){ 5 u = 是d[u]最小的且还未访问的顶点的标号; 6 记u已经被访问; 7 for (从u出发能到达的所有顶点v){ 8 if (v未被访问&&以u为终结点使s到顶点v的最短距离d[u]更优){ 9 优化d[v]; 10 } 11 } 12 }
邻接矩阵版Dijkstra
1 //邻接矩阵模板 2 const int MAXV = 100; 3 const int INF = 1000000; 4 5 int n, G[MAXV][MAXV]; //n 为顶点数,MAXV为最大顶点数 6 int d[MAXV]; //起点到达各点的最短路径长度 7 bool vis[MAXV] = { false }; //标记数组,标记顶点i是否已经被访问 8 9 void Dijkstra(int s){ 10 11 //初始化 12 fill(d, d + MAXV, INF); 13 d[s] = 0; //起点到达自身的距离为0 14 for (int i = 0; i < n; i++){ //循环n次,每次从未访问的顶点中取出一点,然后更新他的理解点距离起点的距离 15 int u = -1, MIN = INF; 16 for (int j = 0; j < n; j++){ //找到未访问的顶点中d[]最小的 17 if (vis[j] == false && d[j] < MIN){ 18 u = j; 19 MIN = d[j]; 20 } 21 } 22 23 //找不到小于INF的d[u], 说明剩下的顶点和起点不连通 24 if (u == -1) return ; 25 vis[u] = true; //标记为已访问 26 for (int v = 0; v < n; v++){ 27 //如果v为访问&&u可到达v且经u的中转可以是v到s距离更短,更新d[v] 28 if (vis[v] == false && G[u][v] != INF && d[u] + G[u][v] < d[v]){ 29 d[v] = d[u] + G[u][v]; 30 } 31 } 32 33 } 34 35 }
邻接表版:
1 const int MAXV = 100; 2 const int INF = 100000; 3 int n; 4 5 int d[MAXV]; 6 bool vis[MAXV] = { false }; 7 8 struct Node{ 9 int v, dis; //v为边的目标顶点,dis为边权 10 }; 11 12 vector<Node> Adj[MAXV]; //存储图的邻接表 13 14 void Dijktra(int s){ 15 //初始化vis和d[s] 16 fill(d, d + MAXV, INF); 17 d[s] = 0; 18 //循环n次每次找出还未访问的且距离起点最近的顶点,取出并访问,更新与它相邻的顶点到起点的距离 19 for (int i = 0; i < n; i++){ 20 int u = -1, MIN = INF; 21 for (int j = 0; j < n; j++){ 22 if (vis[j] == false && d[j] < MIN){ 23 u = j; 24 MIN = d[j]; 25 } 26 } 27 28 //如果找不到小于inf的d[u],说明剩下的顶点和起点s不连通 29 if (u == -1) return ; 30 vis[u] = true; 31 for (int v = 0; v < Adj[u].size(); v++){ 32 int ver = Adj[u][v].v; 33 if (vis[ver] == false && d[u] + Adj[u][v].dis < d[ver]){ 34 d[ver] = d[u] + Adj[u][v].dis; 35 } 36 } 37 38 } 39 }
邻接表版 + 第二标尺(边权,点权,最短路径数)
1 const int MAXV = 100; 2 const int INF = 1000000; 3 4 int n, G[MAXV][MAXV]; //n 为顶点数,MAXV为最大顶点数 5 int d[MAXV]; //起点到达各点的最短路径长度 6 bool vis[MAXV] = { false }; //标记数组,标记顶点i是否已经被访问 7 8 int pre[MAXV]; //存储每个结点的最短路径上的前驱结点 9 10 int cost[MAXV][MAXV] = {0}; //边权花费 11 int c[MAXV] = { 0 }; 12 int w[MAXV]; //每个顶点的物资 13 int weight[MAXV]; //从起点到当前顶点所收集的总物资 14 int num[MAXV]; //从起点到达每个顶点的路径数量 15 16 void Dijkstra(int s){ 17 18 //初始化 19 fill(d, d + MAXV, INF); 20 d[s] = 0; //起点到达自身的距离为0 21 for (int i = 0; i < n; i++){ //循环n次,每次从未访问的顶点中取出一点,然后更新他的理解点距离起点的距离 22 int u = -1, MIN = INF; 23 for (int j = 0; j < n; j++){ //找到未访问的顶点中d[]最小的 24 if (vis[j] == false && d[j] < MIN){ 25 u = j; 26 MIN = d[j]; 27 } 28 } 29 30 //找不到小于INF的d[u], 说明剩下的顶点和起点不连通 31 if (u == -1) return; 32 vis[u] = true; //标记为已访问 33 for (int v = 0; v < n; v++){ 34 //如果v为访问&&u可到达v且经u的中转可以是v到s距离更短,更新d[v] 35 if (vis[v] == false && G[u][v] != INF){ 36 if (d[u] + G[u][v] < d[v]){ 37 d[v] = d[u] + G[u][v]; 38 pre[v] = u; //将u设置成v的前驱结点 39 c[v] = c[u] + cost[u][v]; 40 w[v] = w[u] + weight[v]; 41 num[v] = num[u]; 42 } 43 /*else if (d[u] + G[u][v] == d[v] && c[u] + cost[u][v] < c[v]){ 44 c[v] = c[u] + cost[u][v]; 45 }*/ 46 /* else if (d[u] + G[u][v] == d[v] && w[u] + weight[v] > w[v]){ 47 w[v] = w[u] + weight[v]; 48 }*/ 49 else if (d[u] + G[u][v] == d[v]){ 50 num[v] += num[u]; 51 } 52 } 53 } 54 55 } 56 57 }
Dijkstra + DFS
DIjkstra: 求出所有最短路径和每个顶点到起点的最短距离
1 const int INF = 100000; 2 const int MAXV = 510; 3 4 bool vis[MAXV] = { false }; 5 6 int G[MAXV][MAXV]; 7 int V[MAXV][MAXV];//边权 8 int W[MAXV]; //点权 9 int num = 0;//最短路径条数 10 int d[MAXV]; //d[u]数组 11 int n, m, s, des; //顶点数 12 13 vector<int> pre[MAXV]; //存储每个顶点在最短路径上的前驱结点 14 vector<int> tempPath; //临时路径 15 vector<int> path; //最优路径 16 int optValue = INF; //最优值 17 18 //求出所有最短路径和每个顶点到起点的最短距离 19 void Dijkstra(int s){ 20 21 //初始化d[], d[s]; 22 fill(d, d + MAXV, INF); 23 d[s] = 0; 24 pre[s].push_back(s); 25 26 for (int i = 0; i < n; i++){ 27 //找出还未访问且距离起点最近的顶点 28 int u = -1, MIN = INF; 29 for (int j = 0; j < n; j++){ 30 if (vis[j] == false && d[j] < MIN){ 31 u = j; 32 MIN = d[j]; 33 } 34 } 35 36 //如果没有这样的顶点说明剩下的顶点不与起点相连 37 if (u == -1) return; 38 vis[u] = true; 39 40 //遍历u的邻接点,更新的d[u]和pre[] 41 for (int v = 0; v < n; v++){ 42 if (vis[v] == false){ 43 if (d[u] + G[u][v] < d[v]){ 44 d[v] = d[u] + G[u][v]; 45 pre[v].clear(); //将原来的前驱结点都删除 46 pre[v].push_back(u); 47 } 48 else if (d[u] + G[u][v] == d[v]){ 49 pre[v].push_back(u); 50 } 51 } 52 } 53 } 54 }
DFS: 遍历每条最短路径,求出第二标尺下的最优路径
1 //遍历每条最短路径,求出第二标尺下的最优路径 2 void dfs(int v){ 3 4 //如果v是叶子结点,计算当前路径的第二标尺值,如果更优则更新 5 if (v == s){ 6 tempPath.push_back(v); 7 8 //如果边权最为第二标尺 9 int value = 0; 10 for (int i = tempPath.size() - 1; i > 0; i--){ 11 value += V[tempPath[i]][tempPath[i - 1]]; 12 } 13 14 //如果更优,替换最优值 15 if (optValue > value){ 16 optValue = value; 17 path = tempPath; 18 } 19 20 ////如果点权位第二标尺 21 //int value2 = 0; 22 //for (int i = 0; i < tempPath.size(); i++){ 23 // value2 += W[i]; 24 //} 25 //if (optValue < value2){ 26 // optValue = value2; 27 //} 28 29 //如果是路径数量 30 num++; 31 32 tempPath.pop_back(); 33 } 34 else { 35 tempPath.push_back(v); 36 37 for (int i = 0; i < pre[v].size(); i++){ 38 dfs(pre[v][i]); 39 } 40 41 tempPath.pop_back(); //清除当前路径,方便下条路径的添加 42 } 43 44 }
题型实战:
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: N (≤500) - the number of cities (and the cities are numbered from 0 to N−1), M - the number of roads, C1 and C2 - 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 c1, c2 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 C1 to C2.
Output Specification:
For each test case, print in one line two numbers: the number of different shortest paths between C1 and C2, 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:
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:
2 4
题目大意要求:求给定起点和终点,求两点之间的最短路径的数量,如果存在多条最短路径,取路径上所有点的点权之和最大的那条路径,输出点权之和
代码:
1 #include <stdio.h> 2 #include <algorithm> 3 #include <string.h> 4 using namespace std; 5 6 const int maxv = 510; 7 const int INF = 100000000; 8 int G[maxv][maxv] = { INF }; 9 int n, m, c1, c2; // n 为顶点数,m为边数,c1为起点,c2为终点 10 int d[maxv]; // 起点到各点的距离 11 int num[maxv] = { 0 }; // 起点到达个点的最短路径条数 12 int weight[maxv]; // 每个点的点权 13 int w[maxv]; // 每个点的最距离起点所能积累的最大点权之和 14 bool vis[maxv] = { false }; // 是否被访问过 15 16 void Dijkstra(int s){ 17 // 初始化d[], w[]数组 18 fill(d, d + maxv, INF); 19 d[s] = 0; 20 memset(w, 0, sizeof(w)); 21 w[s] = weight[s]; 22 num[s] = 1; 23 for (int i = 0; i < n; i++){ 24 int u = -1, min = INF; 25 // 寻找一个最小的且未访问过的d[u] 26 for (int j = 0; j < n; j++){ 27 if (vis[j] == false && min > d[j]){ 28 min = d[j]; 29 u = j; 30 } 31 } 32 if (u == -1) return; 33 vis[u] = true; 34 for (int v = 0; v < n; v++){ 35 if (G[u][v] != INF && vis[v] == false){ 36 if (G[u][v] + d[u] < d[v]){ 37 d[v] = G[u][v] + d[u]; 38 num[v] = num[u]; 39 w[v] = w[u] + weight[v]; 40 } 41 else if (G[u][v] + d[u] == d[v]){ 42 num[v] += num[u]; 43 if (w[u] + weight[v] > w[v]){ 44 w[v] = w[u] + weight[v]; 45 } 46 } 47 } 48 } 49 } 50 } 51 54 int main() 55 { 56 // freopen("in.txt", "r", stdin); 57 scanf("%d %d %d %d", &n, &m, &c1, &c2); 58 for (int i = 0; i < n; i++){ 59 scanf("%d", &weight[i]); 60 } 61 62 int u, v, dis; 63 fill(G[0], G[0] + maxv * maxv, INF); 64 for (int i = 0; i < m; i++){ 65 scanf("%d %d %d", &u, &v, &dis); 66 G[u][v] = G[v][u] = dis; 67 } 68 69 Dijkstra(c1); 70 71 printf("%d %d", num[c2], w[c2]); 72 73 // fclose(stdin); 74 return 0; 75 }