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

【代码随想录算法训练营——Day60】图论——94.城市间货物运输I、95.城市间货物运输II、96.城市间货物运输III

卡码网题目链接
https://kamacoder.com/problempage.php?pid=1152
https://kamacoder.com/problempage.php?pid=1153
https://kamacoder.com/problempage.php?pid=1154

题解
94.城市间货物运输I
和昨天那题思路类似,加了个队列优化,仿照着c++代码写的。

95.城市间货物运输II
具体看题解,代码也是仿照题解写的。

96.城市间货物运输III
看题解的感悟:理解思路挺重要,还有了4个拓展,联系了前几天的算法。
代码部分:要加updated更新记录,防止时间超限,题解也是这么写的

代码

#94.城市间货物运输I
from collections import deque
class Edge():def __init__(self, to, val):self.to = toself.val = valif __name__ == "__main__":n, m = map(int, input().split())graph = [[] for _ in range(n + 1)]isInQueue = [False] * (n + 1)for _ in range(m):s, t, v = map(int, input().split())graph[s].append(Edge(t, v))start = 1end = nminDist = [float('inf')] * (n + 1)minDist[start] = 0queue = deque()queue.append(start)isInQueue[start] = Truewhile queue:node = queue.popleft()isInQueue[node] = Falsefor edge in graph[node]:fromm, to, val = node, edge.to, edge.valif minDist[to] > minDist[fromm] + val:minDist[to] = minDist[fromm] + valif isInQueue[to] == False:queue.append(to)isInQueue[to] = Trueif minDist[end] == float('inf'):print("unconnected")else:print(minDist[end])
#95.城市间货物运输II
if __name__ == "__main__":n, m = map(int, input().split())graph = []for _ in range(m):graph.append(list(map(int, input().split())))minDist = [float('inf')] * (n + 1)start, end = 1, nminDist[start] = 0flag = Falsefor i in range(1, n + 1): # 多循环一次for side in graph:fromm, to, val = side[0], side[1], side[2]if i < n:if minDist[fromm] != float('inf') and minDist[to] > minDist[fromm] + val:minDist[to] = minDist[fromm] + valelse:if minDist[fromm] != float('inf') and minDist[to] > minDist[fromm] + val:flag = Trueif flag:print("circle")elif minDist[end] == float('inf'):print("unconnected")else:print(minDist[end])
#96.城市间货物运输III
if __name__ == "__main__":n, m = map(int, input().split())graph = []for _ in range(m):graph.append(list(map(int, input().split())))src, dst, k = map(int, input().split())minDist = [float('inf')] * (n + 1)minDist[src] = 0for i in range(k + 1):updated = FalseminDist_copy = minDist.copy()for side in graph:fromm, to, val = side[0], side[1], side[2]if minDist_copy[fromm] != float('inf') and minDist[to] > minDist_copy[fromm] + val:minDist[to] = minDist_copy[fromm] + valupdated = Trueif not updated:breakif minDist[dst] == float('inf'):print("unreachable")else:print(minDist[dst])
http://www.dtcms.com/a/577730.html

相关文章:

  • 用C++编写一个PCL可视化交互操作的简单范例
  • 建设部网站官工程质量手册农村自建房设计图120平方二层
  • 南京网站推广费用网站宣传文案有哪些
  • 安防监控领域中常用设备AI枪机摄像机
  • matlab 命令pdist, pdist2
  • 有效的括号详解 | C语言用动态数组实现栈解决
  • 2024年上半年试题一:论大数据lambda架构
  • 北斗GNSS位移监测是什么?主要有哪几种应用?
  • 【芯片设计中的时序约束:Multicycle Path与False Path深度解析】
  • 学院网站建设需求分析调研表wordpress做dropping
  • centos7利docker compose 快速部署 Elasticsearch + Kibana
  • 网站流量建设设计广告设计
  • 个体工商户可以搞网站建设免费人脉推广
  • 谷歌浏览器Google Chrome离线安装包
  • Profinet IO从站数据 转IEC104项目案例
  • 嵌入式学习笔记 - SH79F6441芯片之8051的寻址空间,位寻址与字节地址寻址
  • 项目推荐:BettaFish (微舆) - 当多智能体遇上“论坛”协作机制
  • 跑通Visual-RFT报错解决记录
  • 学习网站二次开发如何自己设置网站
  • 自定义配置打印参数,进行打印功能
  • 免费看电视的网站有哪些深圳响应式网站价格
  • 如何给网站划分栏目利用html5 监控网站性能
  • MySQL原生账号权限管理
  • C#中关于ContextMenuStrip批量添加Item的问题
  • 从线程基础到线程池
  • Android selinux policy单独编译与调试
  • XML介绍
  • 营销推广运营 网站wordpress关闭主循环
  • Linux MTD系统深度解析:从原理到实践
  • css第二天