朴素dijkstra算法模板,以PAT 甲级1003 Emergency为例题(求最短路长度、最短路的数目、最短路上的最大权重,用邻接矩阵存储图)

文章提供了一个使用C++实现的朴素Dijkstra算法模板,以解决PAT甲级题目1003Emergency中的最短路径、最短路数目和最短路上最大权重问题。代码涉及邻接矩阵存储图、动态规划优化以及搜索策略,旨在快速找到从起点到终点的最优救援路径并计算可召集的最大救援团队数量。
摘要由CSDN通过智能技术生成

朴素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, 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 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 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.

满分代码

#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]);
}

码字不易,如果大家觉得有用,请高抬贵手给一个赞让我上推荐让更多的人看到吧~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值