PAT甲级——A1087 All Roads Lead to Rome【30】
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】的更多相关文章
- PAT甲级1087. All Roads Lead to Rome
PAT甲级1087. All Roads Lead to Rome 题意: 确实有从我们这个城市到罗马的不同的旅游线路.您应该以最低的成本找到您的客户的路线,同时获得最大的幸福. 输入规格: 每个输入 ...
- PAT 甲级 1087 All Roads Lead to Rome(SPFA+DP)
题目链接 All Roads Lead to Rome 题目大意:求符合题意(三关键字)的最短路.并且算出路程最短的路径有几条. 思路:求最短路并不难,SPFA即可,关键是求总路程最短的路径条数. 我 ...
- PAT 甲级 1087 All Roads Lead to Rome
https://pintia.cn/problem-sets/994805342720868352/problems/994805379664297984 Indeed there are many ...
- pat1087. All Roads Lead to Rome (30)
1087. All Roads Lead to Rome (30) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...
- [图的遍历&多标准] 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 ...
- 【PAT甲级】1087 All Roads Lead to Rome (30 分)(dijkstra+dfs或dijkstra+记录路径)
题意: 输入两个正整数N和K(2<=N<=200),代表城市的数量和道路的数量.接着输入起点城市的名称(所有城市的名字均用三个大写字母表示),接着输入N-1行每行包括一个城市的名字和到达该 ...
- PAT甲级练习 1087 All Roads Lead to Rome (30分) 字符串hash + dijkstra
题目分析: 这题我在写的时候在PTA提交能过但是在牛客网就WA了一个点,先写一下思路留个坑 这题的简单来说就是需要找一条最短路->最开心->点最少(平均幸福指数自然就高了),由于本题给出的 ...
- A1087. All Roads Lead to Rome
Indeed there are many different tourist routes from our city to Rome. You are supposed to find your ...
- PAT (Advanced Level) 1087. All Roads Lead to Rome (30)
暴力DFS. #include<cstdio> #include<cstring> #include<cmath> #include<vector> # ...
随机推荐
- jQuery post使用变量作参数名
jQuery Query Post使用方法: $.post("test.php", { name: "John", time: "2pm" ...
- Tomcat调优详解
前言 在这里告诫一下那些感觉自己啥都会的朋友们,其实你会的可能只是皮毛,不要感觉这个东西以前已经做过了,就不想去做了 其实你还远没有达到精通的地步,遇到以前做过的东西,也要用心的再去做一遍,你可能会从 ...
- 回车切换input选框
在工作中许多时候需要考虑到用户体验,当按下回车键时切换input选框就来得十分必要. <!DOCTYPE HTML> <html> <head> <meta ...
- ionic js 侧栏菜单 把主要内容区域从一边拖动到另一边,来让左侧或右侧的侧栏菜单进行切换
ionic 侧栏菜单 一个容器元素包含侧边菜单和主要内容.通过把主要内容区域从一边拖动到另一边,来让左侧或右侧的侧栏菜单进行切换. 效果图如下所示: 用法 要使用侧栏菜单,添加一个父元素<ion ...
- http协议 头部字段 referrer
学习笔记,非原创,抄自:https://www.cnblogs.com/amyzhu/p/9716493.html:https://blog.csdn.net/java_zhangshuai/arti ...
- innodb 表
1.innodb的存储引擎表类型 如果在创建表时没有显示的定义主键,则innodb存储引擎会按如下方式选择或创建主键 a.首先表中是否有非空的唯一约束(Unique not null)如果有,则该列即 ...
- Android开发 多媒体提取器MediaExtractor详解_将一个视频文件分离视频与音频
前言 此篇博客讲解MediaExtractor将一个视频文件分离视频与音频,如果你对MediaExtractor还没有一个笼统的概念建议先了解我的另一篇入门博客:https://www.cnblogs ...
- ES5-call,apply,bind的用法
区别bind()与call()和apply()? 1. Function.prototype.bind(obj) : * 作用: 将函数内的this绑定为obj, 并将函数返回2. 面试题: 区别bi ...
- phonegap 开发指南系列(3) ----在Eclipse中Android开发环境搭建
前提条件:已在Eclipse中安装好Android SDK 和 ADT. 1.下载PhoneGap,解压. 2.用Eclipse新建一个安卓项目. 3.将phoneGap解压包里的Android文 ...
- hibernate_01_SSH环境搭建
1.maven工程pom.xml文件 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="h ...