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.
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, C1and C2 - 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 c1, c2 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 C1 to C2.
For each test case, print in one line two numbers: the number of different shortest paths between C1 and 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.
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
2 4
方法一:
在节点上附加了第二把尺度,一般用DFS做,但考虑到题目只需要求最大的救援人数,所以直接一遍Dijkstra解决即可,增加的点条件初始值即为该点救援人数的数量,另外需要求方案数量,只需定义一个nums数组存放即可,初始条件为1即可。
#include
#define INF 1000000000
#define maxn 505
using namespace std;
int G[maxn][maxn], dis[maxn], nums[maxn], gather[maxn];
bool vis[maxn];
int n, m, c1, c2;
int rst[maxn];
void dijkstra(int s) {
fill(dis, dis+maxn, INF);
fill(nums, nums+maxn, 0);
fill(gather, gather+maxn, 0);
fill(vis, vis+maxn, false);
gather[s] = rst[s];
dis[s] = 0;
nums[s] = 1;
for (int i = 0; i < n; i++) {
int u = -1, min = INF;
for (int j = 0; j < n; j++) {
if (!vis[j] && dis[j] < min) {
min = dis[j];
u = j;
}
}
if (u == -1) {
return;
}
vis[u] = true;
for (int v = 0; v < n; v++) {
if (!vis[v] && G[u][v] != INF) {
if (dis[u] + G[u][v] < dis[v]) {
dis[v] = dis[u] + G[u][v];
gather[v] = gather[u] + rst[v];
nums[v] = nums[u];
} else if (dis[u] + G[u][v] == dis[v]) {
nums[v] += nums[u];
if (gather[u] + rst[v] > gather[v]) {
gather[v] = gather[u] + rst[v];
}
}
}
}
}
}
int main() {
fill(G[0], G[0]+maxn*maxn, INF);
int u, v, dis;
cin >> n >> m >> c1 >> c2;
for (int i = 0; i < n; i++) {
cin >> rst[i];
}
for (int i = 0; i < m; i++) {
cin >> u >> v >> dis;
G[u][v] = G[v][u] = dis;
}
dijkstra(c1);
cout << nums[c2] << " " << gather[c2];
return 0;
}
方法二:
利用Dijkstra+DFS,用DFS遍历每条路径,然后计算路径上包含救援队最多的path即可。
#include
#define INF 1000000000
#define maxn 505
using namespace std;
vector pre[maxn], path, tempPath;
int n, m, c1, c2;
int G[maxn][maxn], dis[maxn], rst[maxn], nums[maxn];
bool vis[maxn];
void dijkstra(int s) {
fill(dis, dis+maxn, INF);
fill(vis, vis+maxn, false);
fill(nums, nums+maxn, 0);
dis[s] = 0;
nums[s] = 1;
for (int i = 0; i < n; i++) {
int u = -1, min = INF;
for (int j = 0; j < n; j++) {
if (!vis[j] && dis[j] < min) {
min = dis[j];
u = j;
}
}
if (u == -1) {
return;
}
vis[u] = true;
for (int v = 0; v < n; v++) {
if (!vis[v] && G[u][v] != INF) {
if (dis[u] + G[u][v] < dis[v]) {
dis[v] = dis[u] + G[u][v];
pre[v].clear();
pre[v].push_back(u);
nums[v] = nums[u];
} else if (dis[u] + G[u][v] == dis[v]) {
nums[v] += nums[u];
pre[v].push_back(u);
}
}
}
}
}
int max_rst = -1;
void dfs(int u) {
if (u == c1) {
tempPath.push_back(u);
int temp_rst = 0;
for (int i = 0; i < tempPath.size(); i++) {
int v = tempPath[i];
temp_rst += rst[v];
}
if (temp_rst > max_rst) {
max_rst = temp_rst;
}
tempPath.pop_back();
return;
}
tempPath.push_back(u);
for (int i = 0; i < pre[u].size(); i++) {
dfs(pre[u][i]);
}
tempPath.pop_back();
}
int main() {
fill(G[0], G[0]+maxn*maxn, INF);
cin >> n >> m >> c1 >> c2;
int u, v, dis;
for (int i = 0; i < n; i++) {
cin >> rst[i];
}
for (int i = 0; i < m; i++) {
cin >> u >> v >> dis;
G[u][v] = G[v][u] = dis;
}
dijkstra(c1);
dfs(c2);
cout << nums[c2] << " " << max_rst;
return 0;
}