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), 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
 #include<iostream>
#include<string>
#include<unordered_map>
using namespace std;
int graph[][];
int city[], dis[], happiness[];//每个城市的幸福值、到达每个城市时的距离和幸福值
int pathnum[], past[], pastnum[];//到达每个城市时的最短路径条数、每个城市的前驱城市、到达每个城市前经过的城市
bool visit[];//每个城市是否被访问过
unordered_map<string, int>STOI;//将城市字符串映射到整数
string ITOS[];//将整数映射到字符串
int N, K;
void Dijkstra(int vend) {
while (!visit[vend]) {//当未遍历到终点城市时
int v = -, MIN = INT_MAX;
for (int i = ; i < N; ++i)//找出当前未被访问的距离最小的城市
if (!visit[i] && MIN > dis[i]) {
v = i;
MIN = dis[i];
}
if (v == -)//图不连通直接返回
return;
visit[v] = true;//当前城市已访问
for (int i = ; i < N; ++i)//遍历当前城市能到达的城市
if (!visit[i] && graph[v][i] != && dis[i] > dis[v] + graph[v][i]) {//能到达的当前城市未被访问过且距离可更新
dis[i] = dis[v] + graph[v][i];//更新到达该城市的距离
happiness[i] = happiness[v] + city[i];//更新到达该城市的幸福值
pastnum[i] = pastnum[v] + ;//更新到达该城市前遍历过的城市数
past[i] = v;//更新到达该城市的前驱城市
pathnum[i] = pathnum[v];//更新到达该城市的最短路径条数
}
else if (graph[v][i] != && dis[i] == dis[v] + graph[v][i]) {//到达该城市时的距离与该城市储存的距离相等
pathnum[i] += pathnum[v];//更新到达该城市的最短路径条数
if (happiness[i] < happiness[v] + city[i] || (happiness[i] == happiness[v] + city[i] && pastnum[i] > pastnum[v] + )) {
past[i] = v;//更新到达该城市的前驱城市
happiness[i] = happiness[v] + city[i];//更新到达该城市的幸福值
pastnum[i] = pastnum[v] + ;//更新到达该城市的最短路径条数
}
}
}
}
void DFS(int v) {
if (v == ) {
cout << ITOS[v];
return;
}
DFS(past[v]);
cout << "->" << ITOS[v];
}
int main() {
scanf("%d%d", &N, &K);
cin >> ITOS[];
STOI.insert({ ITOS[], });
for (int i = ; i < N; ++i) {
cin >> ITOS[i];
STOI.insert({ ITOS[i],i });
cin >> city[i];
}
while (K--) {
int a;
string s1, s2;
cin >> s1 >> s2 >> a;
graph[STOI[s1]][STOI[s2]] = graph[STOI[s2]][STOI[s1]] = a;
}
int vend = STOI["ROM"];//将ROM设置为终点城市
fill(dis + , dis + N, INT_MAX);//距离初始化为INT_MAX
pathnum[] = ;//起点城市最短路径条数设置为1
Dijkstra(vend);
printf("%d %d %d %d\n", pathnum[vend], dis[vend], happiness[vend], happiness[vend] / pastnum[vend]);
DFS(vend);
return ;
}

PAT甲级——A1087 All Roads Lead to Rome【30】的更多相关文章

  1. PAT甲级1087. All Roads Lead to Rome

    PAT甲级1087. All Roads Lead to Rome 题意: 确实有从我们这个城市到罗马的不同的旅游线路.您应该以最低的成本找到您的客户的路线,同时获得最大的幸福. 输入规格: 每个输入 ...

  2. PAT 甲级 1087 All Roads Lead to Rome(SPFA+DP)

    题目链接 All Roads Lead to Rome 题目大意:求符合题意(三关键字)的最短路.并且算出路程最短的路径有几条. 思路:求最短路并不难,SPFA即可,关键是求总路程最短的路径条数. 我 ...

  3. PAT 甲级 1087 All Roads Lead to Rome

    https://pintia.cn/problem-sets/994805342720868352/problems/994805379664297984 Indeed there are many ...

  4. pat1087. All Roads Lead to Rome (30)

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

  5. [图的遍历&多标准] 1087. All Roads Lead to Rome (30)

    1087. All Roads Lead to Rome (30) Indeed there are many different tourist routes from our city to Ro ...

  6. 【PAT甲级】1087 All Roads Lead to Rome (30 分)(dijkstra+dfs或dijkstra+记录路径)

    题意: 输入两个正整数N和K(2<=N<=200),代表城市的数量和道路的数量.接着输入起点城市的名称(所有城市的名字均用三个大写字母表示),接着输入N-1行每行包括一个城市的名字和到达该 ...

  7. PAT甲级练习 1087 All Roads Lead to Rome (30分) 字符串hash + dijkstra

    题目分析: 这题我在写的时候在PTA提交能过但是在牛客网就WA了一个点,先写一下思路留个坑 这题的简单来说就是需要找一条最短路->最开心->点最少(平均幸福指数自然就高了),由于本题给出的 ...

  8. A1087. All Roads Lead to Rome

    Indeed there are many different tourist routes from our city to Rome. You are supposed to find your ...

  9. PAT (Advanced Level) 1087. All Roads Lead to Rome (30)

    暴力DFS. #include<cstdio> #include<cstring> #include<cmath> #include<vector> # ...

随机推荐

  1. lucene入门-搜索方式

    1 package com.home.utils; import java.util.ArrayList; import java.util.List; import org.apache.lucen ...

  2. 关于SQL server2017无法连接远程服务器的问题

    安装了SQL server2017,能连接上本地数据库,但是连接远程数据库则老报错,什么实例错误之类的,百度找了也是什么打开sql server 服务,什么修改端口1433,什么TCP协议之类的,全部 ...

  3. Python学习笔记(五)——异常处理

    Python 异常总结 异常名称 解释 AssertionError 断言语句(assert)失败:当assert关键字后边的条件为假时,程序将抛出该异常,一般用于在代码中置入检查点 OSError ...

  4. Python中虚拟环境venv的基本用法

    环境windows 7 venv为python3中的默认库,无需安装. 创建新的venv方法, 在当前文件夹下执行cmd,输入如下代码 python -m venv bob bob为需要创建的文件夹名 ...

  5. Erlang学习记录:app demo

    目录结构 │ Emakefile │ make.bat │ start.bat ├─config │ config.config │ server.app ├─ebin │ wulin_app.bea ...

  6. 初识Qgis

    折腾了一天,qgis终于能在跟了自己8年的本本上顺利打开了,官网先后下载了3.8和3.4版本的都出现了同样的问题,"could not load qgis_app.dll",goo ...

  7. 初识 HTML

    HTML 1.描述 HTML超文本标记语言(英语:HyperText Markup Language)是一种用于创建网页的标准标记语言 您可以使用 HTML 来建立自己的 WEB 站点,HTML 运行 ...

  8. thinkphp 操作绑定到类

    定义 ThinkPHP3.2版本提供了把每个操作方法定位到一个类的功能,可以让你的开发工作更细化,可以设置参数ACTION_BIND_CLASS,例如: 'ACTION_BIND_CLASS' =&g ...

  9. 使用WebStorm上传本地项目到GitHub和GitLab

    在使用 WebStorm 上传本地项目到 GitHub 之前,先要做一些相关配置. 首先打开 WebStorm ,依次点击File -> Settings… 打开系统设置面板,在上面搜索 git ...

  10. 将maven项目打成war包

    //修改成war包 <packaging>war</packaging> //plugins中添加新的配置 <build> <plugins> < ...