1003 Emergency

紧急救援路线规划
本文介绍了一个城市应急救援团队领导面临的挑战:如何在接到其他城市的紧急求救电话后,迅速带领团队到达现场并沿途召集尽可能多的救援人员。通过使用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 Specification:

Each input file contains one test case. For each test case, the first line contains 4 positive integers: N(≤500)N (≤500)N(500) - the number of cities (and the cities are numbered from 0toN−10 to N−10toN1), MMM - the number of roads, C​1C​1C1​​ and C​2​​C​2​​C2 - the cities that you are currently in and that you must save, respectively. The next line contains NNN integers, where the iii-th integer is the number of rescue teams in the iii-th city. Then MMM lines follow, each describes a road with three integers c​1​​,c​2​​c​1​​, c​2​​c1,c2 and LLL, 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​1C​1C1​​ to C​2C​2C2​​.

Output Specification:

For each test case, print in one line two numbers: the number of different shortest paths between C​1C​1C1​​ and C​2C​2C2​​, 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算法可解,详见代码


#include <iostream>
using namespace std;
#define INF 100000

int main()
{
	int city[501] = { 0 };	// 节点权重
	int road[501][501];		// 边权重
	bool visit[501] = { 0 };	
	int dist[501] = { 0 };		// 记录最短路径
	int weight[501] = { 0 };	// 记录最大权重和
	int num[501] = { 0 };		// 记录最短路径数量

	int N, M, C1, C2;
	int temp;
	scanf("%d%d%d%d", &N, &M, &C1, &C2);
	for (int i = 0; i < N; i++) {
		scanf("%d", &temp);
		city[i] = temp;
	}
	for (int i = 0; i < N; i++)
		for (int j = 0; j < N; j++)
			road[i][j] = INF;		// 初始化
	int c1, c2, L;
	for (int i = 0; i < M; i++) {
		scanf("%d%d%d", &c1, &c2, &L);
		road[c1][c2] = road[c2][c1] = L;	// 无向图
	}
	
	// 初始化
	for (int i = 0; i < N; i++) 
		dist[i] = road[C1][i];		
	dist[C1] = 0;
	weight[C1] = city[C1];
	num[C1] = 1;
	
	// 核心算法,与寻常Dijkstra算法有所差异 
	for (int i = 0; i < N; i++) {
		int V = -1;
		int MinDist = INF;
		for (int j = 0; j < N; j++) {
			if (visit[j] == false && dist[j] < MinDist) {
				MinDist = dist[j];
				V = j;
			}
		}
		if (V == -1)
			break;
		visit[V] = true;
		for (int W = 0; W < N; W++) {
			if (visit[W] == false && road[V][W] < INF) {
				if (dist[V] + road[V][W] < dist[W]) {
					dist[W] = dist[V] + road[V][W];
					num[W] = num[V];
					weight[W] = weight[V] + city[W];
				}
				else if (dist[V] + road[V][W] == dist[W]) {
					num[W] += num[V];
					if (weight[W] < weight[V] + city[W])
						weight[W] = weight[V] + city[W];
				}
			}
		}
	}
	printf("%d %d", num[C2], weight[C2]);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值