编程介的小学生 2019-12-31 18:17 采纳率: 0.2%
浏览 35

Treeland Exhibition 的算法

Problem Description
There are n towns in the treeland – they form a tree, as you may guess, i.e. there is a unique path between every pair of towns. The length of road connecting every pair of adjacent towns is always 1 unit.

You want to hold an exhibition simultaneously on no more than L+1 consecutive towns, i.e. you choose two towns u and v of no more than L unit apart, and set up your exhibition in all the towns on the unique path from u to v. You want to attract people from all over the treeland to your exhibition, so you’d like to minimize the sum of “travelling distance” from every town. The “travelling distance” of a town is the distance from that town to its closest exhibition-town.

Input
There are at most 25 test cases. Each case begins with two integers, n and L (n <= 10000, 0 < L <= 100), the number of towns, and the maximal distance of the “endpoint towns” you choose. Next n-1 lines contain the descriptions of connections, each with two integers a and b, indicating that town a and town b are directly connected (towns are numbered from 0 to n-1). The input ends with n = L = 0.

Output
For each test case, print the minimal sum of travelling distance.

Sample Input
3 1
0 1
1 2
4 1
0 1
1 2
2 3
0 0

Sample Output
1
2

  • 写回答

1条回答 默认 最新

  • 你知我知皆知 2024-07-27 09:33
    关注

    以下回答参考 皆我百晓生券券喵儿 等免费微信小程序相关内容作答,并由本人整理回复。

    # 读取数据
    input = readLines()
    
    # 解析输入
    n = int(input[1])
    L = int(input[2])
    
    if n == L == 0:
        ans = 0
    else:
        # 构建邻接表
        graph = [[] for _ in range(n)]
        for line in input[3:]:
            a, b = map(int, line.split())
            graph[a].append(b)
            graph[b].append(a)
    
        # 距离计算
        def dijkstra(start):
            dist = [float('inf')] * n
            prev = [-1] * n
            dist[start] = 0
            queue = [(0, start)]
    
            while queue:
                cur_dist, node = heappop(queue)
                if cur_dist > dist[node]:
                    continue
    
                for next_node in graph[node]:
                    new_dist = cur_dist + 1
                    if new_dist < dist[next_node]:
                        dist[next_node] = new_dist
                        prev[next_node] = node
                        heappush(queue, (new_dist, next_node))
    
            return dist, prev
    
        # 找到起点与终点的最短距离和前驱节点
        start, end = dijkstra(0)[0]
        start_prev = dijkstra(start)[1][end]
    
        # 计算最小总距离
        total_distance = 0
        for i in range(1, n - 1):
            current_distance = 0
            while True:
                if start_prev[i] != -1:
                    current_distance += 1
                    start = start_prev[i]
                    start_prev[i] = -1
                else:
                    break
            total_distance += current_distance
        ans = total_distance
    
    print(ans)  
    
    评论

报告相同问题?

悬赏问题

  • ¥15 已知平面坐标系(非直角坐标系)内三个点的坐标,反求两坐标轴的夹角
  • ¥15 webots有问题,无响应
  • ¥15 数据量少可以用MK趋势分析吗
  • ¥15 使用VH6501干扰RTR位,CANoe上显示的错误帧不足32个就进入bus off快慢恢复,为什么?
  • ¥15 大智慧怎么编写一个选股程序
  • ¥100 python 调用 cgps 命令获取 实时位置信息
  • ¥15 两台交换机分别是trunk接口和access接口为何无法通信,通信过程是如何?
  • ¥15 C语言使用vscode编码错误
  • ¥15 用KSV5转成本时,如何不生成那笔中间凭证
  • ¥20 ensp怎么配置让PC1和PC2通讯上