1003. Emergency (25)

原创 2017年01月03日 20:35:39

pat

这题大概意思是,众多城市点,然后某一个城市发生火灾,求你所在城市到目标城市的最短路径,并且能够赶到目标城市的最多消防员,

也就是最短路径上涉及所有的消防员之和。

解题心得:

  1、Dijkstra算法,就是求源点到目标点最短路径。这个算法有点忘,看了书,后来才回想起来。

2、dijkstra算法的意思,就是把点分为两个集合,一个集合是已经算出最短路径,另一个就是待算的,

每次从已经求到的最短路径集合里面找出最短,再去和未知集合比较,从而更改源点到目标点的距离,

将求得的点标记好,放入到已经求成功的集合。

3、map[myStart][myEnd] = map[myEnd][myStart] = myLength;  就是这一句,一开始理解为有向图,

以为只有一个方向的,然后没有把后者加上。

代码如下:

#include"stdio.h"
#include"string.h"
#include"math.h"
#include<iostream>
#include<vector>
using namespace std;
#define INF 0x7FFFFFFF
struct city{
	int roadNum;
	int emergencyNum;
	int distance;
};
vector<int> team;
vector<bool> visit;
vector<vector<int>> map;
vector<struct city> cities;

int countShortest(){
	int min = INF;
	int index = -1;
	for(int i = 0;i < cities.size();i++){
		if(visit[i] == false && cities[i].distance < min){
			min = cities[i].distance;
			index = i;
		}
	}
	return index;
}

void digistra(int start,int end){
	//initialization
	for(int i = 0;i < cities.size();i++){
		cities[i].distance = INF;
		cities[i].emergencyNum = 0;
		cities[i].roadNum = 0;
	}
	cities[start].distance = 0;
	cities[start].emergencyNum = team[start];
	cities[start].roadNum = 1;

	//monify the shortest ways of each point
	while(true){
		int index = countShortest();
		if(index == -1){
			return;
		}
		visit[index] = true;
		if( index == end ){
			return;
		}
		for(int i = 0;i < cities.size();i++){
			if(visit[i] || map[index][i] == INF){
				continue;
			}
			//modify
			if(cities[i].distance > cities[index].distance + map[index][i]){
				cities[i].distance =  cities[index].distance + map[index][i];
				cities[i].emergencyNum = cities[index].emergencyNum + team[i];
				cities[i].roadNum = cities[index].roadNum;
			}else if(cities[i].distance == cities[index].distance + map[index][i]){
				cities[i].roadNum += cities[index].roadNum;
				if(cities[i].emergencyNum < cities[index].emergencyNum + team[i]){
					cities[i].emergencyNum = cities[index].emergencyNum + team[i];
				}
			}
		}

	}
}

int main(){
	int cityNum;
	while(scanf("%d",&cityNum) != EOF){
		int roadNum,startPoint,endPoint;
		scanf("%d%d%d",&roadNum,&startPoint,&endPoint);
		team.resize(cityNum);

		visit.resize(cityNum);
		visit.assign(cityNum,false);
		
		map.resize(cityNum);

		for(int i = 0;i < cityNum;i++)
		map[i].assign(cityNum,INF);
		
		cities.resize(cityNum);

		for(int i = 0;i < cityNum;i++){
			scanf("%d",&team[i]);
		}

		for(int i = 0;i < roadNum;i++){
			int myStart,myEnd,myLength;
			scanf("%d%d%d",&myStart,&myEnd,&myLength);
			if(map[myStart][myEnd] > myLength){     //get the shortest
				map[myStart][myEnd] = map[myEnd][myStart] = myLength;
			}
		}
		digistra(startPoint,endPoint);
		printf("%d %d",cities[endPoint].roadNum,cities[endPoint].emergencyNum);
	}
	return 0;
}


版权声明:版权声明:本博客所有的原创文章,转载请注明出处,作者皆保留版权。

相关文章推荐

1003. Emergency (25)

1003. Emergency (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 ...

Pat甲级1003. Emergency (25)

这道题是一个最短路径问题 题目链接https://www.patest.cn/contests/pat-a-practise/1003 1003. Emergency (25) ...

【PAT】1003. Emergency (25)

1. 题目地址:http://www.patest.cn/contests/pat-a-practise/10032.分析:本题要求输出特定俩点间最短路径的条数和最短路径中能call up的最多救援队...

1003. Emergency (25)

提交情况: #include #include using namespace std; #define Max 510 #define INF 0x3f3f3f3f //Graph为邻接矩阵...

1003. Emergency (25)[dfs]

1. 原题:https://www.patest.cn/contests/pat-a-practise/1003 2. 思路: 题意:给出一个无向带权图,求某两点间的最短路径条数。 思路: 单...

1003. Emergency (25)

As an emergency rescue team leader of a city, you are given a special map of your country. The map s...

1003. Emergency (25)

PAT1003解题报告,主要涉及到图的回溯遍历

【PAT Advanced Level】1003. Emergency (25)

利用深度优先搜索获得最短路径以及最多的支援。 看到有人是先用Dijstra算法获得最短路径,再用DFS求得最短路径的条数,这个以后再实现

1003. Emergency (25)

As an emergency rescue team leader of a city, you are given a special map of your country. The map s...

1003. Emergency (25)——PAT (Advanced Level) Practise

题目信息: 1003. Emergency (25) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standa...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)