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. Python 执行tail文件并操作

    def log_search(self, logfile, search_content, timeout=10): import time import subprocess import sele ...

  2. JS对象 String 字符串对象定义字符串的方法就是直接赋值。比如: var mystr = "I love JavaScript!"

    String 字符串对象 在之前的学习中已经使用字符串对象了,定义字符串的方法就是直接赋值.比如: var mystr = "I love JavaScript!" 定义mystr ...

  3. CSIC_716_20191029【人脸打分系统】

    今日内容: 1.调用百度的AI接口,完成人脸图像打分( 敷衍) 2.完成系统内置时间的打印 3.将上述两段代码生成可执行文件 ------------------------------------- ...

  4. Vim模糊查找与替换

    例如要把 ( 1 ).( 2 ) - 全部替换成其他字符,可以用命令: :%s/(.*)/str/gn 其中,.* 表示匹配任何东西,如果只希望匹配应为字母和数字,可以用 \w\+. 有些特殊字符需要 ...

  5. [JZOJ4330] 【清华集训模拟】几何题

    题目 题目大意 也懒得解释题目大意了-- 正解 正解居然是\(FFT\)? 不要看题目的那个式子这么长,也不要在那个式子上下手. 其实我们会发现,不同的\((x_i-x_j,y_i-y_j,z_i-z ...

  6. R语言 循环

    R语言循环 可能有一种情况,当你需要执行一段代码几次. 通常,顺序执行语句. 首先执行函数中的第一个语句,然后执行第二个语句,依此类推. 编程语言提供允许更复杂的执行路径的各种控制结构. 循环语句允许 ...

  7. Http学习(一)

    HTTP 超文本传输协议 综述: HTTP(HyperText Transfer Protocol)是一套计算机通过网络进行通信的规则.计算机专家设计出HTTP,使HTTP客户(如Web浏览器)能够从 ...

  8. CentOS中GDB提示Missing separate debuginfos解决办法

    安装debuginfo 修改文件 vi /etc/yum.repo.d/CentOS-Debuginfo.repo 修改enabled的值为1 使用debuginfo-install安装需要的文件

  9. 6_2.springboot2.x整合Druid和配置数据源监控

    简介 Druid首先是一个数据库连接池.Druid是目前最好的数据库连接池,在功能.性能.扩展性方面,都超过其他数据库连接池,包括DBCP.C3P0.BoneCP.Proxool.JBoss Data ...

  10. Django框架中session存储到redis中的配置

    本文链接:https://blog.csdn.net/linqunbin/article/details/94786313————————————————版权声明:本文为CSDN博主「linqunbi ...