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 positive integers: - the number of cities (and the cities are numbered from to ), - the number of roads, and - the cities that you are currently in and that you must save, respectively. The next line contains integers, where the -th integer is the number of rescue teams in the -th city. Then lines follow, each describes a road with three integers , and , 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 to .
Output Specification:
For each test case, print in one line two numbers: the number of different shortest paths between and , 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
solve
需要找到从起点到终点的最短路径,同时需要统计最短路径的数量和在这些路径上能收集到的最大救援队数量。
城市数在500以内,可以使用邻接表存图,使用dijkstra来搜索从C1开始到C2的最短路径。
dijkstra是进行最多N-1次搜索,每次搜索找出目前图中未搜过且距离C1最近的点作为起点,遍历其所有边并更新路径。那么再维护一个给每个点维护一个C1到该点的最短路径数量,以及路径上累积的最大救援队数量。
#include<iostream>
#include<vector>
using namespace std;
const int INF = 0x3f3f3f3f;
int main() {
int N, M, C1, C2;
cin >> N >> M >> C1 >> C2;
vector<int> teams(N,0);
for(int i = 0; i < N; i++) cin >> teams[i];
vector<vector<int>> g(N, vector<int> (N, INF));
while(M--) {
int a, b, c;
cin >> a >> b >> c;
g[a][b] = g[b][a] = c;
}
vector<int> dist(N, INF);
vector<int> num(N, 0);
vector<int> maxTeams(N, 0);
dist[C1] = 0;
num[C1] = 1;
maxTeams[C1] = teams[C1];
vector<int> visited(N, false);
for(int i = 0; i < N; i++) {
int u = -1;
for(int j = 0; j < N; j++) {
if(!visited[j] && (u == -1 || dist[u] > dist[j]))
u = j;
}
if(u == -1) break;
visited[u] = true;
for(int j = 0; j < N; j++) {
if(visited[j] || g[u][j] == INF)
continue;
if(dist[u] + g[u][j] < dist[j]) {
dist[j] = dist[u] + g[u][j];
num[j] = num[u];
maxTeams[j] = maxTeams[u] + teams[j];
} else if(dist[u] + g[u][j] == dist[j]) {
num[j] += num[u];
maxTeams[j] = max(maxTeams[j], maxTeams[u] + teams[j]);
}
}
}
cout << num[C2] << " " << maxTeams[C2] << endl;
return 0;
}