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 4 4 positive integers: N ( ≤ 500 ) N (≤500) N(≤500) - the number of cities (and the cities are numbered from 0 0 0 to N − 1 N−1 N−1), M M M - the number of roads, C 1 C_1 C1and C 2 C_2 C2- the cities that you are currently in and that you must save, respectively. The next line contains N N N integers, where the i − t h i-th i−th integer is the number of rescue teams in the i − t h i-th i−th city. Then M M M lines follow, each describes a road with three integers c 1 c_1 c1, c 2 c_2 c2 and L L 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 C1 C1to C 2 C2 C2
Output Specification:
For each test case, print in one line two numbers: the number of different shortest paths between C 1 C_1 C1and C 2 C_2 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
思路
- 此题是求最短路径条数加上第二标尺点权的综合应用。
- 当 d [ u ] + d i s = = d [ v ] d[u]+dis==d[v] d[u]+dis==d[v]时,无论 m x [ u ] + a [ v ] > m x [ v ] mx[u]+a[v]>mx[v] mx[u]+a[v]>mx[v]是否成立,都应当让 n u m [ v ] + = n u m [ u ] num[v]+=num[u] num[v]+=num[u],因为最短路径条数的依据仅仅是第一标尺距离,与点权无关。
#include <bits/stdc++.h>
using namespace std;
const int maxn = 510;
const int inf = 0x3f3f3f3f;
int n,m,s,t;
struct edge{
int v,dis;
};
vector<edge>e[maxn];
int a[maxn];
int d[maxn];
bool vis[maxn];
int num[maxn];
int mx[maxn];
void Dijstra(int s) {
memset(d, 0x3f, sizeof(d));
d[s] = 0;
num[s] = 1;
mx[s]=a[s];
for (int i = 0; i < n; i++) {
int u = -1, mi = inf;
for (int j = 0; j < n; j++) {
if (!vis[j] && d[j] < mi) {
mi = d[j];
u = j;
}
}
if (u == -1) break;
vis[u] = 1;
for (auto &[v, dis]:e[u]) {
if (vis[v]) continue;
if (d[u] + dis < d[v]) {
d[v] = d[u] + dis;
num[v] = num[u];
mx[v] = mx[u] + a[v];
} else {
if (d[u] + dis == d[v]) {
num[v] += num[u];
mx[v] = max(mx[u] + a[v], mx[v]);
}
}
}
}
printf("%d %d\n", num[t], mx[t]);
}
int main() {
scanf("%d%d%d%d", &n, &m, &s, &t);
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
for (int i = 1; i <= m; i++) {
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
e[u].push_back({v, w});
e[v].push_back({u, w});
}
Dijstra(s);
return 0;
}