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

网站 app最新新闻实时新闻

网站 app,最新新闻实时新闻,广东城市建设档案馆官方网站,政府网站前台模板往期 【用deepseek和chatgpt做算法竞赛】——华为算法精英实战营第十九期-Minimum Cost Trees_0:介绍了题目和背景【用deepseek和chatgpt做算法竞赛】——华为算法精英实战营第十九期-Minimum Cost Trees_1:题目输入的格式说明,选择了邻接表…

往期

  • 【用deepseek和chatgpt做算法竞赛】——华为算法精英实战营第十九期-Minimum Cost Trees_0:介绍了题目和背景
  • 【用deepseek和chatgpt做算法竞赛】——华为算法精英实战营第十九期-Minimum Cost Trees_1:题目输入的格式说明,选择了邻接表来表示图
  • 【用deepseek和chatgpt做算法竞赛】——华为算法精英实战营第十九期-Minimum Cost Trees_2:介绍了邻接表,题目输出的格式说明
  • 【用deepseek和chatgpt做算法竞赛】——华为算法精英实战营第十九期-Minimum Cost Trees_3:期主要写一个初代程序,能完整一些简单的例子
  • 【用deepseek和chatgpt做算法竞赛】——ChatGPT还是不行呀 -Minimum Cost Trees_4:介绍了评分规则,让GPT输出了一个看着正确实际不行的代码

这一期,我选择用伪代码的方式与GPT沟通,让GPT做军师和程序员,一定要写一个有分的程序呀

0 基础程序

下面这个程序就是根据题目要求写的一个输入数据读取的程序

import sys
import heapq
from collections import defaultdictdef read_graph_from_file(file_path):""" 从文件读取图数据并解析 """with open(file_path, 'r') as f:lines = f.read().strip().split("\n")n = int(lines[0].strip())  # 节点数s = int(lines[1].strip())  # 源点k = int(lines[2].strip())  # 目标节点数terminals = list(map(int, lines[3].strip().split()))  # 目标节点列表D = int(lines[4].strip())  # 延迟约束m = int(lines[5].strip())  # 边数graph = defaultdict(list)for i in range(6, 6 + m):a, b, c, d = map(int, lines[i].strip().split())graph[a].append((b, c, d))  # 存储 a -> b 的边graph[b].append((a, c, d))  # 存储 b -> a 的边(无向图)return n, s, terminals, D, graphdef process_and_output(file_path):graph_data = read_graph_from_file(file_path)if not graph_data:returnif __name__ == "__main__":# 输入文件路径file_path = "../sample-cases/01"  # 请根据实际文件路径修改process_and_output(file_path)

1 ChatGPT思路

ChatGPT给的思路流程

  • 初始化图数据
  • 构建主树
  • 构建冗余树
  • 验证冗余树
  • 输出
    这个其实就有问题了,因为先构建出主树,为了找到一颗最好的主树,就很有可能把冗余树堵死了

2 从简单的开始

我们先尝试只找一棵树,即只要拿前两个条件的分

  • 单棵树: 如果是单棵树,得 5 分。
  • 满足延迟约束的单棵树: 如果树满足延迟约束,则得 10 分。
    很尴尬的事情发生了,ChatGPT崩了,接下的事情让deepseek来吧,我用的是浙江大学的满血版DeepSeek的“浙大先生”
    在这里插入图片描述
    实际使用还是有一些问题,就是输入框很小呀一次只能能看到一行呀,看不到我完整的输入,修改起来比较麻烦,但是是免费的就很棒,现在是2025年2月21日,希望后面能改进,感谢浙大!
    在这里插入图片描述
deepseek给出的代码如下

满足了简单的要求能找到一颗树了,且拿到了分

import sys
import heapq
from collections import defaultdictdef read_graph_from_file():""" 从标准输入读取图数据并解析 """lines = sys.stdin.read().strip().split("\n")n = int(lines[0].strip())  # 节点数s = int(lines[1].strip())  # 源点k = int(lines[2].strip())  # 目标节点数terminals = list(map(int, lines[3].strip().split()))  # 目标节点列表D = int(lines[4].strip())  # 延迟约束m = int(lines[5].strip())  # 边数graph = defaultdict(list)for i in range(6, 6 + m):a, b, c, d = map(int, lines[i].strip().split())graph[a].append((b, c, d))  # 存储 a -> b 的边graph[b].append((a, c, d))  # 存储 b -> a 的边(无向图)return n, s, terminals, D, graphdef dijkstra(graph, start, delay_constraint):""" 使用 Dijkstra 算法找到从 start 到所有节点的最短路径 """distances = {node: float('inf') for node in graph}distances[start] = 0heap = [(0, start)]path = {}while heap:current_distance, current_node = heapq.heappop(heap)if current_distance > delay_constraint:continuefor neighbor, cost, delay in graph[current_node]:distance = current_distance + delayif distance < distances[neighbor]:distances[neighbor] = distanceheapq.heappush(heap, (distance, neighbor))path[neighbor] = current_nodereturn pathdef build_tree(path, terminals):""" 根据路径构建树 """tree_edges = set()for terminal in terminals:current = terminalwhile current in path:parent = path[current]tree_edges.add((parent, current))current = parentreturn tree_edgesdef process_and_output():n, s, terminals, D, graph = read_graph_from_file()# 使用 Dijkstra 算法找到从 s 到所有节点的路径path = dijkstra(graph, s, D)# 构建树tree_edges = build_tree(path, terminals)# 检查是否满足延迟约束def is_delay_satisfied(tree_edges, graph, s, terminals, D):distances = {node: float('inf') for node in graph}distances[s] = 0heap = [(0, s)]while heap:current_distance, current_node = heapq.heappop(heap)for neighbor, cost, delay in graph[current_node]:if (current_node, neighbor) in tree_edges:distance = current_distance + delayif distance < distances[neighbor]:distances[neighbor] = distanceheapq.heappush(heap, (distance, neighbor))for terminal in terminals:if distances[terminal] > D:return Falsereturn Truedelay_satisfied = is_delay_satisfied(tree_edges, graph, s, terminals, D)# 输出结果print(1)  # 只输出一棵树print(len(tree_edges))  # 树中的边数for edge in tree_edges:print(edge[0], edge[1])# # 判断得分# if delay_satisfied:#     print("满足延迟约束的单棵树,得 10 分。")# else:#     print("单棵树,得 5 分。")if __name__ == "__main__":process_and_output()

恭喜恭喜有分了,虽然垫底,但迈出了第一步,祝贺;
在这里插入图片描述

目前得分是452893,下一期争取拿更高

http://www.yidumall.com/news/84610.html

相关文章:

  • 口碑好的网站建设多少钱高端网站建设哪个好
  • 网站建设推广需要多少钱做谷歌推广比较好的公司
  • 资阳视频网站建设如何做免费网站推广
  • 做外贸网站效果图搜索引擎优化原理
  • 百度seo营销网站电脑速成班短期电脑培训班
  • 东莞做网站价格惠州seo
  • 制作网站高手排名优化是怎么做的
  • 专业做网站咨询百度浏览器网址是多少
  • 北京都有那些做网站的公司优化大师win10能用吗
  • 太原网站建设策划方案网站排名推广工具
  • 国内亲子游做的最好的网站优化优化
  • 电子工程网络通信的专业课网站优化排名易下拉霸屏
  • 怎么看公司网站做的好不好哦搜一搜
  • 临朐网站建设百度怎样发布信息
  • 怎么做收费网站宁波seo外包快速推广
  • 泰安网站建设运营费用百度工具
  • 如何做制作头像的网站域名交易
  • 杭州网站建设免费html网站模板
  • 福州企业网站制作桔子seo工具
  • 南通网站搜索引擎优化高报师培训机构排名
  • 网站开发现状都用php怀来网站seo
  • 摄影师网站模板腾讯广点通广告投放平台
  • 网站开发投资成本b站刺激战场视频
  • 网站改版的几个建议淘宝网店怎么运营起来
  • 陶瓷网站开发背景营销的目的有哪些
  • wordpress 如何切换主题seo还有前景吗
  • 制作华为手机网站建设规划书黄页网络的推广网站有哪些软件
  • 常州便宜的做网站服务现在做网络推广好做吗
  • 哪些网站可以免费做简历百度代理公司查询
  • 做3d办公家具教程的网站seo入门