PAT 甲级 1003. Emergency (25) C++版

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

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, C1 and C2 - 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 c1, c2 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 C1 to C2.

Output

For each test case, print in one line two numbers: the number of different shortest paths between C1 and C2, 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

使用深搜遍历图的所有路径,在所有的路径里找到那条最优的路径。这种暴力破解的方法会耗时些,处理的不好可能会运行超时。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <cstdio>

int final_len = 10000, final_team, routes, isVisited[500], teams[500], cities[500][500];

void dfs(int cur, int dest, int len, int team, int n) {
if (cur == dest) {
if (final_len > len) {
final_len = len;
routes = 1;
final_team = team;
} else if (final_len == len) {
routes += 1;
final_team = (final_team > team ? final_team : team);
}
return;
}
for (int i = 0; i < n; i++) {
if (!isVisited[i] && cities[cur][i] != 0) {
isVisited[i] = 1;
dfs(i, dest, len + cities[cur][i], team + teams[i], n);
isVisited[i] = 0;
}
}
}

int main() {
int n = 0, m = 0, c1 = 0, c2 = 0;
scanf("%d %d %d %d", &n, &m, &c1, &c2);
for (int i = 0; i < n; i++) {
scanf("%d", &teams[i]);
}
for (int i = 0; i < m; i++) {
int a = 0, b = 0, l = 0;
scanf("%d %d %d", &a, &b, &l);
cities[a][b] = cities[b][a] = l;
}

isVisited[c1] = 1;
dfs(c1, c2, 0, teams[c1], n);
printf("%d %d", routes, final_team);
return 0;
}

另一种方法是使用Dijkstra算法来求解, Dijkstra利用贪心算法的思想,每次都找到距离起点最近的点,所以就没有那么耗时了。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <cstdio>

const int inf = 999999;
int recur[501], c[501][501], dist[501], visit[501], cnt[501], recurs[501];

int find_index(int n) {
int min = -1;
for (int i = 0; i < n; i++) {
if (!visit[i] && (min == -1 || dist[i] < dist[min])) {
min = i;
}
}
return min;
}

int main() {

for (int i = 0; i <= 500; i++) {
for (int j = 0; j <= 500; j++) {
c[i][j] = inf;
}
dist[i] = inf;
}

int n = 0, m = 0, c1 = 0, c2 = 0, a = 0, b = 0, l = 0;
scanf("%d %d %d %d", &n, &m, &c1, &c2);
for (int i = 0; i < n; i++) {
scanf("%d", &recur[i]);
}
for (int i = 0; i < m; i++) {
scanf("%d %d %d", &a, &b, &l);
c[a][b] = c[b][a] = l;
}

dist[c1] = 0;
cnt[c1] = 1;
recurs[c1] = recur[c1];
while (true) {
int index = find_index(n);
if (index == -1) break;
visit[index] = 1;
for (int i = 0; i < n; i++) {
if (!visit[i]) {
if (dist[i] > dist[index] + c[index][i]) {
dist[i] = dist[index] + c[index][i];
cnt[i] = cnt[index];
recurs[i] = recurs[index] + recur[i];
} else if (dist[i] == dist[index] + c[index][i]) {
cnt[i] += cnt[index];
if (recurs[i] < recurs[index] + recur[i]) {
recurs[i] = recurs[index] + recur[i];
}
}
}
}
}
printf("%d %d", cnt[c2], recurs[c2]);
return 0;
}