PAT (Advanced Level) Practice 1087 All Roads Lead to Rome (30 分) 凌宸1642

PAT (Advanced Level) Practice 1087 All Roads Lead to Rome (30 分) 凌宸1642

题目描述:

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≤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…

译:每个输入文件包含一个测试用例。对于每个用例,第一行中包含两个正整数, 城市总数 N (2≤N≤200) ,和城市之间能直接到达的路径总数 K ;紧跟着是起点城市的名称。接下来 N - 1行,每行给处一个城市的名称和一个代表可以从该城市获得的开心值的整数,除起点城市外。接下来K行,每行给处形式如City1 City2 Cost的关于两个城市之间的路径的描述。所有的城市名称都是有三个大写英文字母组成,并且目的地固定为ROM 表示罗马城。


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

译:对于每个测试用例,我们应该找出花费最小的路径。如果这样的路径不唯一,那就选择获得开心值最大的路径。如果这样的路径依旧不唯一,我们应该输出平均开心值最大的路径——题目保证这样的路径存在并且唯一。

因此第一行需要输出四个数字:花费最小的路径数,最小花费,获得的最大开心值,最大平均开心值(只取整数部分)。第二行,你应该用此格式 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

The Idea:

首先很明显这是一题单源最短路径的问题。并且有三个维度,花费最小,最大开心值,平均最大开心值。

  • 第一步,建图,由于城市的名称都是三位的大写英文字母,所以可以建立城市名称 name 和数字编号之间的映射,方便后续操作 。
  • 第二步,理解题意中,花费其实就是城市之间距离,平均最大开心值,就是经过的城市最少
  • 第三步,选择 Dijkstra 算法进行求解,具体可见代码的注释。
  • 第四步,按要求输出。

The Codes:

#include<bits/stdc++.h>
using namespace std ;
const int maxn = 210 ;
const int inf = 0x3fffffff ;
int n , k , cost , rom ; 
string st , tem , tem1 ;
int happy[maxn] ;			// 各点的 happy 值  
int rnum[maxn] ;			// 起点到各点的最短路径数 
int d[maxn] ;				// 起点到各点的最短路径 
int pre[maxn] ;				// 起点到各点的最短且最大 happy 值路径上的前驱结点 
int happpMax[maxn
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lingchen0522

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值