PAT 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

解题思路:
本题是一个求单源最短路径的问题,因此可以用Dijkstra算法来进行求解,题中要求输出最短路径的条数和最短路径上最大的点权之和,所以需要在该算法中加上两个数组,一个用于记录最短路径的条数,一个用于保存最短路径上最大的点权之和。

代码如下:

#include
#include
#include
using namespace std;
#define INFINITY 0x3f3f3f3f
#define MAX 502

int G[MAX][MAX];
int team[MAX], dist[MAX], path[MAX],w[MAX],num[MAX];//w[]为最大点权之和,num[]为最短路径数量
bool collected[MAX] = { false };//用来记录结点是否有被收录进去
int N,M,C1,C2; //N为结点个数,M为边的个数,C1为起点,C2为终点

/*邻接矩阵存储图*/
void Create_Graph() {
	/*初始化邻接矩阵*/
	memset(G, INFINITY, sizeof(G));
	cin >> N >> M >> C1 >> C2;
	for (int i = 0; i < N; i++)
		G[i][i] = 0;
	/*构建图*/
	for (int i = 0; i < N; i++) {
		cin >> team[i];
	}
	int u, v, w;
	for (int i = 0; i < M; i++) {
		cin >> u >> v >> w;
		G[u][v] = w;
		G[v][u] = w;
	}	
}

int FindMinDist() {
	int MidDist = INFINITY;
	int MinV=-1;
	for (int i = 0; i < N; i++) {
		if (collected[i] == false && dist[i] < MidDist) {
			MidDist = dist[i];
			MinV = i;
		}
	}
	if (MinV == -1)
		return -1;
	return MinV;
}

void Dijkstra_GSP() {
	memset(dist, INFINITY, sizeof(dist));
	memset(w, 0, sizeof(w));
	memset(num, 0, sizeof(num));
	dist[C1] = 0;
	w[C1] = team[C1];
	num[C1] = 1;	
	int V;
	while (1) {
		/* V = 未被收录顶点中dist最小者 */
		V = FindMinDist();
		if (V == -1)
			break;
		collected[V] = true;
		for (int i = 0; i < N; i++) {
			if (collected[i] == false && G[V][i] < INFINITY) {
				if (dist[V] + G[V][i] < dist[i]) {
					dist[i] = dist[V] + G[V][i];
					w[i] = w[V] + team[i];//更新点权之和
					num[i] = num[V];
				}
				else if(dist[V] + G[V][i] == dist[i]){
					if (w[V] + team[i] > w[i])//相同最短路径下保存点权最大的一个
						w[i] = w[V] + team[i];
					num[i] += num[V];
				}
				

			}
		}
	}

}

int main() {
	Create_Graph();
	Dijkstra_GSP();
	cout << num[C2] <<" "<< w[C2]<<endl;
	return 0;
}

你可能感兴趣的:(PAT)