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
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, C1 and 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.
- Output
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.
- 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
代码一:
- 1.用Dijkstra思想求出单源最短路径的距离
- 2.用DFS思想统计该距离的所有路径
#include <iostream>
using namespace std;
const int N = 500;
const int INFINITY = 1000000;
bool isVisited[N];
int road[N][N];
int team_quantity[N];
int distance_count[N];
int path_min_count = 0;
int team_count = 0;
void initData() {
int i, j;
for(i = 0; i < N; ++i) {
isVisited[i] = false;
team_quantity[i] = 0;
distance_count[i] = INFINITY;
for(j = 0; j < N; ++j) {
road[i][j] = INFINITY;
road[j][i] = INFINITY;
}
}
}
void Dijkstra(int n, int start, int end) {
// 统计出发点到下一点的各个距离
for(int i = 0; i < n; ++i)
distance_count[i] = road[start][i];
isVisited[start] = true;
for(int i = 0; i < n-1; ++i) {
int minDistance = INFINITY;
int currentPosition = 0;
// 选择下一个最近的点
for(int j = 0; j < n; ++j) {
if(!isVisited[j] && distance_count[j] < minDistance) {
minDistance = distance_count[j];
currentPosition = j;
}
}
if(minDistance == INFINITY)
return;
isVisited[currentPosition] = true;
// 更新distance数组内容为从出发点到current的下一点的各个距离
for(int j = 0; j < n; ++j) {
if(road[currentPosition][j] < INFINITY && distance_count[j] > distance_count[currentPosition] + road[currentPosition][j])
distance_count[j] = distance_count[currentPosition] + road[currentPosition][j];
}
}
}
void dfs(int n, int cur_city, int end, int currentDistane, int currentTeamCount) {
isVisited[cur_city] = true;
if(cur_city == end) {
// 找到一条最短路径
if(currentDistane == distance_count[end]) {
++path_min_count;
if(currentTeamCount > team_count)
team_count = currentTeamCount;
}
return;
}
if(currentDistane > distance_count[end])
return;
for(int i = 0; i < n; ++i) {
if(distance_count[cur_city] + road[cur_city][i] == distance_count[i])
dfs(n, i, end, (currentDistane + road[cur_city][i]), (currentTeamCount + team_quantity[i]));
}
}
int main() {
int N, M, C1, C2, L, start, end;
initData();
cin >> N >> M >> start >> end;
for(int i = 0; i < N; ++i)
cin >> team_quantity[i];
for(int i = 0; i < M; ++i) {
cin >> C1 >> C2 >> L;
road[C1][C2] = L;
road[C2][C1] = L;
}
Dijkstra(N, start, end);
for(int i = 0; i < N; ++i)
isVisited[i] = false;
distance_count[start] = 0;
dfs(N, start, end, 0, team_quantity[start]);
cout << path_min_count << " " << team_count << endl;
return 0;
}
代码二
- 用DFS实现
#include <iostream>
using namespace std;
const int INFINITY = 0x7FFFFF;
bool isVisit[500] = {false};
int road[500][500];
int min_distance = INFINITY;
int min_road_count = 0;
int max_team = 0;
int teams[500]={0};
int N, M, start_city, end_city;
void dfs(int cur_city, int cur_distance, int cur_teams) {
if(cur_city == end_city) {
if(cur_distance < min_distance) {
min_distance = cur_distance;
min_road_count = 1;
max_team = cur_teams;
}else if(cur_distance == min_distance) {
++min_road_count;
if(cur_teams > max_team)
max_team = cur_teams;
}
return;
}
isVisit[cur_city] = true;
for(int i=0; i<N; ++i) {
if(isVisit[i]==false && road[cur_city][i]>0) {
dfs(i, cur_distance+road[cur_city][i], cur_teams+teams[i]);
}
}
isVisit[cur_city] = false;
}
int main() {
cin >> N >> M >> start_city >> end_city;
for(int i=0; i<N; ++i)
cin >> teams[i];
for(int i=0; i<M; ++i) {
int C1, C2, L;
cin >> C1 >> C2 >> L;
road[C1][C2] = L;
road[C2][C1] = L;
}
dfs(start_city, 0, teams[start_city]);
printf("%d %d\n", min_road_count, max_team);
return 0;
}
代码三
- 用Dijkstra实现
#include <iostream>
using namespace std;
const int INF = 0x7fffff;
const int MX = 500;
int road[MX][MX];
bool isVisit[MX] = {false};
int dist[MX];
int team_amount[MX];
int teams[MX];
int pathcount[MX];
int N,M,start,en;
void Dijkstra(int s){
team_amount[s] = teams[s];
dist[s] = 0;
pathcount[s] = 1;
while (1){
int u, dmin=INF;
for (int i=0; i<N; i++){
if (isVisit[i] == false && dist[i]<dmin){
dmin = dist[i];
u = i;
}
}
if (dmin==INF || u==en) break;
isVisit[u] = true;
for (int i=0; i<N; i++){
if(isVisit[i] == false){
if (dist[i] > dist[u] + road[u][i]){
dist[i] = dist[u] + road[u][i];
team_amount[i] = team_amount[u] + teams[i];
pathcount[i] = pathcount[u];
}else if (dist[i] == dist[u] + road[u][i]){
pathcount[i] += pathcount[u];
if (team_amount[i] < team_amount[u] + teams[i])
team_amount[i] = team_amount[u] + teams[i];
}
}
}
}
}
int main() {
cin >> N >> M >> start >> en;
for(int i=0; i<N; i++) {
cin >> teams[i];
}
for(int i=0; i<N; i++) { //必要的初始化
dist[i] = INF;
for(int j=0; j<N; j++)
road[i][j] = INF;
}
for(int i=0; i<M; i++) {
int c1, c2, L;
cin >> c1 >> c2 >> L;
road[c1][c2] = L;
road[c2][c1] = L;
}
Dijkstra(start);
printf("%d %d\n", pathcount[en], team_amount[en]);
return 0;
}