360笔试求解
有没有大佬贴一份老张修路(最小生成树)题解,暴力会超时呜呜呜。
菜鸡暴力解:
n,m = input().split() n,m = int(n),int(m) nodes1 = list(map(int,input().split())) nodes2 = list(map(int,input().split())) distance = list(map(int,input().split())) mat = [[0]*n for _ in range(n)] tmp = sum(distance) min_dis = tmp def dfs(node,visited,dis): global tmp_min if len(visited) == n:return dis dic_tmp = mat[node] for k,v in enumerate(dic_tmp): if k not in visited: dis += v visited.add(k) tmp_min = min(dfs(k,visited,dis),tmp_min) dis -= v visited.remove(k) return tmp_min for i in range(n): mat[nodes1[i]-1][nodes2[i]-1] = distance[i] mat[nodes2[i]-1][nodes1[i]-1] = distance[i] for node in range(n): visited = set() visited.add(node) tmp_min = tmp x = dfs(node, visited, 0) min_dis = min(x,min_dis) print(min_dis)