1003 Emergency (25分)

作者:CHEN, Yue
单位:浙江大学
代码长度限制:16 KB
时间限制:400 ms
内存限制:64 MB
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, C​1 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 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.

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,起点C1,终点C2,每个城市拥有若干救援队,以及给出哪那几个城市想通(c1,c2)相距(l),经过城市救援队汇合,求从城市C1到城市C2的最短路径的个数,并求出最短路径上能拥有最多的救援队个数。

思路:

狄杰斯特拉最短路径问题。套用模板,由于题目要求计算最短路径的个数,需要引入变量num[],初始化为num[st]=1,其余为0,放在最短路径中更新。

参考代码:

#include <cstdio>
#include <iostream>
using namespace std;
const int maxn=501;
const int INF=1e9;
int G[maxn][maxn],a[maxn];
int d[maxn],team[maxn]={0},num[maxn]={0};
bool flag[maxn]={false};
int n,m,C1,C2;
void dij(int st){fill(d,d+maxn,INF);d[st]=0;team[st]=a[st];num[st]=1;for(int i=0;i<n;i++){int u=-1,MIN=INF;for(int j=0;j<n;j++){if(!flag[j]&&d[j]<MIN){u=j;MIN=d[j];}}if(u==-1) return;flag[u]=true;for(int v=0;v<n;v++){if(!flag[v]&&G[u][v]!=INF){if(d[u]+G[u][v]<d[v]){d[v]=d[u]+G[u][v];team[v]=team[u]+a[v];num[v]=num[u];}else if(d[u]+G[u][v]==d[v]){if(team[u]+a[v]>team[v])team[v]=team[u]+a[v];num[v]+=num[u];}}}}
}
int main() {fill(G[0],G[0]+maxn*maxn,INF);scanf("%d%d%d%d",&n,&m,&C1,&C2);for(int i=0;i<n;i++)scanf("%d",&a[i]);int c1,c2,l;for(int i=0;i<m;i++){scanf("%d%d%d",&c1,&c2,&l);G[c1][c2]=l;G[c2][c1]=l;}dij(C1);printf("%d %d\n",num[C2],team[C2]);return 0;
}

如有错误,欢迎指正

【PAT甲级A1003 】Emergency (25分)(c++)相关推荐

  1. 19年冬季第二题 PAT甲级 1166 Summit (25分)

    7-3 Summit (25分) A summit (峰会) is a meeting of heads of state or government. Arranging the rest area ...

  2. PAT 甲级 A1010 Radix (25 分)

    题目传送门 这个题用二分做,我自己写的二分呢太菜了,只能拿到19分,不放出来丢人了.后面看了yxc的代码,美妙绝伦哈. 慢慢来拜读一下. #include "bits/stdc++.h&qu ...

  3. PAT甲级 1032 Sharing (25分) 测试点5陷阱

    题目 1032 Sharing 分析 suffix是后缀,题目的意思是求两个单词的公共后缀的第一个字符的地址.我看有些博客说求的是首个共用结点的地址,我觉得是不对的. 晴神/柳神的解法,是把第一个单词 ...

  4. PAT甲级——1166 Summit (25 分)

    思路: 模拟,用两位vector表示edge相连关系 然后,看是否两两相连和是否有其他点与其都相连 src // 不需要并查 #include<bits/stdc++.h> using n ...

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

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

  6. PAT甲级1147 Heaps (30 分):[C++题解]堆、树的遍历、dfs、完全二叉树建树

    文章目录 题目分析 题目来源 题目分析 来源:acwing 分析:给定完全二叉树,判断是否是堆,需要区分大根堆,小根堆.后面是输出后序遍历. AC代码 #include<bits/stdc++. ...

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

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

  8. 1141 PAT Ranking of Institutions (25 分)

    1141 PAT Ranking of Institutions (25 分) After each PAT, the PAT Center will announce the ranking of ...

  9. PAT甲级 1003 Emergency

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

最新文章

  1. MVC3中的tempdata,viewdata,viewbag总结
  2. 深入理解Spring Redis的使用 (九)、通过Redis 实现 分布式锁 的 BUG,以及和数据库加锁的性能测试...
  3. 超 10000 名开发者在追的技术栏目,你绝不能错过!
  4. 一加6屏幕测试代码_一加 7的普通版与Pro/参数对比
  5. Xshell连接centOS7与CentOS7联网——一步到位
  6. 利用波士顿房价数据集实现房价预测
  7. VMware15 Pro激活密钥
  8. Limesdr软件无线电架构的理解
  9. 电力网络安全监测装置_10月 长沙 电力监控系统安全防护技术研修班
  10. html5鲜花网页代码,JS制作漂亮的鲜花完整代码
  11. NVIDIA GPU Compute Capability解释
  12. Git 核心概念:工作区与暂缓区(添加提交及查看状态充分体现)
  13. Enhancement
  14. 从幂律分布到特征数据概率分布——12个常用概率分布
  15. 五笔打字:速成手册---半小时学会五笔打字
  16. PyGame弹珠游戏双人粗略版
  17. 软件产品易用性评价评估标准
  18. K8S 部署 redis 哨兵集群
  19. 北科大计算机实践报告,计算机应用实践报告_北科大.doc
  20. docx批量转换成html,Batch DOCX to HTML Converter(批量docx转换HTML工具)

热门文章

  1. python的tell和seek_4.2Python文件基本操作2:tell、seek
  2. JavaWeb (SSM框架)
  3. 爬取中国天气网的天气预报,可视化展示看着就是爽【python爬虫入门进阶】(06)
  4. wordpress v3.3.1空间上传php,怎么上传wordpress到虚拟主机
  5. ctab法提取dna流程图_CTAB法提取植物DNA.ppt
  6. 转载分享一批老外的超牛25行代码参赛作品的Flash源文件
  7. VMware虚拟机ping不通主机,Destination Host Unreachable
  8. 如何修复SSD硬盘,如何给SanDisk SSD开卡
  9. json 跟着黑马打的代码 但还是undefined。求解,
  10. Stacked DeBERT