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
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
题目大意
根据例子,给定5个城市(点),6条道路(边) ,以及每个城市救援队数,以及道路的权值,如下图所示
题目的意思就是一张图,各边都有权值而且个点也有权值,求出给定两点之间最短路的条数并输出此路径上的点权值和的最大值。就是求出最短路的条数,若有多条相同的最短路输出最大的那个点权值和。
解题思路
先用Dijstra算法算出最短路径,然后dfs寻找最优解
AC代码
#include<iostream>
#include <iomanip>
#include<vector>
#include<set>
#include<string>
#include<algorithm>
using namespace std;
const int INF = 100000000;
int team_num[1000];//记录每一节点的权值
int road[1000][1000];//图的邻接矩阵
int dis[1000];
bool isVisited[1000];//标记某一节点是否被访问过
//最短路径的数目
int path_num = 0;
//最大的医疗队的数目
int g_max_team_num = 0;
void initData(){
int i, j;
for ( i = 0; i < 1000; i++)
{
isVisited[i] = false;
dis[i] = INF;
for ( j = 0; j < 1000; j++)
{
road[i][j] = INF;
}
}
}
//单源最短路径算法
void Dijstra(int n, int src, int des){
int i, j;
for ( i = 0; i < n; i++)
{
dis[i] = road[src][i];
}
isVisited[src] = true;
for ( i = 0; i < n-1; i++) //最多循环n-1次就足够了,选n-1个最小值
{
int minDis = INF;
int cur = 0;
for ( j = 0; j < n; j++)
{
if (!isVisited[j] && dis[j] < minDis){
minDis = dis[j];
cur = j;
}
}
if (minDis == INF){ //已经完成了连通路径的遍历。
return;
}
isVisited[cur] = true;
//更新Dis数组的内容.
for ( j = 0; j < n; j++)
{
if (road[cur][j] < INF&&dis[cur] + road[cur][j] < dis[j]){
dis[j] = dis[cur] + road[cur][j];
}
}
}
}
void dfs(int n, int src, int des, int curdis, int curteamNum){
isVisited[src] = true;
if (src == des){
if (curdis == dis[des]){ //找到一条最短路径
path_num++;
if (curteamNum > g_max_team_num){
g_max_team_num = curteamNum;
}
}
return;
}
if (curdis > dis[des]){
return;//当前的路径长度已经超过最短路径,就没有必要继续搜索了。
}
int i;
for ( i = 0; i < n; i++)
{
if (!isVisited[i] && road[src][i] < INF){
dfs(n,i,des,road[src][i]+curdis,curteamNum+team_num[i]);
isVisited[i] = false;
}
}
}
int main()
{
int i,k,j,n,m,c1,c2,src,des;
initData();
cin >> n >> m >> src >> des;
for ( i = 0; i < n; i++)
{
cin >> team_num[i];
}
int t1, t2, w;
for ( i = 0; i < m; i++)
{
cin >> t1 >> t2 >> w;
road[t1][t2] = w;
road[t2][t1] = w;
}
Dijstra(n, src, des);
for ( i = 0; i < n; i++)
{
isVisited[i] = false;
//重置各city的被访问状态
}
dis[src] = 0;
dfs(n, src, des, 0, team_num[src]);
cout << path_num << " " << g_max_team_num << endl;
cin >> i;
}
#include<iostream>
#include <iomanip>
#include<vector>
#include<set>
#include<string>
#include<algorithm>
using namespace std;
const int INF = 100000000;
int team_num[1000];//记录每一节点的权值
int road[1000][1000];//图的邻接矩阵
int dis[1000];
bool isVisited[1000];//标记某一节点是否被访问过
//最短路径的数目
int path_num = 0;
//最大的医疗队的数目
int g_max_team_num = 0;
void initData(){
int i, j;
for ( i = 0; i < 1000; i++)
{
isVisited[i] = false;
dis[i] = INF;
for ( j = 0; j < 1000; j++)
{
road[i][j] = INF;
}
}
}
//单源最短路径算法
void Dijstra(int n, int src, int des){
int i, j;
for ( i = 0; i < n; i++)
{
dis[i] = road[src][i];
}
isVisited[src] = true;
for ( i = 0; i < n-1; i++) //最多循环n-1次就足够了,选n-1个最小值
{
int minDis = INF;
int cur = 0;
for ( j = 0; j < n; j++)
{
if (!isVisited[j] && dis[j] < minDis){
minDis = dis[j];
cur = j;
}
}
if (minDis == INF){ //已经完成了连通路径的遍历。
return;
}
isVisited[cur] = true;
//更新Dis数组的内容.
for ( j = 0; j < n; j++)
{
if (road[cur][j] < INF&&dis[cur] + road[cur][j] < dis[j]){
dis[j] = dis[cur] + road[cur][j];
}
}
}
}
void dfs(int n, int src, int des, int curdis, int curteamNum){
isVisited[src] = true;
if (src == des){
if (curdis == dis[des]){ //找到一条最短路径
path_num++;
if (curteamNum > g_max_team_num){
g_max_team_num = curteamNum;
}
}
return;
}
if (curdis > dis[des]){
return;//当前的路径长度已经超过最短路径,就没有必要继续搜索了。
}
int i;
for ( i = 0; i < n; i++)
{
if (!isVisited[i] && road[src][i] < INF){
dfs(n,i,des,road[src][i]+curdis,curteamNum+team_num[i]);
isVisited[i] = false;
}
}
}
int main()
{
int i,k,j,n,m,c1,c2,src,des;
initData();
cin >> n >> m >> src >> des;
for ( i = 0; i < n; i++)
{
cin >> team_num[i];
}
int t1, t2, w;
for ( i = 0; i < m; i++)
{
cin >> t1 >> t2 >> w;
road[t1][t2] = w;
road[t2][t1] = w;
}
Dijstra(n, src, des);
for ( i = 0; i < n; i++)
{
isVisited[i] = false;
//重置各city的被访问状态
}
dis[src] = 0;
dfs(n, src, des, 0, team_num[src]);
cout << path_num << " " << g_max_team_num << endl;
cin >> i;
}