admin管理员组

文章数量:1577824

1003 Emergency (25 分)

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

 

思路:对迪杰斯特拉求最短路径的改进,主要要注意的一点是:

       如果经过 v 到达 j 和不经过 v 到达 j 的最短路径长度一样的话,说明到 j 点的这两条路径长度一样,那么,到 j 点的最短路径数量就可以加上到 v 点的最短路径数量,并且到 j 点能聚集的最大人数是经过 v 点和不经过 v 两者之间取最大。(此时可以看成样例中的这种情况 0-2 和 0-1-2 ,v 是1,j 是2)

       如果经过 v 到达 j 的最短路径长度要小于不经过 v 到达 j 的话,需要更新当前到 j 的最短路径长度,同时,到 j 点的最短路径数量是要等于到 v 点的最短路径数量的,到 j 点聚集的人数也是要等于这一条最短路径上能到达的人数,也就是到 v 点时能聚集的最大人数加上 j 点本身有的人数。 (此时可以看成样例中的 3-0-2 和 3-4-2 ,v 是4,j 是2)

代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>
#include<cstring>
#include<cmath>

using namespace std;

const int N = 505, INF = 0x3f3f3f3f;
int teams[N], road[N][N];
int maxteam[N];     //表示到达每个点的时候的最大人数,其实这里可以看成另一个dp数组
int minroad[N];     //保存起点到每个点最短路的条数
int dp[N];          //保存起始点到每个点的最短路径长度
int vis[N];         //该点的访问状态
int n, m, c1, c2;

void init() 
{
	memset(dp, INF, sizeof(dp));
	memset(vis, 0, sizeof(vis));
	memset(maxteam, 0, sizeof(maxteam));
	memset(minroad, 0, sizeof(minroad));
}

void dijk() 
{
	dp[c1] = 0;                         //起点到起点距离为0
	maxteam[c1] = teams[c1];            //初始人数为起点人数
	minroad[c1] = 1;                    //初始最短路为1条

	for (int i = 0; i<n; i++)          //循环n次,每次加入已更新完成的点
	{ 
		int v = -1, temp = INF;        //记录要加入的点的下标
		for (int j = 0; j<n; j++) 
		{
			if (!vis[j] && temp > dp[j]) 
			{
				v = j;
				temp = dp[j];
			}
		}

		vis[v] = 1;     //将选取的点状态置为1
		for (int j = 0; j<n; j++) 
		{
			if (j == v)
				continue;
			if (dp[j] == dp[v] + road[v][j]) 
			{
				minroad[j] += minroad[v];
				maxteam[j] = max(maxteam[j], maxteam[v] + teams[j]);
			}
			if (dp[j] > dp[v] + road[v][j]) 
			{
				dp[j] = dp[v] + road[v][j];
				minroad[j] = minroad[v];
				maxteam[j] = maxteam[v] + teams[j];
			}
		}
	}
}

int main() {
	cin >> n >> m >> c1 >> c2;
	init();
	for (int i = 0; i < n; i++)
		cin >> teams[i];
	for (int i = 0; i<n; i++)
		for (int j = 0; j<n; j++)
			road[i][j] = INF;
	int a, b, c;
	while (m--) 
	{
		cin >> a >> b >> c;
		road[a][b] = road[b][a] = c;
	}
	dijk();
	printf("%d %d\n", minroad[c2], maxteam[c2]);
}

 

本文标签: Emergency