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, c2and 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
思路:本题目根据邻接矩阵,利用dijkstra算法求最短路径的题。
需要解决:
- dijkstra算法
- 多条最短路径,选择路径中,节点和最大的一条
- 遍历路径
dijkstra算法图文讲解参考:https://blog.csdn.net/lbperfect123/article/details/84281300
多条最短路径,要对访问的前一个节点进行记录,然后用DFS遍历路径,求得所有节点和最大的路径。
#include <iostream>
#include <stdio.h>
#include <vector>
using namespace std;
int n, m, c1, c2;
const int inf = 9999999;
int num[501];
vector<vector<int> > pre_city(501);//记录每个节点的前一个节点,用DFS遍历可得到路径
vector<int> tmp_path;//访问路径
int e[501][501];//邻接矩阵
int d[501];/*路径最小值*/
int visited[501] = {0};
int num_path = 0, maxx = 0;
void input()
{
cin >> n >> m >> c1 >> c2;
for(int i = 0; i < n; i++)
{
cin >> num[i];
}
for(int i = 0; i < m; i++)
{
int x,y,w;
cin >> x >> y >> w;
e[x][y] = e[y][x] = w;
}
//初始化对角线为0
for(int i = 0; i < n; i++)
e[i][i] = 0;
}
void DFS(int start, int ends)
{
tmp_path.push_back(start);
if(start == ends)
{
num_path++;
int sum = 0;
for(int i = tmp_path.size() - 1; i >= 0; i--)
{
sum += num[tmp_path[i]];
}
if(sum > maxx)
maxx = sum;
tmp_path.pop_back();
return;
}
for(int i = 0; i < pre_city[start].size(); i++)
DFS(pre_city[start][i], ends);
tmp_path.pop_back();
}
void dijkstra_path()
{
for(int i = 0; i < n; i++)
{
if(e[c1][i] != inf)
{
d[i] = e[c1][i];
pre_city[i].push_back(c1);
}
}
visited[c1] = 1;
for(int i = 0; i < n; i++)
{
/*for1, 查找路径最小值*/
int minn = inf, k;
for(int j = 0; j < n; j++)
{
if(!visited[j] && d[j] < minn)
{
minn = d[j];
k = j;
}
}
visited[k] = 1;
/*for2, 中间点k到未找过的边集v,是否更小*/
for(int v = 0; v < n; v++)
{
if(!visited[v] && d[v] > d[k] + e[k][v])
{
d[v] = d[k] + e[k][v];
pre_city[v].clear();
pre_city[v].push_back(k);
}
else if(!visited[v] && d[v] == d[k] + e[k][v])
pre_city[v].push_back(k);
}
}
DFS(c2, c1);
}
void output()
{
cout << num_path << " " << maxx;
}
int main()
{
/*1003 Emergency (25 分)*/
fill(num, num+501, 0);
fill(e[0], e[0]+501*501, inf);
fill(d, d+501, inf);
input();
dijkstra_path();
output();
return 0;
}