第二题暴力。没分 n = int(input()) edges = [] matrix = [[float('inf')]*n for i in range(n)] for i in range(n-1): edge = [int(i) for i in input().split()] matrix[edge[0]-1][edge[1]-1]=edge[2] matrix[edge[1]-1][edge[0]-1]=edge[2] for i in range(n): matrix[i][i] = 0 for i in range(n): for j in range(n): for k in range(n): matrix[i][j]=min(matrix[i][j],matrix[i][k]+matrix[k][j]) nodelike = [] for i in range(3): nums = [int(i) for i in input().split()] nodelike.append(nums[1:]) res = 0 for i in nodelike[0]: for j in nodelike[1]: for k in nodelike[2]: onemin = float('inf') for z in range(n): onemin = min(onemin,matrix[i-1][z]+matrix[j-1][z]+matrix[k-1][z]) res += onemin print(res/(len(nodelike[0])*len(nodelike[1])*len(nodelike[2])))
点赞 3
牛客网
牛客企业服务