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 444 positive integers: N(≤500)N (≤500)N(≤500) - the number of cities (and the cities are numbered from 000 to N−1N−1N−1), MMM - the number of roads, C1C_1C1and C2C_2C2- the cities that you are currently in and that you must save, respectively. The next line contains NNN integers, where the i−thi-thi−th integer is the number of rescue teams in the i−thi-thi−th city. Then MMM lines follow, each describes a road with three integers c1c_1c1,c2c_2c2 and LLL, 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 C1C1C1to C2C2C2
Output Specification:
For each test case, print in one line two numbers: the number of different shortest paths between C1C_1C1and C2C_2C2 , 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]+dis==d[v]d[u]+dis==d[v]d[u]+dis==d[v]时,无论mx[u]+a[v]>mx[v]mx[u]+a[v]>mx[v]mx[u]+a[v]>mx[v]是否成立,都应当让num[v]+=num[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;
}