All Roads Lead to Rome (30)
时间限制 1000 ms 内存限制 65536 KB 代码长度限制 100 KB 判断程序 Standard (来自 小小)
题目描述
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
思路:一开始准备用第一题的思路做,后来有5个样例不过,看了讨论才发现,统计最短路径条数不能只讨论倒数第二个点,因为中间可能存在一些点有多条路径。所以求最短路径条数应该在松弛过程中维护。
注:city[i]表示i号城市的happy值,
happy[i]表示i号节点的最短路径的happy总值,
cnt[i]表示i号节点最短路径中city数,
scheme[i]表示i号节点最短路径条数
name[s]表示城市名称s的编号
index[i]表示编号为i的城市名称
还有一点:就是happy平均值不能用int存,注意看题,题目要求只是最后输出整数部分,比较时仍需要用double

#include <unordered_map>
#include <iostream>
#include<string>
#include <algorithm>
using namespace std;
const int N = 205, INF = 1000000000;
int n, m, dest;
int g[N][N], dist[N],path[N], happy[N],cnt[N],scheme[N],city[N];
bool st[N];     // 存储每个点的最短距离是否已确定
string index[N];
unordered_map<string,int>name;void dijkstra()
{for (int i = 0; i<=n; i++) dist[i] = INF;dist[0] = 0; //cnt[0]=0;scheme[0]=1;for (int i = 0; i<=n; i++){int id, mind = INF;for (int j = 0; j <= n; j++)if (!st[j] && dist[j] < mind){mind = dist[j];id = j;}st[id] = 1;for (int j = 0; j <= n; j++){if(dist[id]+g[id][j]<dist[j]){dist[j]=dist[id]+g[id][j]; //更新最短路径长度 path[j]=id;                //更新路径 happy[j]=happy[id]+city[j];cnt[j]=cnt[id]+1; scheme[j]=scheme[id];        //可以在松弛过程中更新属性值 }else if(dist[id]+g[id][j]==dist[j]){scheme[j]+=scheme[id];int h=happy[id]+city[j];double ave=h*0.1/(cnt[id]+1);if(h>happy[j] || (h==happy[j] && ave>happy[j]*0.1/cnt[j])){path[j]=id;happy[j]=h;cnt[j]=cnt[id]+1;}}}  }
}void outpath()
{vector<int>v;for(int i=dest;i;i=path[i]) v.push_back(i);cout<<index[0];for(int i=v.size()-1;i>=0;i--) cout<<"->"<<index[v[i]];
}int main()
{string s1,s2;cin>>n>>m>>s1;n--;name[s1]=0;index[0]=s1;for (int i = 0; i <= n; i++)for (int j = 0; j <= n; j++)g[i][j] = INF;for(int i=1;i<=n;i++){cin>>s1>>city[i];name[s1]=i;index[i]=s1;if(s1=="ROM") dest=i;}for(int i=0,d;i<m;i++){cin>>s1>>s2>>d;g[name[s1]][name[s2]]=g[name[s2]][name[s1]]=d;}dijkstra();cout<<scheme[dest]<<" "<<dist[dest]<<" "<<happy[dest]<<" "<<(int)happy[dest]/cnt[dest]<<endl;outpath();return 0;
}

PAT甲级1002 All Roads Lead to Rome相关推荐

  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 A1087 All Roads Lead to Rome

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

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

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

  5. T1028 Do All Roads Lead to Rome (35 point(s))

    T1028 Do All Roads Lead to Rome (35 point(s)) 文章目录 T1028 Do All Roads Lead to Rome (35 point(s)) 题干 ...

  6. Pat甲级 1002 A+B for Polynomials

    Pat甲级 1002 A+B for Polynomials 思路 代码 题目网址 https://pintia.cn/problem-sets/994805342720868352/problems ...

  7. PAT甲级1002 A+B for Polynomials:[C++题解]字符串、多项式加法或高精度加法

    文章目录 题目分析 题目链接 题目分析 本题数据范围是1000 ,可以开一个数组 X [ ] ,X[i ]表示多项式中次幂是i的系数是多少. 因此,本题可以开两个数组,对应相加,然后输出非零项即可. ...

  8. PAT甲级 1002

    PAT 甲级 1002 题目描述:多项式求和问题 输入两个多项式,输出和的多项式(多项式按指数递减排列) Sample Input: 2 1 2.4 0 3.2 2 2 1.5 1 0.5 Sampl ...

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

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

最新文章

  1. jQuery:1.5.4.3,表格变色(单击行,把当行的单选按钮(radio)设为选中状态,并应用当前样式)...
  2. why quantity change in item can cause CUMULAT_H changed as well
  3. js 页面所有超链接后加随机数 基于jquery
  4. python双向索引什么意思_Python 双向链表的实现
  5. 数据库大作业-学生宿舍管理系统
  6. 一种基于C6748 DSP+FPGA的软件无线电平台的设计及应用
  7. 影响我一生的两本书(02)_huadingjin_新浪博客
  8. 领导力 之 《情境领导》
  9. 关于使用GD32E230C SPI驱动SX1278遇到的坑
  10. Oracle中用户角色权限管理
  11. Yahoo.cn邮箱的IMAP设置方法
  12. 了解模型预测控制4--自适应,增益调度和非线性MPC
  13. python三方库打包项目中_python项目生成及导入依赖的第三方库
  14. 第5次作业+160+曾元鹏
  15. DSSM召回原理与优化
  16. 出海美利坚 不可忽视的未成年人法律红线
  17. java成员变量定义_java变量之成员变量和局部变量以及它们的运行机制
  18. 计算机在3d打印发挥的作用,七大优势汇总 看3D打印的路到底能走多远
  19. 【干货】九型人格与自我修炼!你是哪种性格?更适合什么工作?
  20. excel模板中参数替换

热门文章

  1. 淘宝商品信息定向爬虫
  2. 进阶篇:3.3)压铸件设计
  3. 中国紫菜产业发展现状及趋势分析,紫菜养殖产量持续增长「图」
  4. 【camera】【CMOS Sensor】感光芯片cmos sensor简单介绍
  5. APP带第三方微博、微信、QQ等登录注册的登录注册流程分解
  6. 运行opencv程序后出现runtime error! R6025-pure virtual function call 错误提示的解决方法
  7. 智能电饭煲电路图及其原理_求奔腾智能电饭煲原理电路图
  8. 2020年高教社杯全国大学生数学建模竞赛---校园供水系统智能管理(Python代码实现)
  9. InDesign 教程:如何向母版页添加内容?
  10. Java8 的 Stream简单教程