N, M = map(int, input().split())
Edges = []
for i in range(M):
start, end, weight = map(int, input().split())
Edges.append([weight, start, end])
Edges.sort()
Comp = [i for i in range(N)]
Ans = 0
for weight, start, end in Edges:
if Comp[start] != Comp[end]:
Ans += weight
a = Comp[start]
b = Comp[end]
for i in range(N):
if Comp[i] == b:
Comp[i] = a
该算法(以上)如何与包含邻接矩阵的文件组合?
从文件读取矩阵的代码:
with open('in.txt') as f:
matrix = [list(map(int, row.split())) for row in f.readlines()]