(一题覆盖所有dijkstra可能的出题组合!?)1003 Emergency (25 分)

作为紧急救援队队长,你需在接到求救电话后,利用特殊地图快速规划至目标城市的最短路径,同时沿途集结最多救援队伍。输入包括城市数量、道路数、起点和终点城市,以及各城市救援队数量和道路长度,输出则为最短路径数量和可集结的最大救援队数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 c​1​​, c​2​​ 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 C​1​​ to C​2​​.

每个输入文件都包含一个测试用例。对于每个测试用例,第一行包含4个正整数:N城市数量(≤500)(城市编号为0到 N−1),M道路数,您当前所在的城市C​1​​和您必须营救的城市C​2。下一行包含N整数,其中I整数是I城市中救援队的数量。然后M行,每行三个整数c​1​​,c​2​​和L组成的道路,这三个整数分别是由一条道路连接的两个城市和这条道路的长度。保证至少存在一条从C​1​到C​2​​的路径。

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.

对于每个测试用例,请在一行中打印两个数字:C​1​​和C​2​​之间的不同最短路径数,以及您可能收集到的最大救援队数量。
一行中的所有数字必须完全用一个空格分隔,并且一行末尾不允许有多余的空格。

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

 

#include<iostream>
#include<algorithm>
using namespace std;
const int MAXV = 510;//最大顶点
const int inf = 0xffff;//定义无穷大
int n, m, st, ed, g[MAXV][MAXV], weight[MAXV];
//n为顶点数,m为边数,st为起点,ed为终点,g为邻接矩阵,weight为点权;
int d[MAXV], w[MAXV], num[MAXV];
//d[]记录最短距离,w[]记录最大点权之和,num[]记录最短路径条数
bool vis[MAXV] = { false };//如果为true说明已经访问过,初值为未访问
void dijkstra(int s)//s为起点
{
	fill(d, d + MAXV, inf);//将最短距离全部初始化为无穷;
	d[s] = 0;//起点到自身的距离为0;
	w[s] = weight[s];//起点自身的权值
	num[s] = 1;//最短路径条数最小为1
	for (int i = 0; i < n; i++)
	{//循环n次
		int u = -1, min = inf;//找离初始位置最近的点
		for (int j = 0; j < n; j++)
		{//遍历顶点
			if (vis[j] == false && d[j] < min)
			{//找到未访问的最小顶点并确定最小点的位置
				u = j;
				min = d[j];
			}
		}
		if (u == -1)return;
		vis[u] = true;//该点进入已知最短路径行列中
		for (int v = 0; v < n; v++)
		{
			if (vis[v] == false && g[u][v] != inf)//最短距离点的出边
			{//如果v未被访问且u能到达v且以u为中介点能使d[v](起点到v的距离)更优
				if (d[u] + g[u][v] < d[v])
				{
					d[v] = d[u] + g[u][v];//松弛d[v]
					w[v] = w[u] + weight[v];//覆盖w[v]
					num[v] = num[u];//覆盖num[v]
				}
				else if (d[u] + g[u][v] == d[v])
				{//找到相同长度的路径
					if (w[u] + weight[v] > w[v])
						w[v] = w[u] + weight[v];//以u为中介点时点权更大
					num[v] += num[u];//最短路径条数和点权无关,在外面
				}
			}
		}
	}
}
int main()
{
	cin >> n >> m >> st >> ed;
	for (int i = 0; i < n; i++)
		cin >> weight[i];//各点的点权
	int u, v;
	fill(g[0], g[0] + MAXV * MAXV, inf);//二维邻接矩阵初始化
	for (int i = 0; i < m; i++)
	{//读入边权
		cin >> u >> v;
		cin >> g[u][v];
		g[v][u] = g[u][v];//无向互通
	}
	dijkstra(st);//入口
	cout << num[ed] << " " << w[ed] << endl;//最短距离条数,最短路径中的最大点权
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值