PAT 1003 Emergency (25 分)(图,Dijkstra算法)
作者:互联网
一、题目描述
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.
二、输入
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.
三、输出
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.
输入样例
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
输出样例
2 4
四、思路
Dijkstra算法练手题,除了求最短路径,还要求在路径最短的条件下能带去的最大人手。另外,由于最短路径不唯一,需要给每个节点开一个前导路径的数组,方便最后统计路径数量。
#include<iostream>
#include<vector>
using namespace std;
const int maxn = 510;
const int INF = 0x3fffffff;
int n,m,s,e,a,b,w,Count = 0,G[maxn][maxn],d[maxn],peoples[maxn] = {0},visited[maxn] = {0},hands[maxn];
//前导路径数组
vector<int> paths[maxn];
void Dijkstra(){
d[s] = 0;
paths[s].push_back(s);
peoples[s] = hands[s];
for(int i = 0;i<n;i++){
int u = -1,min = INF;
for(int j = 0;j<n;j++){
if(visited[j] == 0 && d[j] < min){
u = j;
min = d[j];
}
}
//如果已找到终点,则返回
if(u == e) return;
visited[u] = 1;
for(int j = 0;j<n;j++){
if(visited[j] == 0 && G[u][j] != INF){
//如果路径更短,则直接更新人手,路径,距离
if(d[u]+G[u][j] < d[j]){
paths[j].clear();
paths[j].push_back(u);
d[j] = d[u] + G[u][j];
peoples[j] = peoples[u]+hands[j];
}
//如果路径一样长,则把路径加一,人手按多的更新
else if(d[u]+G[u][j] == d[j]){
paths[j].push_back(u);
if(peoples[u]+hands[j]>peoples[j]){
peoples[j] = peoples[u]+hands[j];
}
}
}
}
}
}
//计算最短路径的条数
void calcupaths(int index){
if(index == s){
Count++;
return;
}
for(int i = 0;i<paths[index].size();i++){
calcupaths(paths[index][i]);
}
}
int main(){
for(int i = 0;i<maxn;i++) d[i] = INF;
for(int i = 0;i<maxn;i++)
for(int j = 0;j<maxn;j++)
G[i][j] = INF;
scanf("%d %d %d %d",&n,&m,&s,&e);
for(int i = 0;i<n;i++) scanf("%d",&hands[i]);
for(int i = 0;i<m;i++){
scanf("%d %d %d",&a,&b,&w);
G[a][b] = G[b][a] = w;
}
Dijkstra();
calcupaths(e);
printf("%d %d\n",Count,peoples[e]);
return 0;
}
标签:25,PAT,Emergency,int,路径,maxn,each,line,cities 来源: https://blog.csdn.net/weixin_43347688/article/details/113921448