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. 深度遍历到C2就可返回;
2. 使用一个标记每个点是否正在遍历的状态数组,防止在一个环上死循环;
3. 如果当前的路径长度和已经比已有的最短路径长,则返回,加快速度,不然最后一个测试点可能会超时;
4. 注意如果最小路径长度相等要把所有的最短路径都记录下来,最后输出rescue和最大的值。
#include
#include
#include
#include
using namespace std;
const int max_city = 500 + 5;
int rescue[max_city];
int road[max_city][max_city];
int visit[max_city];
int C1, C2, N;
struct way
{
int sum_res = 0;
int sum_L = 0;
};
vector min_ways;
void dfs(int c1,int c2,way w)//这里要用两个点,因为需要确定是通过哪条路到达C2,w是通过传值传递下去;
{
if (road[c1][c2] == 0 && c1 != c2)
{
return ;
}
if (visit[c2] == 1)
{
return ;
}
w.sum_res += rescue[c2];
w.sum_L += road[c1][c2];
if (!min_ways.empty() && min_ways[0].sum_L < w.sum_L)
{
return;
}
visit[c2] = 1;
if (c2 == C2)
{
if (min_ways.empty()|| min_ways[0].sum_L == w.sum_L)
{
min_ways.push_back(w);
}
else if (min_ways[0].sum_L > w.sum_L)
{
min_ways.clear();
min_ways.push_back(w);
}
visit[c2] = 0;
return;
}
for (int i = 0; i < N; ++i)
{
if (i==c2)
{
continue;
}
dfs(c2, i, w);
}
visit[c2] = 0;
return;
}
int main()
{
int M;
cin >> N >> M >> C1 >> C2;
memset(rescue, 0, sizeof(rescue));
memset(road, 0, sizeof(road));
memset(visit, 0, sizeof(visit));
for (int i = 0; i < N; ++i)
{
cin >> rescue[i];
}
for (int i = 0,c1,c2,L; i < M; ++i)
{
cin >> c1 >> c2 >> L;
road[c1][c2] = road[c2][c1] = L;
}
way w;
dfs(C1, C1, w);
int max_rescue = 0;
for (auto it : min_ways)
{
if (max_rescue < it.sum_res)
{
max_rescue = it.sum_res;
}
}
cout << min_ways.size() <<" "<< max_rescue << endl;
system("pause");
return 0;
}