朴素dijkstra算法模板,以PAT 甲级1003 Emergency为例题(求最短路长度、最短路的数目、最短路上的最大权重,用邻接矩阵存储图)
#include<iostream>
#include<cstring>
#include<algorithm>
//#include<bits/stdc++.h> 可用这个代替,它包含了目前C++的所有头文件
using namespace std;
const int N =510; //存储图顶点数的最大取值
int n,m; //存储图的顶点数和边数
int c1,c2; //存储起始顶点和目标顶点
int w[N], stNum[N], mW[N]; //存储图上每个点的权重、最短路的数目、最短路上的最大权重
int g[N][N]; //邻接矩阵存储图
int dist[N]; // 存储c1号点到每个点的最短距离
bool st[N]; // 存储每个点的最短路是否已经确定
// 求c1号点到c2号点的最短路长度、最短路的数目、最短路上的最大权重
void dijkstra() {
//初始化c1
memset(dist, 0x3f, sizeof dist);
dist[c1] = 0;
mW[c1] = w[c1], stNum[c1] = 1;
for (int i = 0; i < n - 1; ++i) {
// 在还未确定最短路的点中,寻找距离最小的点
int t = -1;
for (int j = 0; j <n; ++j) {
if (!st[j] && (t == -1 || dist[t] > dist[j]))
t=j;
}
//t已确定最短路径
st[t] = true;
// 用t更新其他点的距离
for (int j = 0; j < n; ++j) {
if (dist[j] > dist[t] + g[t][j]) {
dist[j] = dist[t] + g[t][j];
stNum[j] = stNum[t];
mW[j] = mW[t] + w[j];
}
else if (dist[j] == dist[t] + g[t][j]) {
stNum[j] += stNum[t];
mW[j] = max(mW[j], mW[t] + w[j]);
}
}
}
}
int main() {
scanf("%d%d%d%d", &n, &m, &c1, &c2);
for (int i = 0; i < n; i++) {
cin >> w[i];
}
memset(g, 0x3f, sizeof g);
while(m--){
int a,b,c;
scanf("%d%d%d", &a, &b, &c);
g[a][b] = min(g[a][b], c); //若有重边则最终保留最短的那条边
g[b][a] = min(g[b][a], c);
}
dijkstra();
printf("%d %d %d",dist[c2], stNum[c2],mW[c2]);
}
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: 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.
满分代码
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 510;
int n, m, c1, c2;
int aidNum[N], maxNum[N], stNum[N]; //城市救援队数量,可召集到的最大救援队数量,最短路径数目
int g[N][N];
int dist[N];
bool st[N];
void dijkstra() {
memset(dist, 0x3f, sizeof dist);
dist[c1] = 0;
maxNum[c1] = aidNum[c1], stNum[c1] = 1;
for (int i = 0; i < n - 1; ++i) {
int t = -1;
for (int j = 0; j <n; ++j) {
if (!st[j] && (t == -1 || dist[t] > dist[j]))
t=j;
}
st[t] = true;
for (int j = 0; j < n; ++j) {
if (dist[j] > dist[t] + g[t][j]) {
dist[j] = dist[t] + g[t][j];
stNum[j] = stNum[t];
maxNum[j] = maxNum[t] + aidNum[j];
}
else if (dist[j] == dist[t] + g[t][j]) {
stNum[j] += stNum[t];
maxNum[j] = max(maxNum[j], maxNum[t] + aidNum[j]);
}
}
}
}
int main() {
scanf("%d%d%d%d", &n, &m, &c1, &c2);
for (int i = 0; i < n; i++) {
cin >> aidNum[i];
}
memset(g, 0x3f, sizeof g);
while(m--){
int a,b,c;
scanf("%d%d%d", &a, &b, &c);
g[a][b] = c;
g[b][a] = c;
}
dijkstra();
printf("%d %d",stNum[c2],maxNum[c2]);
}
码字不易,如果大家觉得有用,请高抬贵手给一个赞让我上推荐让更多的人看到吧~