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. java笔试之从单向链表中删除指定值的节点

    输入一个单向链表和一个节点的值,从单向链表中删除等于该值的节点,删除后如果链表中无节点则返回空指针. 链表的值不能重复 构造过程,例如 1 -> 2 3 -> 2 5 -> 1 4  ...

  2. iOS进阶四-自动释放池原理

    概述 AutoreleasePool(自动释放池)是OC中的一种内存自动回收机制,它可以延迟加入AutoreleasePool中的变量release的时机.在正常情况下,创建的变量会在超出其作用域的时 ...

  3. FCC——相关练习

    算法题目1:Seek and Destroy(摧毁数组) 金克斯的迫击炮! 实现一个摧毁(destroyer)函数,第一个参数是待摧毁的数组,其余的参数是待摧毁的值. 帮助资源: Arguments ...

  4. [190308]Ubuntu 安装完之后,安装的软件小记

    install software vim sudo apt-get install -y vim Typora command copy from Typora website # or run: # ...

  5. leetcode-47-全排列②

    题目描述: 方法一:回溯 class Solution: def permuteUnique(self, nums: List[int]) -> List[List[int]]: #nums = ...

  6. Nacos v0.7.0:对接CMDB,实现基于标签的服务发现能力

    Nacos近期发布了0.7.0版本,该版本支持对接第三方CMDB获取CMDB数据.使用Selector机制来配置服务的路由类型.支持单机模式使用MySQL数据库.上线Node.js客户端,并修复了一些 ...

  7. 带撤销贪心——cf1148F好题

    自己不会做,看了题解懂得 从最高位依次往低位遍历,因为偶数个1是不改变符号的,所以带个贪心即可(可以看成是带撤销的..) 每轮循环用sum记录该位选择1可以减少的值 如果是负数,就不要改成1 如果是正 ...

  8. 求1到n这n个整数间的异或值 (O(1)算法)

      问题:求1到n这n个整数间的异或值,即 1 xor 2 xor 3 ... xor n 记 f(x, y) 为x到y的所有整数的异或值. 对 f(2^k, 2^(k+1) -1) (注意文章中的  ...

  9. H2数据库的基本使用

    文章目录 下载jar包 启动服务 下载jar包 下载h2-1.3.176.jar 这个包(部分服务版本不一致,请自行更换版本) 启动服务 从终端定位到刚才jar包下载的位置,比如我这里是Downloa ...

  10. char类型在传参时接收不到数据的原因

    mybatis的原因!!!!! 数据库这个样子 在postman中调用接口:SQL select * from T_TRAIN_MARSHALLING where TRAIN_NUM is null ...