ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

PAT 1003 Emergency (25 分)(图,Dijkstra算法)

2021-02-21 18:29:16  阅读:213  来源: 互联网

标签:25 PAT Emergency int 路径 maxn each line cities


一、题目描述

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

三、输出

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.

输入样例

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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有