PAT (Advanced Level) Practice

1003 Emergency

分数 25

作者 CHEN, Yue

单位 浙江大学

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

solution one:Dijkstra算法

#include <iostream>
#include <cstring>
#include <algorithm>using namespace std;const int INF = 0x3fffffff; //无穷大数据
const int maxn=510; //顶点最大值int G[maxn][maxn]; //邻接矩阵
int weight[maxn];  //顶点权值int w[maxn];  // 路径上顶点上筹集的最大物资
int num[maxn]; // 最短路径条数
int d[maxn];  //最短路径
bool hs[maxn]; // 顶点是否被标记int Nv,Ne,st,ed; //顶点数,边数,起点,终点void Read(); //读出数据,并建图
void Dijkstra(int s); //Gijkstra 算法int main()
{fill(*G,*G+maxn*maxn,INF);   //这句话没写,害我调试了半天Read();Dijkstra(st);cout << num[ed] << ' ' << w[ed] << endl;return 0;
}void Read()  //读出数据,并建图
{cin >> Nv >>Ne >> st >> ed;for(int i=0;i<Nv;++i)cin >> weight[i];for(int i=0;i<Ne;++i){int v1,v2,dis;cin >> v1 >> v2 >> dis;G[v1][v2] = G[v2][v1] = dis;}
}void Dijkstra(int s)  //Gijkstra 算法
{fill(d,d+maxn,INF);d[s]=0;w[s]=weight[s];num[s]=1;for(int i=0;i<Nv;++i){int u=-1,MIN=INF;for(int j=0;j<Nv;++j){if(hs[j]==0 && d[j]<MIN){u=j;MIN=d[j];}}if(u==-1)return;hs[u]=1;for(int v=0;v<Nv;++v){if(hs[v]==0 && G[u][v]!=INF && d[u]+G[u][v] < d[v]){d[v]= d[u] +G[u][v];w[v]=w[u]+weight[v];num[v]=num[u];}else if(hs[v]==0 && G[u][v]!=INF && d[u]+G[u][v] == d[v]){if(w[u]+weight[v] > w[v])w[v] = w[u]+weight[v];num[v]+=num[u];}}}
}

solution two:Bellman_Ford算法:

#include <iostream>
#include <cstring>
#include <algorithm>
#include <set>
#include <vector>using namespace std;typedef pair<int, int> PII;const int INF = 0x3fffffff; //无穷大数据
const int maxn=510; //顶点最大值vector<PII> Adj[maxn]; //邻接表
int weight[maxn];  //顶点权值int w[maxn];  // 路径上顶点上筹集的最大物资
int num[maxn]; // 最短路径条数
int d[maxn];  //最短路径
bool hs[maxn]; // 顶点是否被标记
set<int> pre[maxn];int Nv,Ne,st,ed; //顶点数,边数,起点,终点void Read(); //读出数据,并建图
void Dijkstra(int s); //Gijkstra 算法int main()
{Read();Dijkstra(st);cout << num[ed] << ' ' << w[ed] << endl;return 0;
}void Read()  //读出数据,并建图
{cin >> Nv >>Ne >> st >> ed;for(int i=0;i<Nv;++i)cin >> weight[i];for(int i=0;i<Ne;++i){int v1,v2,dis;cin >> v1 >> v2 >> dis;Adj[v1].push_back({v2,dis});Adj[v2].push_back({v1,dis});}
}void Dijkstra(int s)  //Gijkstra 算法
{fill(d,d+maxn,INF);d[s]=0;w[s]=weight[s];num[s]=1;for(int i=1;i<=Nv;++i){for(int u=0;u<Nv;++u){for(int j=0;j<Adj[u].size();++j){int v = Adj[u][j].first,dis = Adj[u][j].second;if(d[u] + dis < d[v]){d[v] = d[u] + dis;num[v] = num[u];w[v] = w[u] + weight[v];pre[v].clear();pre[v].insert(u);}else if(d[u] + dis == d[v]){if(w[u] + weight[v] > w[v])w[v] = w[u] + weight[v];pre[v].insert(u);num[v] = 0;for(auto s:pre[v])num[v] += num[s];}}}}
}

A1003 Emergency相关推荐

  1. 【PAT甲级】A1001-A1050刷题记录

    文章目录 A1001 A+B Format (20 分) 0.25 ★(一元多项式加法) A1002 A+B for Polynomials (25 分) 0.21 (单源最短路Dijkstra+边权 ...

  2. 【置顶】【PAT】PAT甲级题目及分类总结(持续更新ing)

    在2019年3月底,我决定考浙大计院,经过一个月还算凑合的学习,痛定思痛,决定整理整理自己的博客. 粗略估计,大概一个月的时间里我AC了31道题,大概用时40个小时上下,毕竟还要把大部分时间花在笔试上 ...

  3. 【PAT甲级A1003 】Emergency (25分)(c++)

    1003 Emergency (25分) 作者:CHEN, Yue 单位:浙江大学 代码长度限制:16 KB 时间限制:400 ms 内存限制:64 MB As an emergency rescue ...

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

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

  5. Linux 开机显示:welcome to emergency mode

    welcome to emergency mode!after logging in ,type "journalctl -xb" to view system logs,&quo ...

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

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

  7. Err Welcoe to emergency mode

    OS: Linux 7.3 重启后无法登陆图形界面,而是直接以emergency mode进入命令行界面: 这样虽然能用root密码登录,但是startx打不开,网也连不上,重启网卡就又退回到welc ...

  8. 2020牛客国庆集训派对day4 Emergency Evacuation

    Emergency Evacuation 题意: 有n个人在不同的位置上,在最后面有一个出口exit,所有人都要逃离出去(走出出口),且每个格子最多容纳一个人,当有人挡在前面时,后面的人必须停留,所有 ...

  9. 【PAT - 甲级1003】Emergency (25分)(Dijkstra,最短路条数,双权值最短路)

    题干: As an emergency rescue team leader of a city, you are given a special map of your country. The m ...

最新文章

  1. java 闭包与回调
  2. java date.getyear_为什么Java的Date.getYear()返回111而不是2011?
  3. 100分程序员的8个习惯
  4. matlab积分使用
  5. TestNG配合ant脚本进行单元测试
  6. 小程序 web socket_程序员的薪水和发展方向大全
  7. 端口号及对应的服务汇总 (适用于Linux/Windows系统)
  8. Myeclipse 操作技巧
  9. NFS, web,负载均衡,Nginx yum 源码安装
  10. js统计字符串中特定字符出现的个数
  11. 微博登陆python学习
  12. dw2xls已升级至pb11.5
  13. opengles图像处理之边缘检测
  14. 算法策略 | MACD跨周期短线交易策略开发(股指+商品双版)
  15. # ** Error: ../tb/bfm/bfm_tb_ddr3/bfm_tb_ddr3.v(186): Illegal task output argument.
  16. css炫酷标题,炫酷 CSS 背景效果的 10 个代码片段
  17. Android aab打包报错(持续更新中~),android插件开发过时
  18. F - Fairy, the treacherous mailman
  19. php 导入excel 日期格式值处理
  20. 关于台电X16 plus (Tpad)安装win10系统

热门文章

  1. Proteus常用元件对照表
  2. SetupWizard调试技巧
  3. ValueError: Length mismatch: Expected axis has 2 elements, new values have 1 elements
  4. pjsip 设置麦克风语音输入量 扬声器播放
  5. RTK ? PPK ?到底该选啥
  6. 互联网世界的神奇逻辑
  7. 毕业设计-基于深度学习的交通标识识别-opencv
  8. TSDB时序数据库时序数据压缩解压技术浅析
  9. 我奋斗了18年、不是为了和你喝咖啡
  10. IT商界著名公司来历和简介