PAT (Advanced Level) 1003 Emergency (25 分)

版权声明:转载请附链接 https://blog.csdn.net/isunLt/article/details/83690111

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

Code

#include <iostream>
#include <cstring>
using namespace std;

const int maxNumOfCity = 505;
const int INF = 0x3f3f3f3f;
int cityWeight[maxNumOfCity]; // 每个城市有的救援队人数
int cityCost[maxNumOfCity][maxNumOfCity]; //邻接矩阵,用以表示每个城市连接的关系
int dist[maxNumOfCity]; //记录原点到其他城市的最短距离
int pathCount[maxNumOfCity], numOfMen[maxNumOfCity]; //记录最短路径的条数和延最短路径所能集结到的救援队人数
bool isVisit[maxNumOfCity]; //记录每个节点(这里指城市)是否被访问过 
int numOfCity, numOfRoad; // 记录实际城市数量和路的数量

void Dijkstra(int org); // 函数声明

int main()
{
	int org, dst;
	cin >> numOfCity >> numOfRoad >> org >> dst; // 获取输入数据的第一行
	memset(isVisit, false, sizeof(isVisit));
	for (int i = 0; i < numOfCity; i++)
	{
		pathCount[i] = numOfMen[i] = 0;
		for (int j = 0; j < numOfCity; j++)
		{
			cityCost[i][j] = (i == j) ? 0 : INF;
		}
	}
	for (int i = 0; i < numOfCity; i++)
	{
		cin >> cityWeight[i]; //获取输入数据的第二行
	}
	for (int i = 0; i < numOfRoad; i++)
	{
		int start, end, cost;
		cin >> start >> end >> cost;
		cityCost[start][end]= cityCost[end][start]= cost; //获取剩下的输入数据
	}
	Dijkstra(org);
	cout << pathCount[dst] << " " << numOfMen[dst] << endl;
	return 0;
}

void Dijkstra(int org)
{
	//起点城市到起点城市
	pathCount[org] = 1; //有1条路
	numOfMen[org] = cityWeight[org]; //能集结到的救援人数就是城市驻扎的人数
	isVisit[org] = true;
	//初始化dist表
	for (int i = 0; i < numOfCity; i++)
	{
		dist[i] = cityCost[org][i];
		if (dist[i] != INF && i != org) //如果这个城市i可经由起点城市到达
		{
			numOfMen[i] = numOfMen[org] + cityWeight[i]; //可集结人数等于起点人数加城市i驻扎人数
			pathCount[i] = 1;
		}
	}

	for (int i = 0; i < numOfCity - 1; i++) //对于剩下的numOfCity-1个城市
	{
		//找到距离最近的城市pos
		int minDist = INF, pos = -1;
		for (int j = 0; j < numOfCity; j++)
		{
			if (!isVisit[j] && dist[j] < minDist)
			{
				minDist = dist[j];
				pos = j;
			}
		}
		isVisit[pos] = true;
		//更新dist表
		for (int j = 0; j < numOfCity; j++)
		{
			//如果城市没被访问过,而且起点到pos加从pos到j城市j的距离缩短
			if (!isVisit[j] && dist[pos] + cityCost[pos][j] < dist[j])
			{
				dist[j] = dist[pos] + cityCost[pos][j]; //更新从起点到j的最短距离
				numOfMen[j] = numOfMen[pos] + cityWeight[j]; //可集结的人数等于到pos集结的人数加j城本身驻扎的人数
				pathCount[j] = pathCount[pos];
			}
			//如果城市没被访问过,而且起点到pos加从pos到j城市j的距离与之前相等
			//表示又找到了一条新的最短路
			else if (!isVisit[j] && dist[pos] + cityCost[pos][j] == dist[j])
			{
				pathCount[j] += pathCount[pos]; //最短路数量相加加
				if (numOfMen[j] < numOfMen[pos] + cityWeight[j])
				{
					numOfMen[j] = numOfMen[pos] + cityWeight[j]; //更新可集结人数
				}
			}
		}
	}
	return;
}

思路

单源最短路径问题,无负权重路径,使用Dijkstra算法。本解题思路详见代码注释。

另附:为什么无穷大总是0x3f3f3f3f?
https://blog.csdn.net/hurmishine/article/details/51946015

以上

猜你喜欢

转载自blog.csdn.net/isunLt/article/details/83690111
今日推荐