Bootstrap

【用deepseek和chatgpt做算法竞赛】——华为算法精英实战营第十九期-Minimum Cost Trees_1

往期【用deepseek和chatgpt做算法竞赛】——华为算法精英实战营第十九期-Minimum Cost Trees_0
上一期我们已经知道了题目的要求,这一期,

我们根据题目要求,提交一个初版的代码

0 输入描述

在这里插入图片描述
输入格式解释(根据提供的 Input 描述)

  1. 第一行:节点数量 𝑛(表示图中有 𝑛个节点 (顶点),节点编号为从 0 到 n-1,节点数范围:3 ≤ n ≤ 60,000)

  2. 第二行:源点 (起点) 𝑠(源点 (Source) 节点编号 s, 范围:0 ≤ s ≤ n − 1)

  3. 第三行:终端节点数量 𝑘(最少 1 个,最多为 30 个或 𝑛−1个中较小的值,因此最多不会超过 30 个目标点。)

  4. 第四行:终端节点编号列表 𝑡1,𝑡2,…,𝑡𝑘t

  5. 第五行:延迟约束 𝐷

  6. 第六行:边的数量 𝑚

  7. 接下来的 𝑚行(边的信息每条边具有:成本 (Cost) (范围:1 ≤ cᵢ ≤ 200)延迟 (Delay) 范围:1 ≤ dᵢ ≤ 4000)
    在这里插入图片描述

Python 读取输入的示例
下面是根据该输入格式读取数据并构建邻接表的 Python 代码:

from collections import defaultdict

def read_input():
    # 读取基本信息
    n = int(input().strip())           # 节点数
    s = int(input().strip())           # 源点
    k = int(input().strip())           # 目标节点数
    terminals = list(map(int, input().strip().split()))  # 目标节点列表
    D = int(input().strip())           # 延迟约束
    m = int(input().strip())           # 边数

    # 初始化邻接表
    graph = defaultdict(list)

    # 读取边并存储两条有向边
    for _ in range(m):
        a, b, c, d = map(int, input().strip().split())
        graph[a].append((b, c, d))  # a -> b
        graph[b].append((a, c, d))  # b -> a

    return n, s, terminals, D, graph

# 示例输入测试
import io
input_data = """5
0
2
3 4
1000
4
0 1 50 200
0 2 20 150
1 3 70 300
2 4 40 100
"""
input = io.StringIO(input_data).readline
n, s, terminals, D, graph = read_input()

print(f"节点数: {n}")
print(f"源点: {s}")
print(f"目标节点: {terminals}")
print(f"延迟约束: {D}")
print("\n邻接表:")
for node, edges in graph.items():
    print(f"{node} -> {edges}")

在这里插入图片描述

;