# 1003. Emergency (25)

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 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[start].distance = 0;
cities[start].emergencyNum = team[start];

//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];
}else if(cities[i].distance == cities[index].distance + map[index][i]){
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){
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);
}
return 0;
}

#### 【C++】1003. Emergency (25)*

2014-03-18 15:14:58

#### 【PAT 1003】 Emergency 图论Dijkstra

2014-02-18 22:37:20

#### pat1003Emergency (25)

2015-10-02 13:12:01

#### PAT 1003. Emergency (25) （求两点间最短路的条数）

2014-03-18 12:19:12

#### 1003. Emergency (25)-PAT甲级真题（Dijkstra算法）

2016-08-24 15:11:15

#### PAT甲级 1003.Emergency(25) 题目翻译与答案

2017-02-18 18:09:51

#### 1003. Emergency (25)(java版)

2017-11-22 19:59:48

#### Emergency (dijkstra 变形）

2017-03-22 16:34:50

#### 【再认真理解】1003. Emergency (25) Dijastra最优路径算法修改版求解

2015-07-09 22:45:03

#### 【PAT】Emergency（最短路条数-SPFA）

2016-05-07 17:55:38