1150 Travelling Salesman Problem

本文深入探讨了图遍历算法中的关键概念,如简单路径和简单回路,并提供了一段Python代码示例,用于判断路径是否构成简单回路,是否遍历所有节点,以及计算最短距离。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

image

主要搞懂两个概念

  • 简单路径:除开始节点和结束节点可以相同外,其他的必须不同
  • 简单回路:开始节点和结束节点相同的简单路径

代码

这次超时了,得了评分是21分,通过集合判断是否是环和通过集合判断是否周游了全部节点显然太耗时了,这是改善的空间

n, m = map(int, input().split(" "))

graph = matrix = [([-1] * n) for i in range(n)]

for index in range(m):
    i, j, dist = map(int, input().split(" "))
    graph[i - 1][j - 1] = dist
    graph[j - 1][i - 1] = dist

path_num = int(input())
paths = []
nodes = set(range(1, n + 1))
min_ = 9999
ind = 0
for index in range(path_num):
    path = list(map(int, input().split(" ")))
    d = 0
    circle = False
    simple = False
    ts = False
    if path[1] == path[-1]:
        circle = True
    if circle:
        if len(set(path[1:-1])) == (path[0] - 1):
            simple = True
    else:
        if len(set(path[1:-1])) == path[0]:
            simple = True

    for i in range(path[0] - 1):
        if graph[path[i + 1] - 1][path[i + 2] - 1] == -1 or graph[path[i + 2] - 1][path[i + 1] - 1] == -1:
            d = "NA"
            circle = False
            break
        else:
            if graph[path[i + 1] - 1][path[i + 2] - 1] != -1:
                d = d + graph[path[i + 1] - 1][path[i + 2] - 1]
            else:
                d = d + graph[path[i + 2] - 1][path[i + 1] - 1]

    if set(path[1:-1]) == nodes:
        ts = True
    if circle and simple and ts:
        print("Path " + str(index + 1) + ": " + str(d) + " " + "(TS simple cycle)")
    elif circle and simple == False and ts:
        print("Path " + str(index + 1) + ": " + str(d) + " " + "(TS cycle)")
    else:
        print("Path " + str(index + 1) + ": " + str(d) + " " + "(Not a TS cycle)")
    if type(d) is int and circle and ts:
        if d < min_ and d > 0:
            min_ = d
            ind = index + 1
print("Shortest Dist(" + str(ind) + ") = " + str(min_) + "")
??正文结束??
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值