一、题目:
1003 Emergency (25 分)
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: - the number of cities (and the cities are numbered from 0 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
二、思路
使用Dijkstra
三、注意点
留意team[]
和route[]
的初始化和修改的位置在哪。
当有多条最短路径时,要把它们的route
加起来。
四、AC代码
#include <iostream>
#include <limits.h>
using namespace std;
void Dijkstra(int **map, const int city[], int v, int n, int dist[],
int prev[], int team[], int route[]) {
bool s[n];//S集合,已经在特殊路径中
//初始化S,dist,prev,team,route
for (int i = 0; i < n; ++i) {
s[i] = false;
dist[i] = map[v][i];
if (dist[i] == INT_MAX) {//与源断开
prev[i] = -1;
route[i] = 0;
team[i] = 0;
} else {//与源连接
prev[i] = v;
route[i] = 1;
team[i] = city[v] + city[i];
}
}
s[v] = true;
team[v] = city[v];
//开始
for (int i = 0; i < n; ++i) {
//选出与S 集合相连的最近点
int min_dist = INT_MAX;
int u = v;
for (int j = 0; j < n; ++j) {
if ((!s[j]) && (dist[j] < min_dist)) {
u = j;
min_dist = dist[j];
}
}
//纳入S 集合
s[u] = true;
//更新非S 集合点的距离
for (int j = 0; j < n; ++j) {
if ((!s[j]) && (map[u][j] < INT_MAX)) {//可以通过新点连接到
int new_dist = dist[u] + map[u][j];//通过新点连接后的距离
if (new_dist < dist[j]) {//缩短了距离,更新
dist[j] = new_dist;
prev[j] = u;
team[j] = team[u] + city[j];
route[j] = route[u];
} else if (new_dist == dist[j]) {//距离相同
route[j] += route[u];
int new_team = team[u] + city[j];
if (new_team > team[j])
team[j] = new_team;
}
}
}
}
}
int main() {
int city_n = 0, road_n = 0, from_id = 0, to_id = 0;
cin >> city_n >> road_n >> from_id >> to_id;
int *city = new int[city_n];
for (int i = 0; i < city_n; ++i) {
cin >> city[i];
}
int **map = new int *[city_n];
for (int i = 0; i < city_n; ++i) {
map[i] = new int[city_n];
for (int j = 0; j < city_n; ++j) {
map[i][j] = INT_MAX;
}
map[i][i] = 0;
}
int c1 = 0, c2 = 0, dist = 0;
for (int i = 0; i < road_n; ++i) {
cin >> c1 >> c2 >> dist;
map[c1][c2] = dist;
map[c2][c1] = dist;
}
int *dists = new int[city_n];
int *prev = new int[city_n];
int *team = new int[city_n];
int *route = new int[city_n];
Dijkstra(map, city, from_id, city_n, dists, prev, team, route);
cout << route[to_id] << " " << team[to_id];
}