【PAT甲】1003 Emergency (25分)(Dijkstra求最短路条数及最大点权和)

举报
小哈里 发表于 2022/05/10 23:22:03 2022/05/10
【摘要】 problem 1003 Emergency (25分) As an emergency rescue team leader of a city, you are given a special ma...

problem

1003 Emergency (25分)
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条边(有点权和边权),从c1出发到c2
  • 求最短路条数,及相应的最大点权和

solution

  • Dijkstra的状态和转移:dis[i] 表示从出发点到i结点最短路径的路径长度(初始为0和一堆正无穷),每次我们找到未标记的点中dis最小的,将它打上标记并遍历其他点且当dis[u]+e[u][v]<dis[v]更新dis[v]值。
  • 统计最短路条数:num[i]表示从出发点到i结点最短路径的条数(初始为1和一堆0),当找到更短的路时(即dis值需要更新时),令num[v] = num[u],或者找到恰好相等的路时,num[v] += num[u]。
  • 统计最大点权,w[i]表示从出发点到i点的点权和,当遇到更短的路时,把权值加上相应的点权,当找到恰好相等的路时,选择最大的点权加上。
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;

int n, m, c1, c2;
int e[510][510], weight[510];
int dis[510], num[510], w[510], vis[510];
const int inf = 99999999;

int main(){
	cin>>n>>m>>c1>>c2;
	for(int i = 0; i < n; i++)
		cin>>weight[i];
	memset(e,0x3f,sizeof(e));
	memset(dis,0x3f,sizeof(dis));
	for(int i = 0; i < m; i++){
		int a, b, c;
		cin>>a>>b>>c;
		e[a][b] = e[b][a] = c;
	}
	dis[c1] = 0;
	num[c1] = 1;
	w[c1] = weight[c1];
	for(int i = 0; i < n; i++){
		int u = -1, minn = inf;
		for(int j = 0; j < n; j++){
			if(!vis[j] && dis[j]<minn){
				u = j;
				minn = dis[j];
			}
		}
		if(u==-1)break;
		vis[u] = 1;
		for(int v = 0; v < n; v++){
			if(!vis[v] && e[u][v]!=inf){
				if(dis[u]+e[u][v]<dis[v]){
					dis[v] = dis[u]+e[u][v];
					num[v] = num[u];
					w[v] = w[u]+weight[v];
				}else if(dis[u]+e[u][v]==dis[v]){
					num[v] = num[v]+num[u];
					w[v] = max(w[v], w[u]+weight[v]);
				}
			}
		}
	}
	cout<<num[c2]<<" "<<w[c2];
	return 0;
}



  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52

文章来源: gwj1314.blog.csdn.net,作者:小哈里,版权归原作者所有,如需转载,请联系作者。

原文链接:gwj1314.blog.csdn.net/article/details/108410132

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。