PAT(Advanced Level)Practice 1003 Emergency(迪杰斯特拉求最短路径,纯C实现)

Emergency

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

题意:一共有n个城市,城市间有m条路,你所在的城市为C1,你要求救援的城市为C2,然后每个城市有x个救援队,让你求出从你所在城市到达目的城市的最短路径,并且求出最短路径有多少条,在最短路径中你最多能带多少救援队过来。
AC代码:

#include <stdio.h>
#include <string.h>

inline int max(int a, int b) {
	return a > b ? a : b;
}
int main() {
	int G[500][505], n, m, start, end, num[505], n1[505], s[505];  //n1表示救援队的数量,s表示最短路径的数量
	memset(G, -1, sizeof(G));
	scanf("%d %d %d %d", &n, &m, &start, &end);
	for(int i = 0; i < n; i++) scanf("%d", &num[i]);
	for(int i = 0, u, v, w; i < m; i++) {
		scanf("%d %d %d", &u, &v, &w);
		G[u][v] = w;
		G[v][u] = G[u][v];
	}
	int d[505], vis[505];
	memset(d, 0x3f, sizeof(d));
	memset(vis, 0, sizeof(vis));
	memset(n1, 0, sizeof(n1));
	memset(s, 0, sizeof(s));
	d[start] = 0;
	s[start] = 1;
	n1[start] = num[start];
	for(int i = 0; i < n; i++) {
		int min = 0x3f3f3f3f, u = -1;
		for(int j = 0; j < n; j++) {
			if(!vis[j] && d[j] < min) {
				min = d[j];
				u = j;
			}
		}
		if(u == -1) break;
		vis[u] = 1;
		for(int j = 0; j < n; j++) {
			if(!vis[j] && G[u][j] != -1)
				if(d[j] > d[u] + G[u][j]) {
					d[j] = d[u] + G[u][j];
					s[j] = s[u];
					n1[j] = n1[u] + num[j];
				} else if(d[j] == d[u] + G[u][j]) {
					s[j] += s[u];
					n1[j] = max(n1[j], n1[u] + num[j]);
				}
		}
		if(u == end) break;
	}
	printf("%d %d", s[end], n1[end]);
	return 0;
}

End

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 游动-白 设计师:上身试试 返回首页