1087 All Roads Lead to Rome (30 point(s))

【Microchip】微芯 MPLAB® X IDE开发环境新建项目(点亮1颗LED灯)(常用右键菜单快捷键) 1.新建项目2. 选择芯片型号和调试器3.选择编译器4.项目命名和选择文件路径5.新建成功,给项目添加main.c7.将新建的mainXC16.c文件添加进项目6000000UL这三个就是。 阅读详情

1087 All Roads Lead to Rome (30 point(s))

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.

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

Dijkstra算法的灵活使用、DFS。理解题意、小心求解是关键。

条条大路通罗马。每个到罗马的城市有一定的快乐值,每条道路有一定的花销。要求找出出发城市到罗马的最短路径。如果最短路径不唯一,找出其中可以得到快乐值最高的,如果仍然不唯一,找出其中经过城市最少的(即平均快乐值最高)。输出花销最短的路径的条数(快乐值、平均快乐值可以不同)、最短路径的花销、获得的最大快乐值以及平均快乐值,还有输出满足最后条件的路径。

关于数组维护。采用Dijkstra算法,因此首先要维护visit[]和dis[],分别记录i是否已经加入集合、出发点到i的最短路径。按照题意,还需要记录每个城市的快乐值h[],到达当前城市的快乐值sumH[]、到达每个城市的最短路径的条数paths[]、到达当前城市所经过的城市数量pastCities[]。特别地,为了能够最终给出这条路径,还需要记录前驱城市lastCity[]。

当每个结点newP加入集合,遍历每一个相邻的边(另一结点t和花销c),如果结点原本不可达或者经过newP有更小的距离,说明得到了更短的距离,则更新上述所有数组;如果经过newP有相同的距离,说明相同距离的路径也不唯一,维护paths[],再根据新路径的快乐值情况和所经过城市的数量,进一步确定是否需要维护其它数组(详见代码)。

理解算法的贪心策略,只要dest进入集合即可结束。

注意点:

1. 需要建立好城市名、城市数字编号的映射关系,使用map或者unordered_map根据value查询key,建立数组实现可以根据key查询value。区分map和unordered_map。

2. 回溯法得出路径。

3. 数组的设置和更新。

一定要小心,仔细!粗心大意的错误太多了!

关于Dijsktra算法:https://blog.youkuaiyun.com/coderwait/article/details/89161811

大神代码:https://blog.youkuaiyun.com/richenyunqi/article/details/80146150

#include<iostream>
#include<vector>
#include<cstring>
#include<map>
using namespace std;
const int MAX = 205;
struct Edge{
	int next;
	int cost;
	Edge(int n,int c){
		next = n;
		cost=c;
	}
};
vector<Edge> graph[MAX];//图 
int h[MAX],dis[MAX],sumH[MAX];//幸福值、从起点到该城市的距离、到达这个城市的幸福值 
int paths[MAX],lastCity[MAX], pastCities[MAX];//到达该城市的路径数、到达该城市的前驱城市、 
bool visit[MAX]={false};
map<string,int> mp;
string names[MAX];
void dfs(int city){//回溯法找出路径 
	if(city==0){
		cout<<names[city];return;
	}
	dfs(lastCity[city]);
	cout<<"->"<<names[city];
} 
int main(void){
	int N,K;cin>>N>>K;
	cin>>names[0];
	mp.insert({names[0],0});
	for(int i=1;i<N;i++){
		cin>>names[i]>>h[i];
		mp.insert({names[i],i});
	}
	string a,b;int c;
	for(int i=0;i<K;i++){
		cin>>a>>b>>c;
		int ax = mp[a],bx = mp[b];
		graph[ax].push_back(Edge(bx,c));
		graph[bx].push_back(Edge(ax,c));
	}
	int dest = mp["ROM"];
	memset(dis,-1,sizeof(dis));
	memset(sumH,0,sizeof(sumH));
	memset(paths,0,sizeof(paths));
	memset(pastCities,0,sizeof(pastCities)); 
	dis[0]=0;paths[0]=1;//初始化
	visit[0]=true;
	int newP = 0;
	while(!visit[dest]){
		for(int i=0;i<graph[newP].size();i++){
			int t = graph[newP][i].next;
			int c = graph[newP][i].cost;
			if(visit[t]) continue;
			if(dis[t]==-1||dis[newP]+c<dis[t]){//不可达,或者有更短的路径 
				dis[t] = dis[newP]+c;//更新到达距离
				sumH[t] = sumH[newP]+h[t];//更新到达该城市的幸福值
				paths[t] = paths[newP];//更新到达该城市的路径数 
				lastCity[t] = newP;//更新到达该城市的前驱城市 
				pastCities[t] = pastCities[newP]+1;//更新经过的城市数量 
			}
			else if(dis[newP]+c == dis[t]){//有相同长度的路径
				paths[t] += paths[newP];//更新到达该城市的路径数
				if(sumH[t]<sumH[newP]+h[t]||
				(sumH[t]==sumH[newP]+h[t]&&pastCities[t]>pastCities[newP]+1))
				{
					sumH[t] = sumH[newP]+h[t];//更新到达该城市的幸福值
					lastCity[t] = newP;//更新到达该城市的前驱城市  
					pastCities[t] = pastCities[newP]+1;//更新经过的城市数量 
				}  
			}
		}
		int min = 123123123;
		for(int i=0;i<N;i++){//寻找新的结点加入集合 
			if(visit[i]) continue;
			if(dis[i]==-1) continue;
			if(dis[i]<min){
				min = dis[i];
				newP = i;
			}
		}
		visit[newP] = true;
	} 
	cout<<paths[dest]<<" "<<dis[dest]<<" "<<sumH[dest]<<" "<<sumH[dest]/pastCities[dest]<<endl;
	dfs(dest);
	return 0;
}

 

QcarCamera-EVS HAL实现(一) 高通车载EVS实现 阅读详情

相关推荐

植物叶绿体基因组分析全攻略:从共线性到密码子偏好性

本文全面解析植物叶绿体基因组分析流程,从共线性分析到密码子偏好性研究。详细介绍了叶绿体基因组结构特征、测序数据获取与质量控制、基因组组装与注释方法,以及比较基因组学和进化分析技术。特别关注IR区边界变异分析和选择压力评估,为植物系统发育研究和物种鉴定提供实用指南。

weixin_28475533的博客 222

1087 All Roads Lead to Rome (30 point(s))

A1087 #include&lt;cstdio&gt; #include&lt;string&gt; #include&lt;map&gt; #include&lt;iostream&gt; using namespace std; const int maxn=1010; const int INF=1000000000; int N, K; map&lt;string, int&gt;s...

学习为主,偶尔扯淡 383

10分钟训练你的AI歌手:RVC WebUI声音克隆保姆级教程

本文提供了一份详细的RVC WebUI声音克隆保姆级教程,指导用户如何在10分钟内利用Retrieval-based-Voice-Conversion技术训练专属AI歌手。教程涵盖从环境配置、数据准备、模型训练到最终推理应用的全流程,即使是新手也能快速上手,实现高质量的声音克隆与歌曲生成。

weixin_29073241的博客 821

1087. 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

accbbbxx的博客 565

保姆级教程:手把手教你用记事本修改DNF私服PVF,打造毕业级自定义装备

本文提供了一份详细的DNF私服PVF文件修改教程,教你如何使用记事本轻松打造毕业级自定义装备。从PVF文件结构解析到基础属性修改,再到高级特效设置,手把手教你实现装备属性的全方位定制,无需编程基础即可完成。

weixin_42525385的博客 1205

零件边界线提取处理原理详解

该程序提出了一种高效的点云边界提取方法,通过三阶段处理流程实现零件几何特征的精确提取。首先采用RANSAC算法进行坐标系标准化,包括主平面检测、点云分割和双重旋转对齐。接着使用凹包算法和RANSAC直线拟合提取底面边界。最后通过多方向密度切片分析(Z向100片、X向50片),自动选择高密度切片进行边界提取和直线拟合。算法具有鲁棒性强、处理效率高(支持OpenMP并行计算)和参数可配置等特点,能有效提取零件的几何边界特征,为后续质量检测提供基础。实验结果显示该方法能准确提取零件的多方向边界线。

weixin_45196569的博客 433

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. 输入描述: Each inpu

s136424的博客 710

【知识图谱】neo4j安装与配置_社区版_2025(附完整安装软件包)

(经历过各种版本NEO4J,遇见杂七杂八的各种问题,也看过非常多很好的教程,特此来分享一下,为大家排排坑。

知识靠谱的博客 2万+

PAT A 1087 All Roads Lead to Rome (30 分)

### 一、思路 单源最短路径问题。 **直接使用map<string, int>存储数据求解担心会有超时问题,所以建立0~N - 1和城市缩写的映射关系,用整型城市索引处理。** 基于Dijkstra算法的路径选择: 若从点v去j代价更低:更新j路径前驱为v; 若从点v去j代价相同: 1、更新路径条数:n_path[j] += n_path[v],若写成n_path[j]++会导致测试点2答案错误。 2、路过v点的路线,幸福总量sum[]更大,更换为路过v点的路径。 2、幸福总量相同,但经过v点的路线

Siumai's Blog 912

第4章 部署负载均衡 Keepalived + HAProxy

本章在所有 Master 节点部署 Keepalived + HAProxy,为 kube-apiserver 提供高可用入口。Keepalived 实现 VIP(192.168.3.59)漂移,HAProxy 轮询转发请求至三个 Master 的 6443 端口(监听 6444 避免冲突)。健康检查脚本连续 5 次失败则停止 Keepalived,触发 VIP 自动切换。本章还包含配置详解、日常运维和问题排查。

幸福街--linux技术分享 121

智能门禁:AI Agent的访客意图识别

随着人工智能技术的飞速发展,智能门禁系统已经成为现代安全管理的关键环节。本文深入探讨了AI Agent在访客意图识别中的应用,通过详细的分析和实例讲解,揭示了智能门禁系统的核心工作原理、算法实现及其在安全领域的重要作用。文章结构清晰,逻辑严谨,旨在为广大技术爱好者提供一次全面的技术盛宴。在当今社会,门禁系统已成为各类建筑和机构不可或缺的安全设施。随着信息安全意识的增强,传统门禁系统的局限性逐渐显现。例如,在访客管理方面,传统方法通常依赖于人工登记、身份验证等手段,不仅效率低下,而且容易出现漏洞。

AI架构师小马 711

1087 All Roads Lead to Rome (30 point(s))

1087 All Roads Lead to Rome (30 point(s)) 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 conta

佳迪斯的博客 216

华为OD新系统机试真题 2026.5.10 - 美观的灯笼

华为OD新系统机试真题【美观的灯笼】,提供C语言、Java、C++、Python、Go、JavaScript源码实现,并提供完整思路讲解,以及在线OJ模拟做题。华为OD机试真题【美观的灯笼】

qq_45776114的博客 644

1087 All Roads Lead to Rome (30 point(s))

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 ...

张蔡辉 305

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

【PAT】A1087 All Roads Lead to Rome (30point(s))

文章目录A1087 All Roads Lead to Rome (30point(s))Input Specification:Output Specification:Sample Input:Sample Output:CodeAnalysis Author: CHEN, Yue Organization: 浙江大学 Time Limit: 200 ms Memory Limit: 64 M...

Duke 249

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

1087 All Roads Lead to Rome (30 point(s)) PAT甲级

1087 All Roads Lead to Rome (30 point(s)) Dijkstra+DFS 题目 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

neko的博客 228

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

1087 All Roads Lead to Rome (30point(s)) - C语言 PAT 甲级

Dijkstar 算法求出题目要求的最短路径,DFS 输出路径 * 需要用两个数组,映射城市名和编号 * 最短路径要求,路径最短,幸福值最大,平均幸福值最大

有三只土豆 186

PAT(甲级)1087 All Roads Lead to Rome (30point(s))

题目 题目链接 思路 题目大意:找出从出发点到罗马的花费最小的路径,如果最小路径存在多条,找到最快乐的那条路径,如果最快乐的路径不止一条,找到平均快乐值最大的那条,也就是一共有三个衡量标尺; 衡量标尺太多了,采用Dijktras + DFS 比较好,先用Dijktras找到所有的最短路径,在用DFS遍历所有最短路径,根据第二、第三标尺找到结果; 注意:使用vector时要记得初始化大小啊,害得我找了半天错,害!!! 代码 #include <iostream> #include <vect

hjyp的博客 205

PAT----A1087 All Roads Lead to Rome (30point(s))

A1087 All Roads Lead to Rome (30point(s)) 题意 找出花费最小并且获得最大幸福度的路径,如果不唯一则选择最短的。 思路 考察dijskra算法,这里用了优先队列。 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 ...

qq_43578870的博客 215

PAT_甲级_1087 All Roads Lead to Rome (30point(s)) (C++)【Dijkstra/DFS】

1,题目描述 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 ...

&再见萤火虫&的博客 328

[PAT]1087. All Roads Lead to Rome (30)

/************************************************************** 1087. All Roads Lead to Rome (30) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Indeed there are many different t

点十八的一方天地 2353

1087. All Roads Lead to Rome (30)【最短路】——PAT (Advanced Level) Practise

题目信息1087. All Roads Lead to Rome (30)时间限制200 ms 内存限制65536 kB 代码长度限制16000 B Indeed there are many different tourist routes from our city to Rome. You are supposed to find your clients the route with

4269

pat 1087. All Roads Lead to Rome (30)

1087. All Roads Lead to Rome (30)

nan327347465的专栏 3027

1087. All Roads Lead to Rome (30)(dijkstra + dfs统计)

1087. All Roads Lead to Rome (30) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...

codeswarrior的博客 392

pat甲级1087 All Roads Lead to Rome (30分)

A1087 经验 pat的用字符串作为顶点构图的题似乎都不会卡map的时间(目前没遇到过)。这个题做的比较成功,用时43min,几乎没有调试就过了。 #include <bits/stdc++.h> using namespace std; const int N = 205, inf = 0x3f3f3f3f; int n, k, id; map<string, int&g...

qq_43732110的博客 283

PAT甲组 1087. All Roads Lead to Rome (30)

https://www.patest.cn/contests/pat-a-practise/1087 1087. All Roads Lead to Rome (30) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者

智熄 363
上一篇: 1003 Emergency (25 point(s))(未完待续)
下一篇: 1083 List Grades (25 point(s))
coderwait
博客等级 码龄9年 20粉丝 210原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值