PAT甲级1087 All Roads Lead to Rome (30分):[C++题解]dijkstra求单源最短路综合、最短路条数、保存路径
文章目录
- 题目分析
- 题目链接
题目分析
来源:acwing
分析:
首先这是一道dijkstra算法求最短路的题目,不过此题较为复杂。首先需要将字符串城市名映射成数字,这里使用hash table 名为city_number
. 此外,还需要存路径pre[N]
数组,当然本题多的是求最短路的条数,使用cnt[N]
数组; 多的是求幸福度最大,使用sum[N]
数组;多的是统计结点数量,使用num[N]
数组。
ac代码
#include<bits/stdc++.h>
using namespace std;
const int N = 210;unordered_map<string ,int> city_number; //城市名 和 序号映射
string city[N]; // 序号 和城市名映射,用于输出bool st[N];
int w[N];//幸福度权值
int c[N][N];//城市之间的成本
int dist[N];//成本最低 int sum[N];//幸福感
int num[N]; //点数
int cnt[N]; //最短路数量
int pre[N];//保存路径int n , m;
string S; //起点和终点void dijkstra(){memset(dist, 0x3f, sizeof dist);dist[0] = 0 ,cnt[0] =1; num[0]=0,sum[0]=0;for(int i = 0; i< n; i++){int t = -1;//遍历寻找最小成本的点for(int j =0; j< n; j ++)if(!st[j] &&(t == -1 || dist[j]<dist[t]))t= j;st[t] =true;//更新其他点的距离for( int j = 1; j< n; j++){if(dist[j] > dist[t] + c[t][j]){dist[j] = dist[t] + c[t][j];sum[j] = sum[t] +w[j];num[j] =num[t] +1;cnt[j] =cnt[t];pre[j] =t;}else if(dist[j] == dist[t]+ c[t][j]){//成本相同,看幸福度cnt[j]+=cnt[t];if(sum[j]< sum[t] + w[j]){sum[j] = sum[t] +w[j];num[j] = num[t] +1;pre[j] =t;}else if(sum[j] == sum[t] + w[j]){if(num[j] >num[t] + 1){num[j] =num[t] +1;pre[j] =t;}}}}}}int main(){memset(c,0x3f, sizeof c);cin >> n >> m >> city[0];city_number[city[0]] = 0; //起点是0号for(int i = 1; i<n;i++){int content;cin >> city[i] >>content;city_number[city[i]] =i; //从1号开始映射w[i] =content;}while(m--){string c1,c2;int a;cin >> c1 >> c2 >> a;int n1 = city_number[c1],n2 = city_number[c2];c[n1][n2] = c[n2][n1] =min(c[n1][n2], a); //成本}dijkstra();int T = city_number["ROM"]; //终点编号cout<< cnt[T] << ' '<< dist[T] <<" " << sum[T] <<" "<<sum[T]/num[T]<<endl;vector<int> path;for(int i =T; i!=0; i=pre[i]) path.push_back(i); cout<<city[0];for(int i =path.size()-1; i>=0; i--) cout<<"->"<<city[path[i]];}
题目链接
PAT甲级1087 All Roads Lead to Rome (30分)
https://www.acwing.com/problem/content/1579/
PAT甲级1087 All Roads Lead to Rome (30分):[C++题解]dijkstra求单源最短路综合、最短路条数、保存路径相关推荐
- 1087 All Roads Lead to Rome (30 分)
题目链接: 题目详情 - 1087 All Roads Lead to Rome (30 分) (pintia.cn)https://pintia.cn/problem-sets/9948053427 ...
- PAT甲级1030 Travel Plan (30分):[C++题解]dijkstra求单源最短路、保存路径
文章目录 题目分析 题目链接 题目分析 来源:PAT网站 分析 dijkstra模板默写过来,然后多了一个保存路径,使用数组pre[N]记录最短路上每个点的前驱,通过pre数组保存到vector中 v ...
- 【PAT甲级 单源最短路径】1087 All Roads Lead to Rome (30 分)
字符串索引图,使用了map映射,用了一些不常用的知识,能这么写出来很开心 这么写可以使得map的默认值为INF而不是值初始化的0 struct defaultCost{int cost;default ...
- 1087 All Roads Lead to Rome (30 分)【难度: 一般 / Dijkstra】
https://pintia.cn/problem-sets/994805342720868352/problems/994805379664297984 PAT这种类型地题出了很多次了. 思路: 先 ...
- pat 1087. All Roads Lead to Rome (30)
pat 1087. All Roads Lead to Rome (30) 时间限制 200 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN ...
- pat 1087. All Roads Lead to Rome (30) 解答
这题和之前一题紧急救援有点像 这里用的变形的dijkstra 每个节点设置一个vector,遇到相同最短路的,就在vector记录来自哪个节点: 这样 dijkstra结束后,就可以从终点往前推出 所 ...
- PAT甲级1111 Online Map (30分):[C++题解]两次dijkstra求单源最短路、保存路径、长度最短、时间最短
文章目录 题目分析 题目链接 题目分析 来源:acwing 分析:dijkstra求单源最短路的题目. 只是写两遍而已,第一遍求按照路径长度求,第二遍按照时间最少求. 另外加一个vector路径的判断 ...
- PAT甲级1002 All Roads Lead to Rome
All Roads Lead to Rome (30) 时间限制 1000 ms 内存限制 65536 KB 代码长度限制 100 KB 判断程序 Standard (来自 小小) 题目描述 Inde ...
- PAT甲级1151 LCA in a Binary Tree (30 分):[C++题解]LCA、最低公共祖先、哈希表映射
文章目录 题目分析 题目链接 题目分析 来源:acwing 分析: 和下面这道题几乎是同一题:PAT甲级1143 Lowest Common Ancestor (30 分):[C++题解]LCA.最低 ...
最新文章
- 自研海外PCDN系统技术架构与演进
- 《Python Cookbook 3rd》笔记(3.8):分数运算
- 【转载保存】mapreduce优秀文章
- 第12课第2.2节 字符设备驱动程序之LED驱动程序_测试改进
- DWR2学习笔记(一)
- java调用python脚本_调用Python写vb的脚本方法
- SAP License:SAP 系统参数设置
- protocol buffer开发指南
- 拓端tecdat|R语言有极值(EVT)依赖结构的马尔可夫链(MC)对洪水极值分析
- Activity launchMode
- C语言实现贪吃蛇[内附详细步骤]
- PR开场片头模板 恐怖惊悚电影侦探节目logo展示PR模板
- 三菱FX1S PLC控制伺服电机小结
- 无法访问。您可能没有权限使用网络资源。请与这台服务器的管理员联系查明你是否有访问权限。
- Ubuntu中文件颜色的含义
- JDK1.8新特性及常用新特性
- 华为P20 Pro对比iPhone X:谁更能拍出人像高级美?
- ArcGIS的栅格数据空间分析——栅格插值(1)
- 复位的recovery time和removal time
- 爱立信联合SK电讯和宝马进行首次多车辆5G测试
热门文章
- css: line-height 与box-sizing
- Linux服务器网页显示乱码
- linux java mysql 备份 runtime_Linux下mysql定时备份脚本以及java实现
- 【数理知识】《数值分析》李庆扬老师-第6章-解线性方程组的迭代法
- PyTorch 可视化工具 TensorboardX
- 构建根文件系统之busybox(一)浅析
- 雅客EXCEL(5)-tab键、同组数据录入、提取重复值、提取身份证号码的出生日期
- 【PC工具】PhotoScape简单绿色功能强大的照片编辑器
- c语言调用hzk16,C语言使用HZK16显示每个像素的代码
- 【课件】基础雷达信号处理