PAT (Advanced Level) Practice — 1003 Emergency (25 分)

题目链接:https://pintia.cn/problem-sets/994805342720868352/problems/994805523835109376

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, C​1​​ and C​2​​ - 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 c​1​​, c​2​​ 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 C​1​​ to C​2​​.

Output Specification:

For each test case, print in one line two numbers: the number of different shortest paths between C​1​​ and C​2​​, 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

:题意 n个城市,m条可走的道路,每个城市有weight[i]个救援队,求城市st到城市en的最短路径的个数以及在最短路径情况下救援小组之和的最大值(即点权值最大)

:思路  dijkstra算法

e[u][v]表示从城市u到城市v的距离,dis[i]表示到城市i的距离,num[i]表示到城市 i 的最短路径的个数,

ans[i]表示到城市 i 救援小组数量之和,vis[i] 标记城市 i 有没有走过

在更新时 当dis[v]>dis[u]+e[u][v]时,不仅要更新dis[v],也要更新 num[v]=num[u]  ans[v]=ans[u]+weight[v]

当dis[v]==dis[u]+e[u][v],也要更新num[v]+=num[u],判断救援队数量ans[v]是否小于ans[u]+weight[v],如果小于就更新weight[v]

 

#include
#include
using namespace std;
const int inf=9999999;
int n,m,u,v,w,st,en;
int dis[511],num[511],ans[511];
int e[511][511];
int weight[511];
bool vis[511];

void dijkstra(){
	for(int i=0;idis[u]+e[u][v]){
					dis[v]=dis[u]+e[u][v];
					num[v]=num[u];
					ans[v]=ans[u]+weight[v];
				}else if(dis[v]==dis[u]+e[u][v]){
					num[v]=num[v]+num[u];
					if(ans[u]+weight[v]>ans[v]){
						ans[v]=ans[u]+weight[v];
					}
				}
			}
		}
	}	
}
int main(){
	while(cin>>n>>m>>st>>en){
		for(int i=0;i>weight[i];
		}
		for(int i=0;i>u>>v>>w;
			e[u][v]=e[v][u]=w;
		}
		for(int i=0;i

 

 PAT (Advanced Level) Practice — 1003 Emergency (25 分)_第1张图片

 

你可能感兴趣的:(PAT-甲级,#,最短路径)