A1003 Emergency (25 分)

本文介绍了一种使用Dijkstra算法优化救援队伍调度的问题解决方案。在面对紧急情况时,如何快速且有效地调动救援资源至关重要。文章提供了两种方法:一是通过增强Dijkstra算法一次遍历来寻找最优路径并计算最大救援力量;二是结合Dijkstra算法与深度优先搜索(DFS)来探索所有可能的最短路径,并确定能集结最多救援队伍的路径。

摘要生成于 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​​.

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.

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

方法一:

 在节点上附加了第二把尺度,一般用DFS做,但考虑到题目只需要求最大的救援人数,所以直接一遍Dijkstra解决即可,增加的点条件初始值即为该点救援人数的数量,另外需要求方案数量,只需定义一个nums数组存放即可,初始条件为1即可。

#include <bits/stdc++.h>
#define INF 1000000000
#define maxn 505
using namespace std;


int G[maxn][maxn], dis[maxn], nums[maxn], gather[maxn];
bool vis[maxn]; 
int n, m, c1, c2;
int rst[maxn];

void dijkstra(int s) {
	fill(dis, dis+maxn, INF);
	fill(nums, nums+maxn, 0);
	fill(gather, gather+maxn, 0);
	fill(vis, vis+maxn, false);
	gather[s] = rst[s];
	dis[s] = 0;
	nums[s] = 1;
	for (int i = 0; i < n; i++) {
		int u = -1, min = INF;
		for (int j = 0; j < n; j++) {
			if (!vis[j] && dis[j] < min) {
				min = dis[j];
				u = j;
			}
		}
		if (u == -1) {
			return;
		}
		vis[u] = true;
		for (int v = 0; v < n; v++) {
			if (!vis[v] && G[u][v] != INF) {
				if (dis[u] + G[u][v] < dis[v]) {
					dis[v] = dis[u] + G[u][v];
					gather[v] = gather[u] + rst[v];
					nums[v] = nums[u];
				} else if (dis[u] + G[u][v] == dis[v]) {
					nums[v] += nums[u];
					if (gather[u] + rst[v] > gather[v]) {
						gather[v] = gather[u] + rst[v];
					}
				}
			}
		}
	}
}

int main() {
	fill(G[0], G[0]+maxn*maxn, INF);
	int u, v, dis;
	cin >> n >> m >> c1 >> c2;
	for (int i = 0; i < n; i++) {
		cin >> rst[i];
	}
	for (int i = 0; i < m; i++) {
		cin >> u >> v >> dis;
		G[u][v] = G[v][u] = dis;
	}
	dijkstra(c1);
	cout << nums[c2] << " " << gather[c2];
	return 0;
} 

方法二:

利用Dijkstra+DFS,用DFS遍历每条路径,然后计算路径上包含救援队最多的path即可。

#include <bits/stdc++.h>
#define INF 1000000000
#define maxn 505
using namespace std;

vector<int> pre[maxn], path, tempPath;
int n, m, c1, c2;

int G[maxn][maxn], dis[maxn], rst[maxn], nums[maxn];
bool vis[maxn];

void dijkstra(int s) {
	fill(dis, dis+maxn, INF);
	fill(vis, vis+maxn, false);
	fill(nums, nums+maxn, 0);
	dis[s] = 0;
	nums[s] = 1;
	for (int i = 0; i < n; i++) {
		int u = -1, min = INF;
		for (int j = 0; j < n; j++) {
			if (!vis[j] && dis[j] < min) {
				min = dis[j];
				u = j;
			} 
		} 
		if (u == -1) {
			return;
		}
		vis[u] = true;
		for (int v = 0; v < n; v++) {
			if (!vis[v] && G[u][v] != INF) {
				if (dis[u] + G[u][v] < dis[v]) {
					dis[v] = dis[u] + G[u][v];
					pre[v].clear();
					pre[v].push_back(u);
					nums[v] = nums[u];
				} else if (dis[u] + G[u][v] == dis[v]) {
					nums[v] += nums[u];
					pre[v].push_back(u);
				}
			}
		}
	}
	
}


int max_rst = -1;

void dfs(int u) {
	if (u == c1) {
		tempPath.push_back(u);
		int temp_rst = 0;
		for (int i = 0; i < tempPath.size(); i++) {
			int v = tempPath[i];
			temp_rst += rst[v];
		}
		if (temp_rst > max_rst) {
			max_rst = temp_rst;
		}
		tempPath.pop_back();
		return;
	}
	tempPath.push_back(u);
	for (int i = 0; i < pre[u].size(); i++) {
		dfs(pre[u][i]);
	}
	tempPath.pop_back();
}



int main() {
	fill(G[0], G[0]+maxn*maxn, INF);
	cin >> n >> m >> c1 >> c2;
	int u, v, dis;
	for (int i = 0; i < n; i++) {
		cin >> rst[i];
	}
	for (int i = 0; i < m; i++) {
		cin >> u >> v >> dis;
		G[u][v] = G[v][u] = dis;
	}
	dijkstra(c1);
	dfs(c2); 
	cout << nums[c2] << " " << max_rst;
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值