【PAT-Advanced】1003 Emergency

Problem

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



Solution

这是图算法里最短路径问题的变形。

因为节点数不太大,所以用邻接矩阵来存储路径(其实是懒得动脑)。

并且因为懒得动脑不想写Dijkstra,就用dfs来遍历,遍历到目的地之后比较路径长度和救援队数量,满足条件则更新对应变量。如果不是目的地则继续往前搜索直到搜不到新的节点。

实现代码如下:

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
71
72
73
#include <bits/stdc++.h>

#define MAXN 505
#define INF 10000000

using namespace std;

int M; //number of roads
int N; //number of cities
int C1, C2; //outset, desitination
int rt_num[MAXN]; //number of rescue teams in each city
int road[MAXN][MAXN]; //Adjacency matrix
int visited[MAXN];
int cnt;
int shortest_path;
int max_rt;

void init()
{
M = N = C1 = C2 = cnt = max_rt = 0;
shortest_path = INF;
for(int i=0; i<MAXN; i++){
visited[i] = 0;
rt_num[i] = 0;
for(int j=0; j<MAXN; j++){
road[i][j] = INF;
}
}
}

void dfs(int point, int length, int rt){
visited[point] = 1;
if(length > shortest_path){
return;
}
if(point == C2){
if(length < shortest_path){
cnt = 1;
shortest_path = length;
max_rt = rt;
}else if(length == shortest_path){
cnt++;
if(rt > max_rt){
max_rt = rt;
}
}
}else{
for(int i=0; i<N; i++){
if(road[point][i] != INF && visited[i] == 0){
dfs(i, length+road[point][i], rt+rt_num[i]);
visited[i] = 0;
}
}
}

}

int main(int argc, const char * argv[]) {
init();
cin >> N >> M >> C1 >> C2;
for(int i=0; i<N; i++){
cin >> rt_num[i];
}
int start, end, length;
for(int i=0; i<M; i++){
cin >> start >> end >> length;
road[start][end] = length;
road[end][start] = length;
}
dfs(C1, 0, rt_num[C1]);
cout << cnt << " " << max_rt << endl;
return 0;
}