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. UVALive7461 - Separating Pebbles 判断两个凸包相交

    //UVALive7461 - Separating Pebbles 判断两个凸包相交 #include <bits/stdc++.h> using namespace std; #def ...

  2. CF431E Chemistry Experiment

    题意:有n个试管,有高度为hi的水银.操作1:将试管x中的水银高度改成y.操作2:将体积为v的水注入试管,求水位的高度?n,q<=1e5. 标程: #include<bits/stdc++ ...

  3. EF 如何更新多对多关系的实体

    ctx.Entry(user).Collection(t => t.UserPrivileges).Load(); Come form:https://www.thereformedprogra ...

  4. Java 虚拟机 - ClassLoader

    ClassLoader定义 ClassLoader种类 BootStrapClassLoader无法在IDEA里面查看源代码,只能看JVM 源码才能找到. ExtClassLoader,会从Syste ...

  5. Elasticsearch日志之删除索引

    1.查询索引 [root@ecs-- elasticsearch]# curl -XGET http://localhost:9200/* {,,},},},,,},},},,,},},},,,},} ...

  6. Linux和Windows下ping命令详解

    转:http://linux.chinaitlab.com/command/829332.html 一.Linux下的ping参数 用途 发送一个回送信号请求给网络主机. 语法 ping [ -d] ...

  7. FastJSON实现详解

    摘要:“快”作为程序员追逐的终极目标之一,而FastJSON则很好的证明了这一特性.本期<问底>,静行将带大家见证它序列化和反序列化的实现过程,一起领略它的“快”感. 还记得电影<功 ...

  8. POJ-1976-A Mini Locomotive-dp

    A train has a locomotive that pulls the train with its many passenger coaches. If the locomotive bre ...

  9. Mysql优化系列之数据类型优化

    本篇是优化系列的第一篇:数据类型 为了不产生赘述,尽量用简洁的语言来描述. 在选择数据类型之前,首先要知道几个原则: 更小的通常更好 尽量使用可以正确存储数据的最小数据类型.更小的数据类型意味着更快, ...

  10. HYNB Round 15: PKU Campus 2019

    HYNB Round 15: PKU Campus 2019 C. Parade 题意 将平面上n*2个点安排在长度为n的两行上. 做法 首先可以忽略每个点之间的影响,只用考虑匹配即可 然后把所以点归 ...