pat

题目

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 $C1$ 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的模板题,加了个计算路径数的。这里用pair和priority_queue优化了代码,简洁一点。模板来自《Competitive Programming》。

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


#include <cstdio>
#include <queue>
#include <vector>
using namespace std;
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef pair<int, ii> pii;
typedef vector<ii> vii;
typedef vector<pii> vpii;
const int INF = 0x3f3f3f3f;
int (){
int N, M, C1, C2, a, b, c;
scanf("%d %d %d %d", &N, &M, &C1, &C2);
vi re(N, 0);
vector<vpii> AdjList(N, vpii());
for(int i = 0;i < N;i++)
scanf("%d", &re[i]);
for(int i = 0;i < M;i++){
scanf("%d %d %d", &a, &b, &c);
AdjList[a].push_back(make_pair(b, make_pair(c, re[b])));
AdjList[b].push_back(make_pair(a, make_pair(c, re[a])));
}
int cnt = 0;
vi dist(N, INF), cost(N, -INF), num(N, 0);
dist[C1] = 0; cost[C1] = re[C1]; num[C1] = 1;
priority_queue<pii, vpii, greater<pii> > pq;
pq.push(make_pair(0, make_pair(0, C1)));
while(!pq.empty()){
pii front = pq.top(); pq.pop();
int d = front.first, u = front.second.second;
if(d > dist[u]) continue;
for(int j = 0;j < (int)AdjList[u].size();j++){
pii v = AdjList[u][j];
if(dist[u] + v.second.first < dist[v.first]){
dist[v.first] = dist[u] + v.second.first;
num[v.first] = num[u];
cost[v.first] = cost[u] + v.second.second;
pq.push(make_pair(dist[v.first], make_pair(cost[v.first], v.first)));
}else if(dist[u] + v.second.first == dist[v.first]){
if(cost[u] + v.second.second > cost[v.first])
cost[v.first] = cost[u] + v.second.second;
num[v.first] += num[u];
}
}
}
printf("%d %d", num[C2], cost[C2]);
return 0;
}