PAT1003 Emergency (25)(Dijkstra算法)

题目

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

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

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

分析题目

题目要求的是最短路径,以及点权值的加权,用Dijkstra算法

学习Dijkstra算法

此处参考了从杰的透彻理解地杰斯特算法
图1
如图1,

步骤集合S集合U
1选入A
S={A(0)}
以A为中间点开始找
U = {B(6), C(3), D(∞), E(∞), F(∞)
2选入C
S = {A(0), C(3)}
以A->C开始找,若为最短路径则更新U
U = {B(5), D(6), E(7), F(∞)}
3选入B
S = {A(0), C(3), B(5)} 以A-B开始找,若为最短路径则更新U
U = {D(6), E(7), F(∞)}
4S = {A(0), C(3), B(5), D(6)}U = {E(7), F(9)}
5S = {A(0), C(3), B(5), D(6), E(7)}U = F(9)
6S = {A(0), C(3), B(5), D(6), E(7), F(9)}U 集合为空

按最短路径长度的递增次序依次把第二组的顶点加入S中。在加入的过程中,总保持从源点v到S中各顶点的最短路径长度不大于从源点v到U中任何顶点的最短路径长度

代码

#include <iostream>
#include <algorithm>
using namespace std;
int n, m, c1, c2;
int e[510][510], weight[510], dis[510], num[510], w[510];
bool visit[510];
const int inf = 99999999;
int main() {
    scanf("%d%d%d%d", &n, &m, &c1, &c2);
    for(int i = 0; i < n; i++)
        scanf("%d", &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++) {
        scanf("%d%d%d", &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];
                }
            }
        }
    }
    printf("%d %d", num[c2], w[c2]);
    return 0;
}

参考代码:日吹柳神

总结

  1. 使用fill的时候,注意是fill一维矩阵还是二维矩阵
int dis[500];
int E[500][500];
fill(dis,dis+500,inf); //对一维矩阵
fill(E[0],E[0]+500*500,inf);//对二维矩阵
  1. 注意fill和memset区别
    其中fill能用的范围更广,而memset一般用于初始化char数组、string,如果用于初始化int数组的话只能填入0或1
    具体用法:fill和memset区别
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Dijkstra算法是一种用于计算从源节点到其他所有节点的最短路径的图论算法。它是一种贪心算法,通过不断地选择最短路径节点并标记为已访问,从而不断缩小搜索范围来实现最短路径的求解。Dijkstra算法的时间复杂度为O(n^2)或O(n log n),具体取决于实现方式。 ### 回答2: Dijkstra算法,也称为迪杰斯特拉算法,是一种用于解决单源最短路径问题的经典算法。该算法通过计算从起点到所有其他节点的最短路径,并在过程中逐步找到最短路径。它以荷兰计算机科学家狄克斯特拉(Edsger W. Dijkstra)命名。 Dijkstra算法的基本思想是从起点开始,逐步通过其他节点来确定最短路径。算法使用两个集合:一个集合记录已确定最短路径的节点,另一个集合记录尚未确定最短路径的节点。算法首先初始化起点的最短路径为0,然后选择与起点相连的节点中距离最近的节点,更新起点到这个节点的最短路径长度。接着,再选择与上一个节点相连的未确定最短路径的节点中距离最短的节点,更新最短路径长度。这个过程会一直持续到所有节点都被确定最短路径。 Dijkstra算法通过使用优先队列来选择距离起点最近的节点,从而提高了效率。通过不断计算和更新节点的最短路径长度,算法最终可以得到从起点到其他所有节点的最短路径。 Dijkstra算法在许多应用中被广泛使用,比如路由器中的路由选择、地图导航系统以及网络优化等。它是一种非常有用和高效的算法,能够快速计算出最短路径并帮助解决一些实际问题。 ### 回答3: Dijkstra 算法,也被称为迪杰斯特拉算法,是一种用于解决单源最短路径问题的图算法。它的目标是从一个源点到图中所有其他点的最短路径。 算法的基本思想是采用贪心策略,通过逐步确定源点到其他各点的最短路径。 具体操作步骤如下: 1. 创建一个距离数组dist[],用于存储源点到达每个顶点的最短路径长度。初始时,源点到自身的距离为0,其余顶点到源点的距离为无穷大。 2. 创建一个集合sptSet[],用于记录已经找到最短路径的顶点集合。初始时,该集合为空。 3. 重复下述步骤直到sptSet[]包含所有顶点: a. 从dist[]数组中选择一个最小的值,它对应的顶点不在sptSet[]中,并加入sptSet[]中。 b. 更新dist[]数组,即如果通过新加入的顶点到达其他顶点的路径更短,则更新其距离。 4. 最终,dist[]数组中存储的就是源点到各个顶点的最短路径长度。 Dijkstra 算法的复杂度为O(V^2),其中V为图的顶点数。该算法适用于没有负权边的图。当图中存在负权边时,需要使用其他算法,如Bellman-Ford算法。 在实际应用中,Dijkstra 算法常被用于网络路由问题,优化交通运输中的最短路径等。通过Dijkstra算法,我们可以找到从一个点到其他所有点的最短路径,帮助我们在网路中选择最优路径,提高效率和性能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值