题目链接:

题目详情 - 1087 All Roads Lead to Rome (30 分) (pintia.cn)https://pintia.cn/problem-sets/994805342720868352/problems/994805379664297984

题目描述:

Indeed there are many different tourist routes from our city to Rome. You are supposed to find your clients the route with the least cost while gaining the most happiness.

Input Specification:

Each input file contains one test case. For each case, the first line contains 2 positive integers N (2≤N≤200), the number of cities, and K, the total number of routes between pairs of cities; followed by the name of the starting city. The next N−1 lines each gives the name of a city and an integer that represents the happiness one can gain from that city, except the starting city. Then K lines follow, each describes a route between two cities in the format City1 City2 Cost. Here the name of a city is a string of 3 capital English letters, and the destination is always ROM which represents Rome.

Output Specification:

For each test case, we are supposed to find the route with the least cost. If such a route is not unique, the one with the maximum happiness will be recommanded. If such a route is still not unique, then we output the one with the maximum average happiness -- it is guaranteed by the judge that such a solution exists and is unique.

Hence in the first line of output, you must print 4 numbers: the number of different routes with the least cost, the cost, the happiness, and the average happiness (take the integer part only) of the recommanded route. Then in the next line, you are supposed to print the route in the format City1->City2->...->ROM.

Sample Input:

6 7 HZH
ROM 100
PKN 40
GDN 55
PRS 95
BLN 80
ROM GDN 1
BLN ROM 1
HZH PKN 1
PRS ROM 2
BLN HZH 2
PKN GDN 1
HZH PRS 1

Sample Output:

3 3 195 97
HZH->PRS->ROM

思路:

1. 最短路径的一道题,Dijkstra+DFS, Dijkstra求最短路,DFS打印最短路。

2. 给出起点和终点(终点就是ROM),求最短路,因为城市都是字符串,所以要用map映射为数字方便处理(当然还要做数字到字符串的映射)

3. 求最短路(题中是最小花费)的同时还要求最大的幸福值,所以如果最短路条数不唯一,则还需要看当前路径是否幸福值更大。

4. 因为要打印出最短路径,所以要将S到达ROM的前序路径都保存下来,再通过DFS深度遍历输出路径。

代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn = 202, inf = 0x3f3f3f3f;
// G邻接矩阵,d最小花费,hapi幸福值,num最短路数量,wt最大幸福值,pre保存前序路径
int G[maxn][maxn], d[maxn], hapi[maxn], num[maxn], wt[maxn], pre[maxn];
int n, k, cost, des = 1; // n个城市,k条路,cost花费,des目的地(起点为1)
bool vis[maxn]; // 当前城市是否访问过
vector<string> path; // 保存最终路径
// 将城市名称和数字序列对应
map<string, int> StoI;
map<int, string> ItoS;void Dijkstra(int s){fill(d, d+maxn, inf);    // 初始化d d[s] = 0;  // 到自身的最小花费为0num[s] = 1; // // 到自身的路径只有1条wt[s] = 0; // 到自身的花费为0for(int i = 0; i < n; i++){ // 对n城市都进行一次dijkstraint MIN = inf, u = -1; for(int j = 1; j <= n; j++){ // 查找未访问过的城市中花费最小的if(!vis[j] && d[j] < MIN){MIN = d[j];u = j;}}if(u == -1) return; // 未找到直接返回vis[u] = true; // 标记当前城市已访问for(int j = 1; j <= n; j++){ // 遍历n个城市if(!vis[j] && G[u][j]){ // 能直接到达且未访问过的城市if(d[u] + G[u][j] < d[j]){ // 如果当前路径到达的最小花费更小d[j] = d[u] + G[u][j]; // 更新最小花费路径wt[j] = wt[u] + hapi[j]; // 更新最大幸福值num[j] = num[u]; // 最小花费条数不变pre[j] = u; // 更新前序}else if(d[u] + G[u][j] == d[j]) { // 如果最小花费的路径不唯一num[j] += num[u]; // 最小花费的路径条数累加          if(wt[j] < wt[u] + hapi[j]){ // 如果当前路径最大幸福值更大wt[j] = wt[u] + hapi[j]; //更新最大幸福值pre[j] = u; // 更新前序}} }}}
}void DFS(int d){ // 从终点开始递归if(d == 1){ // 如果到达起点path.push_back(ItoS[d]); // 添加城市return;}DFS(pre[d]); // // 否则递归查找前序路径path.push_back(ItoS[d]); // 添加经过的城市
}int main(){string city; cin >> n >> k >> city; //输入StoI[city] = 1; //起点城市为1ItoS[1] = city;for(int i = 2; i <= n; i++){cin >> city >> hapi[i];StoI[city] = i; //城市名称和数组对应ItoS[i] = city;if(city == "ROM") des = i; // ROM为目的地}for(int i = 0; i < k; i++){string s1, s2; // 城市名称int v1, v2; // 城市序列cin >> s1 >> s2 >> cost;v1 = StoI[s1];v2 = StoI[s2];G[v1][v2] = G[v2][v1] = cost; //赋值v1到v2的花费}Dijkstra(1);DFS(des);printf("%d %d %d %d\n", num[des], d[des], wt[des], wt[des]/(path.size()-1));for(int i = 0; i < path.size(); i++){ //打印路径if(i != 0) printf("->");cout << path[i];}return 0;
} 

总结:

这题就是将城市序号变成了字符串, 用map映射之后就是一个经典的Dijkstra+DFS的最短路题目,代码基本都是模板,细心点的你一定可以做对。

1087 All Roads Lead to Rome (30 分)相关推荐

  1. PAT甲级1087 All Roads Lead to Rome (30分):[C++题解]dijkstra求单源最短路综合、最短路条数、保存路径

    文章目录 题目分析 题目链接 题目分析 来源:acwing 分析: 首先这是一道dijkstra算法求最短路的题目,不过此题较为复杂.首先需要将字符串城市名映射成数字,这里使用hash table 名 ...

  2. 1087 All Roads Lead to Rome (30 分)【难度: 一般 / Dijkstra】

    https://pintia.cn/problem-sets/994805342720868352/problems/994805379664297984 PAT这种类型地题出了很多次了. 思路: 先 ...

  3. 【PAT甲级 单源最短路径】1087 All Roads Lead to Rome (30 分)

    字符串索引图,使用了map映射,用了一些不常用的知识,能这么写出来很开心 这么写可以使得map的默认值为INF而不是值初始化的0 struct defaultCost{int cost;default ...

  4. pat 1087. All Roads Lead to Rome (30)

    pat 1087. All Roads Lead to Rome (30) 时间限制 200 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN ...

  5. pat 1087. All Roads Lead to Rome (30) 解答

    这题和之前一题紧急救援有点像 这里用的变形的dijkstra 每个节点设置一个vector,遇到相同最短路的,就在vector记录来自哪个节点: 这样 dijkstra结束后,就可以从终点往前推出 所 ...

  6. 1087 All Roads Lead to Rome(69行代码+超级无敌详细注释)

    分数 30 全屏浏览题目 作者 CHEN, Yue 单位 浙江大学 Indeed there are many different tourist routes from our city to Ro ...

  7. All Roads Lead to Rome (30)

    题目描述 Indeed there are many different tourist routes from our city to Rome. You are supposed to find ...

  8. PAT1087 All Roads Lead to Rome (30)(最短路径+dfs+回溯)

    题意: 有N个城市,M条无向边,从某个给定的起始城市出发,前往名为ROM的城市.每个城市(除了起始城市)都有一个点权(称为幸福值),和边权(每条边所需的花费).求从起点到ROM所需要的最少花费,并输出 ...

  9. PAT A1087 All Roads Lead to Rome

    1087 All Roads Lead to Rome 分数 30 作者 CHEN, Yue 单位 浙江大学 Indeed there are many different tourist route ...

最新文章

  1. [Tracking] KCF + KalmanFilter目标跟踪
  2. SpringBoot 定制化原理
  3. 【maven插件】maven-resources-plugin
  4. 【Vue】路由Router传参的两种方式(详解)
  5. spark学习-72-源代码:Endpoint模型介绍(4)-Spark为何使用Netty通信框架替代Akka
  6. 如何啃透周志华的《机器学习》西瓜书?
  7. 杜甫的《望岳》在哪里作的?山脚下还是山头上?
  8. 前后端开源的一款简单的微信个人博客小程序
  9. rhce考试试题以及答案_RedHat红帽认证 RHCE 7.0题库和答案
  10. 应急指挥调度管理系统|城市综合应急指挥调度系统
  11. JAVA上传smartupload_java使用smartupload组件实现文件上传的方法
  12. 【知识图谱】OpenKG 发布多个新冠病毒相关开放知识图谱
  13. Jenkins与码云集成
  14. 树莓派(Raspberry Pi) 命令行下如何配置wifi(wlan)
  15. bzoj4238: 电压
  16. Python实现圆通快递单号查询官方接口教程
  17. CISCO WLC的配置备份与导入
  18. storm风暴英雄 tempo_风暴英雄玩好乔汉娜需要知道的7个技巧 TempoStorm
  19. mysql存储过程工作日判断_Oracle存储过程根据指定日期返回(N个)工作日的时间...
  20. 四川大学计算机学院硕士毕业要求,四川大学计算机学院(软件学院)2020年非全日制硕士研究生接受调剂生的通知...

热门文章

  1. 微信红包程序c语言,C语言实战番外篇——模拟微信抢红包
  2. 全国省市区三级Json数据
  3. sqlserver2014 sa无法登录[已解决]
  4. c语言计算二次函数的原函数公式,二次函数的四种表达式求法推导(20201127010605).docx...
  5. 【C++】KMP算法代码实现
  6. MeeGo开发者(一):何为MeeGo?
  7. A02-自定义Icon图标
  8. 计算机语言之java基础知识一
  9. MATLAB 神经网络预测工具箱
  10. conv2d函数中dilation参数:int or tuple