原题传送门 》》

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
结尾无空行

思路:dijkstra算法,板子见文章末尾,这里不解释了。
但是需要额外对路径数量以及队伍数量进行计数。

关键是:当新收录了点u,如何操作才能记录路径数量?记录队伍数量?
做法是:逐个更新经过u的路径uv,查看新的dist[v],假如dist[v]与之前的一样,则v的路径数量加上u的路径,队伍数量取uv的最大值
假如路径更短了,则路径数量和原u相同,队伍数量是u路径的队伍加上v城市的队伍

#include<cstdio>
#include<list>
using namespace std;int N,M,C1,C2;int L[500][500];
int dist[500];//到起点的距离
int troopNums[500];//某城市的队伍数量
int troopOnPath[500];//经过某路径的队伍数量
int pathNums[500];//到达该城市的路线数量
bool collected[500];//城市是否收入S int input(){for(int i = 0; i < 500; ++i){for(int j = 0; j < 500; ++j){L[i][j] = 0x3f3f3f3f;}}fill(dist,dist+500,0x3f3f3f3f);fill(collected,collected+500,false); scanf("%d%d%d%d",&N,&M,&C1,&C2);for(int i = 0; i < N; ++i){scanf("%d",&troopNums[i]);}for(int i = 0; i < M; ++i){int a, b, l;scanf("%d%d%d",&a,&b,&l);L[a][b] = l;L[b][a] = l;}dist[C1] = 0;troopOnPath[C1] = troopNums[C1];pathNums[C1] = 1; return 0;
}int dijk(){while(true){int u = -1;for(int i = 0; i < N; ++i){if(!collected[i]&&(u == -1 || dist[i] < dist[u])){u = i;}}if(u == -1) break;//找到未收录的距离最短的点,如果是-1,则说明没有找到这样的点,算法结束。collected[u] = true;for(int v = 0; v < N; ++v){if(!collected[v] && dist[u]+L[u][v] <= dist[v]){if(dist[u]+L[u][v] == dist[v]) {pathNums[v]+=pathNums[u];troopOnPath[v] = max(troopOnPath[v],troopOnPath[u]+troopNums[v]);}else{pathNums[v] = pathNums[u];troopOnPath[v] = troopOnPath[u]+troopNums[v];}dist[v] = dist[u]+L[u][v];}}}return 0;}int solve(){input();dijk();printf("%d %d\n",pathNums[C2],troopOnPath[C2]);return 0;
}
int main(){solve();return 0;
}

Dijkstra的板子:

const int MAX_V = 100;
const int INF = 0xffffff;
int cost[MAX_V][MAX_V];int d[MAX_V];//记录从出发点到该点的最短距离。bool used[MAX_V];//该点已经被收入S中int V;//顶点数void dijkstra(int s){fill(d,d+V,INF);
fill(used,used+V,false);
d[s] = 0;while(true){int v = -1;
for(int u = 0; u < V; ++u){if(!used[u] && (v == -1||d[u] < d[v]))
v = u;
}//从尚未使用过的点中找到一个距离最小的点 if(v == -1) break;used[v] = true;for(int u = 0; u < V; ++u){d[u] = min(d[u],d[v]+cost[v][u]);//更新v相连的点的值
}
}
}

PTA 1003 Emergency相关推荐

  1. PAT甲级1003 Emergency Dijkstra算法(堆优化版/朴素版)

    前言   最近花了很多的时间在写JAVA项目上面,疏忽了算法和数据结构的学习.最近突然醒悟基础更为重要,打算从今天开始每天抽出一些时间做下PAT甲级的题目.现有题库的前两题很简单,从第三题开始吧. 题 ...

  2. PAT甲级1003 Emergency:[C++题解]dijkstra求最短路、最短路条数

    文章目录 题目分析 题目链接 题目分析 分析:求单源最短路,使用dijkstra()算法. 最短路的条数,和最短路中 人数最多的一条,输出最多人数. 本题点比较少,使用邻接矩阵d[N][N]来存. a ...

  3. PAT(甲级) 1003. Emergency

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

  4. PTA(3)--Emergency

    PTA(3)–Emergency 解答: 1.深度优先搜索: 注意此处不能将对角线初始化为0,因为在没有救援队数量的属性时,自己访问自己对结果没有影响,但本题如果自己访问自己就会使同一个城市的救援队数 ...

  5. PAT 1003 Emergency(最短路(迪杰斯特拉||贝尔曼)最小边权下的最大点权)

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

  6. 1003 Emergency 题目解析

    文章目录 题目描述 题意理解 解题思路 核心算法 完整代码 题目描述 1003 Emergency As an emergency rescue team leader of a city, you ...

  7. PAT甲级 1003 Emergency

    PAT甲级 1003 Emergency As an emergency rescue team leader of a city, you are given a special map of yo ...

  8. 1003 Emergency (25 分)

    1003 Emergency (25 分) 题目大意 n个城市m条路,每个城市有救援小组,所有的边的边权已知.给定起点和终点,求从起点到终点的最短路径条数以及最短路径上的救援小组数目之和.如果有多条就 ...

  9. 1003 Emergency (25 分)【Dijastra与DFS解法】

    立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 As an emergency rescue team leader of a city, you are given a spe ...

最新文章

  1. Tomcat虚拟目录配置
  2. 统信uos系统考试题_148款!富士通及旗下晟拓品牌系列打印机适配统信UOS
  3. hadoop 卡主_HDFS DisTCP执行卡住了,怎么办?
  4. scala 拆分字符串翻转_Scala程序分割字符串
  5. dremwere怎样让多个图片并列排放_国标双壁波纹管直径200、300、400、500、600、800图片展示...
  6. android 开发如何做内存优化
  7. oracle中入库判断空串,不同数据库和SpringDataJPA对字段值null,''空值的判断
  8. mysql查询字段为null的方法
  9. 提问的智慧(转自github)
  10. ToLua(LuaFramework) -ToLua框架使用指南
  11. big mac sur 免驱显卡_macOS Big Sur 系统原生显卡驱动信息表
  12. 一加8 pro 刷入 kali Hunter
  13. 霜降后,宝宝穿衣要做到三捂两不捂
  14. vue实现导出表格数据
  15. 机电revit的【桥架转化】功能,识别CAD图纸点击转化
  16. 数字化具体指的是什么?
  17. 个人开发者越来越难之-----上传应用市场的软著虐我遍体鳞伤
  18. 痞子衡嵌入式:i.MXRT连接特殊Octal Flash时(OPI DTR模式下反转字节序)下载与启动注意事项(以MX25UM51245为例)...
  19. 无痕埋点在Android中的实现
  20. 利用ffmpeg把一帧原始视频数据转换成jpg格式的图片

热门文章

  1. Kerberos安装及拖管Ambari 2.7
  2. K8S专题-基础组件的部署1
  3. Linux命令解释之df
  4. 苹果将投资10亿美元扩容位于美国雷诺的数据中心
  5. [转] Hibernate一级缓存、二级缓存
  6. 解决PyScripter中文乱码问题
  7. java web 路径 .html,java web 路径(java web 路径).doc
  8. softmax回归的从零开始实现-09-p4
  9. mysql switch binlog_TiDB binlog实时同步数据到下游Kafka
  10. win7禁用powershell_简述Windows 7中的Windows PowerShell功能