每日算法: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算法当有多个数组时,要先仔细考虑每个数组的初始化;比如增加了存放点权的数组,就要再开一个数组记录每个点到起点积累的点权;这个积累的点权就要进行初始化;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

前路漫漫亦灿灿上岸

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

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

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

打赏作者

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

抵扣说明:

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

余额充值