PTA甲级 1003 Emergency (Dijkstra算法)

PTA甲级

1003 Emergency

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, 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 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

题目大意:
每个城市有不同数量的救援队,两个城市之间有一条长度为L的路,每个城市的救援队会随大部队到来而加入大部队.
输出C1到C2的最短路径条数和路途上可以聚集的最大救援队的数量.
本题主要考察对Dijkstra算法的使用,同时还要记录最短路条数和考虑点权的问题(每个城市的救援队).
(关于Dijkstra可以参考我的另一篇文章,Dijkstra算法)
(具体细节和解释已经添加到注释中)

#include<iostream>
using namespace std;
#define inf 99999999
int map[510][510];//储存边权 
int t[510]={0};//储存点权 
int visit[510]={0};//是否录入 
int dist[510];//储存最短路长度 
int weight[510]={0};//储存最多点权
int num[510]={0};//储存到达每个点的最短路数量
int main()
{
	int a,b,c,v;
	int n,m,c1,c2;
	cin>>n>>m>>c1>>c2;
	for(int i=0;i<n;i++) 
	{
		cin>>t[i];
	}
	for(int i=0;i<n;i++)//初始化map 
	for(int j=0;j<n;j++)
	map[i][j]=inf;
	for(int i=0;i<m;i++)
	{
		cin>>a>>b>>c;
		map[a][b]=c;
		map[b][a]=c;
	}
	v=c1;
	num[v]=1;//设置初始的num
	weight[v]=t[v];//设置初始的weight
	dist[v]=0;
	visit[v]=1;//录入
	for(int i=0;i<n;i++)//初始化数据
	{
		dist[i]=map[v][i];
		num[i]=num[v];
		if(i!=v)
		weight[i]=weight[v]+t[i];//更新每个点的最大救援队数量
	}
	while(1)
	{
		int min=inf;
		v=-1;
		for(int i=0;i<n;i++)//寻找未被录入且dist最小的点 
		{
			if(min>dist[i]&&visit[i]==0)
			{
				min=dist[i];
				v=i;
			}
		}
		if(v==-1||v==c2)//无符合的点或者已经到达c2,结束 
		break;
		visit[v]=1;
		for(int i=0;i<n;i++)//更新dist,num,weight 
		{
			if(visit[i]==0&&map[v][i]!=inf)//未被录入且与v邻接 
			{
				if(dist[i]>dist[v]+map[v][i])//dist会变小 
				{
					dist[i]=dist[v]+map[v][i];//修改dist 
					num[i]=num[v];//更新num 
					weight[i]=weight[v]+t[i];//更新每个点的最大救援队数量
				}else if(dist[i]==dist[v]+map[v][i])//num增加 
				{
					num[i]+=num[v];//dist[v]+map[v][i]也是最短路径,所以num[i]也是最短路数量. 
					if(weight[i]<weight[v]+t[i])//相同路长度,优先weight大的. 
					weight[i]=weight[v]+t[i];
				}
			}
		}
	}
	cout<<num[c2]<<" "<<weight[c2];
	return 0;
}

如果文中有什么错误或模糊的地方欢迎评论指出.

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值