当前位置: 首页 > news >正文

菊厂20250416软件机考T2解答(200分)

题目

已知某市运营了N条地铁线路,市民在乘坐地铁时单条线路通票2元,换乘一次加1元。给出N条线路的所有站名列表,请帮乘客寻找从出发站到目的站最便宜的地铁换乘方案,并输出票价。每条地铁线路不包含环路,即没有相同站名。

输入:

第一行为地铁线路条数N,范围是[1, 1000]

第二行到第N+1行,每条线路依次包含的站名,每个站名包含的字符个数不超过100,站点个数也不超过100,依次用空格隔开,不同线路中相同的站点名表示是一个换乘站

第N+2行,出发站和目的站,用空格隔开

输入保证:若可达则为唯一解

输出:

第一行按出发站-换乘站(可以是多个)-目的站的格式输出换乘方案的字符串

第二行输出换乘方案的总票价

如果没有任何方案实现出发站到目的站,只输出一行:NA

样例:

输入:

3
A B C D F
C E G H 
B G I J
A J

输出:

A-B-J
3

解释:一号线有A,B,C,D,F五个站点,二号线有C,E,G,H四个站点,三号线有B,G,I,J四个站点。从A站到J站的最省钱换乘路线是经B站换乘,票价3元

代码

思路:输入处理——站点与线路建立映射——直接到达检查——邻接表构建——BFS——路径回溯与打印输出

from collections import defaultdict, deque
# If you need to import additional packages or classes, please import here.def func():N = int(input())lines = []for i in range(N):line = list(map(str, input().split()))lines.append(line)start, end = list(map(str, input().split()))station_to_lines = defaultdict(list)for line_idx, stations in enumerate(lines):for s in stations:station_to_lines[s].append(line_idx)found_direct = Falsefor line_idx in station_to_lines[start]:if end in lines[line_idx]:print(f"{start}-{end}")print(2)exit()adj_with_s = defaultdict(list)for s in station_to_lines:line_list = station_to_lines[s]for i in range(len(line_list)):for j in range(i + 1, len(line_list)):l1 = line_list[i]l2 = line_list[j]adj_with_s[l1].append((l2, s))adj_with_s[l2].append((l1, s))start_lines = station_to_lines[start]end_lines = set(station_to_lines[end])dist = [-1] * Nprev = {}q = deque()for line in start_lines:dist[line] = 0q.append(line)target_line = -1found = Falsewhile q:current_line = q.popleft()if current_line in end_lines:target_line = current_linefound = Truebreakfor neighbor_line, s in adj_with_s.get(current_line, []):if dist[neighbor_line] == -1 or dist[current_line] + 1 < dist[neighbor_line]:dist[neighbor_line] = dist[current_line] + 1prev[neighbor_line] = (current_line, s)q.append(neighbor_line)if not found:print("NA")exit()else:path = []current_line = target_linewhile current_line in prev:current_line, s = prev[current_line]path.append(s)path.reverse()res_path = startfor i in range(len(path)):res_path = res_path + '-' + path[i]res_path = res_path + '-' + endprint(res_path)k = len(path)cost = k + 2print(cost)# please define the python3 input here. For example: a,b = map(int, input().strip().split())# please finish the function body here.# please define the python3 output here. For example: print().if __name__ == "__main__":func()

相关文章:

  • GR00T N1:面向通用类人机器人的开放基础模型
  • Google Cloud最新报告:全球领先企业AI应用案例与智能体落地方向探索
  • 利用Global.asax在ASP.NET Web应用中实现功能
  • 绿盟二面面试题
  • 概念实践极速入门 - 常用的设计模式 - 简单生活例子
  • 在 Redis Lua 脚本中,keyCount 参数的作用是明确区分脚本参数中的 KEYS 和 ARGV,具体关系如下:
  • JNI的使用
  • 华为OD机试真题——硬件产品销售方案(2025A卷:100分)Java/python/JavaScript/C++/C语言/GO六种最佳实现
  • 设计模式(责任链模式)
  • SyntaxError: Unexpected end of JSON input 内容被截断报错
  • ios接入穿山甲【Swift】
  • 自动驾驶创建场景(just soso)
  • 用 Iris数据做决策树分析
  • 充电宝项目:规则引擎Drools学习
  • 【正点原子STM32MP257连载】第四章 ATK-DLMP257B功能测试——EEPROM、SPI FLASH测试 #AT24C64 #W25Q128
  • 黑马商城项目(二) Docker
  • Docker 中多个容器之间的通信
  • (Linux操作系统)自定义shell的实现
  • react使用createFromIconfontCN,自定义iconfont 图标
  • 艺术字体AI生成阿里云WordArt锦书、通义万相、SiliconFlow、Pillow+OpenCV本地生成艺术字体
  • 广州网站建设推广/公司要做seo
  • 个人做盈利慈善网站/北京搜索优化排名公司
  • 尤溪建设局网站/win7运行速度提高90%
  • 二级域名网站建设/自己建网站
  • 广州建设工程中心网站/公司网站制作
  • 网站建设用户登录/安卓手机优化大师官方下载