题目描述

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.

输入描述:

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.

输出描述:

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 recommended.  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 recommended route.  Then in the next line, you are supposed to print the route in the format "City1->City2->...->ROM".

输入例子:

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

输出例子:

3 3 195 97
HZH->PRS->ROM

AC代码

#include<bits/stdc++.h>
using namespace std;
int N,K,start,des;
unordered_map<string ,int> m;
string namelist[200];
int happyofcity[200],dis[200][200],mindis[200];
vector<int> v[200],path,finpath;
int findis,finhappy,finstep,finnum;void dfs(int curcity,int curdis,int curhappy,int curstep)
{if(curdis>mindis[curcity])return;path.push_back(curcity);if(curcity==des){if(curdis<mindis[curcity]){mindis[curcity]=curdis;finpath=path;findis=curdis;finhappy=curhappy;finstep=curstep;finnum=1;}else{finnum++;if(curhappy>finhappy||curhappy==finhappy&&curstep<finstep){finpath=path;finhappy=curhappy;finstep=curstep;}}}else{mindis[curcity]=curdis;for(auto each:v[curcity]){dfs(each,curdis+dis[each][curcity],curhappy+happyofcity[each],curstep+1);}}path.pop_back();
}
int main()
{int i,j,k,l;cin>>N>>K;string startcity;cin>>startcity;m[startcity]=0;namelist[0]=startcity;happyofcity[0]=0;for(int i=1;i<N;i++){string s;cin>>s;namelist[i]=s;m[s]=i;cin>>happyofcity[i];}des=m["ROM"];while(K--){string c1,c2;cin>>c1>>c2>>l;i=m[c1];j=m[c2];v[i].push_back(j);v[j].push_back(i);dis[i][j]=dis[j][i]=l;}for(int i=0;i<N;i++) mindis[i]=1000000;dfs(start,0,0,0);cout<<finnum<<" "<<findis<<" "<<finhappy<<" "<<finhappy/finstep<<endl;cout<<startcity;for(int i=1;i<finpath.size();i++){cout<<"->"<<namelist[finpath[i]];}
}

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

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

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

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

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

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

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

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

    题目链接: 题目详情 - 1087 All Roads Lead to Rome (30 分) (pintia.cn)https://pintia.cn/problem-sets/9948053427 ...

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

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

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

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

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

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

  8. PAT甲级1002 All Roads Lead to Rome

    All Roads Lead to Rome (30) 时间限制 1000 ms 内存限制 65536 KB 代码长度限制 100 KB 判断程序 Standard (来自 小小) 题目描述 Inde ...

  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. python爬虫beautifulsoup实例-Python爬虫学习(二)使用Beautiful Soup库
  2. 终于有了属于自己的家,哈哈,很高兴~~
  3. 二叉树垂直遍历 java_【004】二叉树垂直遍历
  4. 电压源和电流的关联参考方向_结点电压法解题系列之四:电流源支路
  5. 使用https协议解决掉顽固不化的已解密的登录请求
  6. 用python爬取知识星球
  7. 医院大数据中心建设要点分析
  8. java jax ws_Java 7是否包含JAX-WS实现或API?
  9. python反编译class文件_反编译java class文件
  10. QCC3040---Panic types
  11. 【Oracle】并行等待之PX Deq: Execute Reply
  12. GCA matting(2020, trimap)
  13. 山东大学科技文献期末复习(个人速成向)
  14. js 获取浏览器屏幕的宽度和高度
  15. HTML5期末考核大作业,个人网站—— 程序员个人简历模板下载HTML+CSS+JavaScript
  16. 六个步骤教你学会用ZBrush绘制头部模型
  17. 感恩节跟进技巧(附邮件模板)
  18. Windows10下Latex缺少sty文件时的安装方法
  19. Hashtable和hashMap有什么区别
  20. 要如何图片文字识别翻译?这些软件能帮你

热门文章

  1. 丹佛斯变频器型号说明_台达变频器VFD-CH2000型号说明及功能介绍
  2. 事务超时时间无效_Java面试题:Spring事务面试考点的集合整理。建议收藏阅读...
  3. idea通过数据库生成实体类插件_idea数据库生成实体类
  4. linux 内核 锐龙,AMDGPU内核驱动程序在Linux 5.0上运行良好
  5. ubuntu apt-get update 失败解决
  6. svn .a文件上传不了
  7. SQL Server中时间格式转换函数convert()的使用
  8. thinkphp5.0验证码使用
  9. QT5新建工程错误-无法打开源文件QtWidgets/QApplication
  10. POJ 2777 线段树