Catalog
  1. 1. Input Specification:
  2. 2. Output Specification:
  3. 3. Sample Input:
  4. 4. Sample Output:
  5. 5. Solution:
PTA(Advanced Level) 1003 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, 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 Specification:

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:

1
2
3
4
5
6
7
8
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:

1
2 4

Solution:

注意:

1.题目要求的是最短路的条数而不是长度

2.memset的用法:将每个字节赋相同值(0x)

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
60
61
62
63
64
65
66
67
68
69
70
#include<iostream>
#include<cstdio>
#include<cstring>

#define INF 99999
#define Ncity 501
#define Nroad 10000

using namespace std;

int rescue[Ncity], map[Ncity][Ncity], dis[Nroad], vis[Ncity] = { 0 }, maxteam[Ncity], way[Ncity];

int main() {
int N, M, C1, C2;
int i, j, k, u, minn;
scanf("%d%d%d%d", &N, &M, &C1, &C2);

//memset(map, INF, sizeof(map)); //初始化地图
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
map[i][j] = INF;
}
}
for (i = 0; i < N; i++) //输入各城市救援队数量
scanf("%d", &rescue[i]);

for (i = 0; i < M; i++) { //输入路径
scanf("%d%d", &j, &k);
scanf("%d", &map[j][k]);
map[k][j] = map[j][k];
}

for (i = 0; i < N; i++) {
dis[i] = map[C1][i];
maxteam[i] = rescue[i];
way[i] = 1;
if (dis[i] != INF && i != C1) {
maxteam[i] += rescue[C1];
}
}

vis[C1] = 1; //标记起点
for (i = 1; i < N; i++) { //Dijkstra
minn = INF;
for (j = 0; j < N; j++) {
if (vis[j] == 0 && dis[j] < minn) {
minn = dis[j];
u = j;
}
}
vis[u] = 1; //标记u
for (k = 0; k < N; k++) { //松弛u
if (map[u][k] < INF && vis[k] == 0) { //沿着一条路走
if (dis[k] > dis[u] + map[u][k]) {
dis[k] = dis[u] + map[u][k];
maxteam[k] = maxteam[u] + rescue[k];
way[k] = way[u];
}
else if (dis[k] == dis[u] + map[u][k]) { //出现了另一条路
way[k] += way[u];
if (maxteam[k] < maxteam[u] + rescue[k]) {
maxteam[k] = maxteam[u] + rescue[k];
}
}
}
}
}
printf("%d %d", way[C2], maxteam[C2]);
return 0;
}
Author: Christopher Shen
Link: https://www.pasxsenger.com/2019/11/09/PTA-Advanced-Level-1003-Emergency/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.
Donate
  • 微信
  • 支付寶