每日算法:Dijkstra的应用

日期:2023.10.17

第六天

题目来源:patA1003

题目:

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

代码长度限制

16 KB

时间限制

400 ms

内存限制

64 MB

题解:

/*
	把每一个点的最短路径数量和累计人数都计算得到,然后输出题目需要的点 
*/


#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int MAXN = 510;//最大顶点数
const int INF = 0x3f3f3f3f;

int N, M, C1, C2;//顶点数,边数,起点编号,终点编号

int G[MAXN][MAXN];//图
int d[MAXN];//每个顶点到起点的最短距离
bool vis[MAXN] = {0};

int weight[MAXN]; //点权,每个城市的救援人数
int w[MAXN];//从起点到顶点的最短路径上的累计救援人数, 如果多条最短路径,输出最大的累计人数 

int  num[MAXN];//顶点i到起点的最短路径的数量 

void Dijkstra(int s) { //起点

	//距离数组初始化 
	memset(d, 0x3f, sizeof(d));
	d[s] = 0;
	
	//路径数量初始化
	memset(num, 0, sizeof(num));
	num[s] = 1;
	
	//起点的最短路径条数初始化 
	memset(w, 0, sizeof(w));
	w[s] = weight[s]; 
	
	for(int i = 0; i < N; i++) {
		//找到非集合S里距离起点最近的点
		int MIN = INF, u = -1;
		for(int j = 0; j < N; j++) {
			if(vis[j] == 0 && d[j] < MIN) {
				MIN = d[j];
				u = j;
			}
		} 
		if(u == -1) return ;
		vis[u] = 1;
		
		for(int j = 0; j < N; j++) {
			if(vis[j] == 0 && G[u][j] != INF) {
				if(d[u] + G[u][j] < d[j]){
					//更新点j的最短路径
					d[j] =  d[u] + G[u][j];
					//到达点j的路径数量,此时等于到达点u的路径数量
					num[j] = num[u]; 
					//更新最短路径的救援人数
					w[j] = w[u] + weight[j]; 
				}
				//具有多条最短路径的话,维护数据都在这里 
				else if(d[u] + G[u][j] == d[j]) {
					//到达j的路径数加上到达u的 
					num[j] += num[u];
					//更新最大累计人数
					if(w[u] + weight[j] > w[j]) {
						w[j] = w[u] + weight[j];
					}
				}
			}
		}
		
	}
	
	 
	 
	
}

int main(void) {
	
	scanf("%d%d%d%d", &N, &M, &C1, &C2);
	
	
	 
	//输入点权
	for(int i = 0; i < N; i++) {
		scanf("%d", &weight[i]);
	} 
	
	//初始化所有边都为无穷大
	memset(G, 0x3f, sizeof(G)); 
	
	//输入边
	for(int i = 0; i < M; i++) {
		int v1, v2, len;
		scanf("%d%d%d", &v1, &v2, &len);
		G[v1][v2] = G[v2][v1] = len;
	} 
	
	Dijkstra(C1);
	
	printf("%d %d", num[C2], w[C2]);
	
	return 0;
}

问题:

1.第一次运行时,数据粘贴不进去,因为scanf()函数没有取地址;

2.dijkstra算法当有多个数组时,要先仔细考虑每个数组的初始化;比如增加了存放点权的数组,就要再开一个数组记录每个点到起点积累的点权;这个积累的点权就要进行初始化;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Dijkstra算法是一种经典的最短路径算法,它可以在有向图中找到从起点到终点的最短路径。下面是一个具体的应用案例和代码实现。 假设有一个有向图,其中每个节点表示一个城市,每条边表示两个城市之间的道路,边的权重表示两个城市之间的距离。现在需要从起点城市出发,到达终点城市,并且要求经过的路径是最短的。 首先,我们需要定义一个图的数据结构,可以使用邻接矩阵或邻接表来表示。这里我们使用邻接矩阵来表示图,其中matrix[i][j]表示从城市i到城市j的距离,如果两个城市之间没有道路,则matrix[i][j]为无穷大。 ``` #define MAXN 100 #define INF x3f3f3f3f int matrix[MAXN][MAXN]; // 邻接矩阵表示图 int dist[MAXN]; // 存储起点到每个节点的最短距离 bool visited[MAXN]; // 标记每个节点是否已经访问过 void dijkstra(int start, int end, int n) { memset(visited, false, sizeof(visited)); memset(dist, INF, sizeof(dist)); dist[start] = ; for (int i = ; i < n; i++) { int u = -1; for (int j = ; j < n; j++) { if (!visited[j] && (u == -1 || dist[j] < dist[u])) { u = j; } } if (u == -1) break; visited[u] = true; for (int v = ; v < n; v++) { if (!visited[v] && matrix[u][v] != INF) { dist[v] = min(dist[v], dist[u] + matrix[u][v]); } } } } ``` 在上面的代码中,我们使用了一个dist数组来存储起点到每个节点的最短距离,visited数组用于标记每个节点是否已经访问过。在每次循环中,我们找到距离起点最近的未访问节点u,并将其标记为已访问。然后,我们遍历所有与u相邻的节点v,并更新它们的最短距离。 最后,我们可以通过dist[end]来获取从起点到终点的最短距离。 注意,上面的代码只是一个简单的示例,实际应用中可能需要对其进行一些修改和优化,例如使用堆来优化查找最近节点的过程。 以上是Dijkstra算法的一个具体应用案例及代码实现。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

前路漫漫亦灿灿上岸

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值