一、题目
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: 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 Specification:
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
二、题目大意
从出发地到目的地,要求路程最短,在路程最短的情况下,权重最小。
三、考点
DFS、图
DFS核心思想与模板:【笨方法学PAT】DFS核心思想与模板
四、解题思路
1、使用二维数组保存图,一维数组保存权重,一维数组标记点是否访问过;将起点、终点、输出结果作为全局变量。
2、DFS,最终要输出最短路径的个数,最短路径中最大救援组个数(权重),所以 dfs(int index,int path_length,int team_num),维护当前点、当前路径长度、当前救援小组个数,三个变量。
3、路径更短的时候,对最短路径、最短路径个数、救援小组个数进行更新;路径一样,救援小组更多,对最短路径个数、救援小组个数进行更新。
3、DFS遍历中,要同时对是否访问过点进行标记,使用 bool visit[] 一维数组。
五、代码
#include<iostream>
#include<string>
#define N 510
#define INF 99999999
using namespace std;
int e[N][N], weight[N];
bool visit[N];
int ans_path_num=0, ans_team_num=0,ans_min_length = INF;
int n, m, m_start, m_end;
void dfs(int index,int path_length,int team_num) {
//剪枝
if (path_length > ans_min_length)
return;
//终止条件
if (index == m_end) {
if (ans_min_length>path_length) {
ans_min_length = path_length;
ans_path_num = 1;
ans_team_num = team_num;
}
else if (ans_min_length == path_length) {
ans_path_num++;
if (ans_team_num < team_num)
ans_team_num = team_num;
}
return;
}
//下一层
for (int i = 0; i < n; ++i) {
if (visit[i] == false && e[index][i] != INF) {
visit[i] = true;
dfs(i, path_length + e[index][i], team_num + weight[i]);
visit[i] = false;
}
}
}
int main() {
//初始化路
fill(e[0], e[0] + N*N, INF);
fill(visit, visit + N, false);
//读取数据
cin >> n >> m >> m_start >> m_end;
for (int i = 0; i < n; ++i)
cin >> weight[i];
for (int i = 0; i < m; ++i) {
int a, b, c;
cin >> a >> b >> c;
e[a][b] = e[b][a] = c;
}
//DFS
visit[m_start] = true;
dfs(m_start, 0, weight[m_start]);
//输出
cout << ans_path_num << " " << ans_team_num;
system("pause");
return 0;
}