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, C1 and C1- 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 , c1 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 C1.

Output Specification:

For each test case, print in one line two numbers: the number of different shortest paths between C1 and C1 , 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,出发城市,目的城市

第二行输入为:各个城市医疗队的数量

最后m行每行为:c1 到c1以及权重

求单元最短路径首先应该想到的是Dijkstra算法,单元最短路径不止一条,并且要记录出发城市到目的城市的医疗队数量,可以采用vector进行存储,每个节点的长度代表单元路径条数,值代表医疗队数量。

**城市医疗队分布图**
![](https://img2018.cnblogs.com/blog/1648242/201904/1648242-20190409150812020-819659854.png)
**vector存储结构**
```c++
// 1003_Emergency.cpp : Defines the entry point for the console application.
//

include "stdafx.h"

include

include <string.h>

include

define MaxSize 500

using namespace std;

int Graph[MaxSize][MaxSize]; // 图数组

int vis[MaxSize]={0},dis[MaxSize];

int curNum[MaxSize]={0};

int n,m,cur,des; // n 城市个数,m 为路径,cur 为目前所在城市,des 为目标城市

const int INF=0x7f;

vector vec[MaxSize];

// 输入数据

void Input(){

int i,x,y,z;

cin>>n>>m>>cur>>des;

memset(Graph,-1,sizeof(Graph));

for(i=0;i<n;i++)

dis[i]=MaxSize;

// 每个城市的救援队数量

for(i=0;i<n;i++)

cin>>curNum[i];

// 输入图

for(i=0;i<m;i++){

cin>>x>>y>>z;

Graph[x][y]=z;

Graph[y][x]=z;

}

}

void push(int target,int dataid)

{

for(int i=0;i<vec[dataid].size();i++)

// 当前城市医疗队数量+前一节点医疗队数量

vec[target].push_back(vec[dataid][i]+curNum[target]);

}

void Dijkstra(int cur,int des){

int i,Min;

int cen=cur;

vis[cur]=1; // 已访问

vec[cur].push_back(curNum[cur]);

dis[cur]=0;

while(true){

// 找到相邻节点 更新距离

for(i=0;i<n;i++){

if(vis[i]0&&Graph[cen][i]!=-1){

if(Graph[cen][i]+dis[cen]<dis[i]){

dis[i]=Graph[cen][i]+dis[cen];

vec[i].clear(); // 清除当前城市数据节点

push(i,cen);

}else if(Graph[cen][i]+dis[cen]dis[i]){

push(i,cen);

}

}

}

// 找到下一节点

Min=INF;

for(i=0;i<n;i++){

if(vis[i]0&&dis[i]<Min){

Min=dis[i];

cen=i;

}

}

vis[cen]=1;

if(cendes)

break;

}

}

int main(int argc, char* argv[])

{

Input();

Dijkstra(cur,des);

vector desVec=vec[des];

cout<<desVec.size()<<' ';

// 找单元最短路径最大医疗队数量

int MaxValue=desVec[0];

for(int i=1;i<desVec.size();i++){

if(MaxValue<desVec[i]){

MaxValue=desVec[i];

}

}

cout<<MaxValue << endl;

return 0;

}

**<center>2020考研打卡第一天,你可知道星辰之变,骄阳岂是终点?千万不要小看一个人的决心。加油!!!</center>**
**<center>人生为棋,我愿为卒,行动虽慢,可谁也没见我后退一步。</center>**

PAT-1003 Emergency(Dijkstra)的更多相关文章

  1. 1003 Emergency (25 分)

    1003 Emergency (25 分) As an emergency rescue team leader of a city, you are given a special map of y ...

  2. 迪杰斯特拉(dijkstra)算法的简要理解和c语言实现(源码)

    迪杰斯特拉(dijkstra)算法:求最短路径的算法,数据结构课程中学习的内容. 1 . 理解 算法思想::设G=(V,E)是一个带权有向图,把图中顶点集合V分成两组,第一组为已求出最短路径的顶点集合 ...

  3. 最短路径之迪杰斯特拉(Dijkstra)算法

    迪杰斯特拉(Dijkstra)算法主要是针对没有负值的有向图,求解其中的单一起点到其他顶点的最短路径算法.本文主要总结迪杰斯特拉(Dijkstra)算法的原理和算法流程,最后通过程序实现在一个带权值的 ...

  4. 理解最短路径——迪杰斯特拉(dijkstra)算法

    原址地址:http://ibupu.link/?id=29 1.       迪杰斯特拉算法简介 迪杰斯特拉(dijkstra)算法是典型的用来解决最短路径的算法,也是很多教程中的范例,由荷兰计算机科 ...

  5. PAT 1003 Emergency

    1003 Emergency (25 分)   As an emergency rescue team leader of a city, you are given a special map of ...

  6. 图论——迪杰斯特拉算法(Dijkstra)实现,leetcode

    迪杰斯特拉算法(Dijkstra):求一点到另外一点的最短距离 两种实现方法: 邻接矩阵,时间复杂度O(n^2) 邻接表+优先队列,时间复杂度O(mlogn)(适用于稀疏图) (n:图的节点数,m:图 ...

  7. 算法-迪杰斯特拉算法(dijkstra)-最短路径

    迪杰斯特拉算法(dijkstra)-最短路径 简介: 迪杰斯特拉算法是由荷兰计算机科学家狄克斯特拉于1959 年提出的,因此又叫狄克斯特拉算法.是从一个顶点到其余各顶点的最短路径算法,解决的是有向图中 ...

  8. 数据结构与算法——迪杰斯特拉(Dijkstra)算法

    tip:这个算法真的很难讲解,有些地方只能意会了,多思考多看几遍还是可以弄懂的. 应用场景-最短路径问题 战争时期,胜利乡有 7 个村庄 (A, B, C, D, E, F, G) ,现在有六个邮差, ...

  9. PAT (Advanced Level) Practise 1003 Emergency(SPFA+DFS)

    1003. Emergency (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue As an emerg ...

随机推荐

  1. a.c:5:5: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result [-Wun

    PTA做题时出现的错误,用if括起来就没有了. if(scanf("%d",&a)){}; 其实并不是这里有问题,如果你的输出有问题,他就会鸡蛋里挑骨头的先显示这个错误.

  2. 2.1Python基础语法(一)之注释与数据类型:

    返回总目录 目录: 1.注释 2.乱码 3.变量 4.数据类型 5.数据的转换 6.动态,静态和强类型,弱类型 (一)注释:(编译时是被编译器忽略的) 1.注释的分类: 2.特殊注释: (二)乱码:( ...

  3. 小渣渣的json和jsonp和ajax的实质和区别

    json和jsonp和ajax的实质和区别ajax的两个问题 1.ajax以何种格式来交换数据 2.跨域的需求如何解决 数据跨域用自定义字符串或者用XML来描述 跨域可以用服务器代理来解决jsonp来 ...

  4. 团队作业——Alpha冲刺 10/12

    团队作业--Alpha冲刺 冲刺任务安排 杨光海天 今日任务:编辑界面返回以及预览的intent实现 明日任务:未能按时间要求完成的失败总结 郭剑南 今日任务:优化图像预处理所有功能的函数代码,尝试与 ...

  5. Android 生态消息推送平台介绍

    一.手机厂商平台 华为消息推送服务 华为推送(Push)是为开发者提供的消息推送平台,建立了从云端到手机端的消息推送通道,使应用可以将最新信息及时通知用户,从而构筑良好的用户关系,提升用户的感知和活跃 ...

  6. 【转】开篇python--明白python文件如何组织,理解建立源文件

    在Python 中引用是非常简单的事情,这里需要清楚三个概念就可以了包.模块.类.类这个就不用说了. 模块对应的是一个.py 文件,那么module_name 就是这个文件去掉.py 之后的文件名,p ...

  7. 设置webstorm支持ES6语法

    1.  点击File目录下的Default Settings 2.  再依次点击Languages & Frameworks  ----->  JaveScript  ----> ...

  8. [测试] Markdown+Latex

    标题 标题 标题 标题 标题 标题 #include <cstdio> #define R register int #define I inline void #define IL in ...

  9. [国家集训队]Tree II

    嘟嘟嘟 这道题其实还是挺基础的,只不过操作有点多. 区间乘和区间加按线段树的方式想. 那么就先要下放乘标记,再下放加标记.但这两个和反转标记是没有先后顺序的. 对于区间加,sum加的是区间长度\(*\ ...

  10. gatewayworker中使用tcp协议连接硬件设备获取数据报错解决办法!

    运行后过段时间报错, Warning: Error while sending STMT_PREPARE packet. PID=1776 in D:\phpStudy\WWW\api\mysql-m ...