本题是标准的最小生成树问题,此处用Prim算法可得结果为$261832-2153=259679$。
d=[]
with open("107_network.txt") as f:
for line in f:
d.append([int(i) if i.isdigit() else 0 for i in line.strip().split(",")])
m=sum(sum(i) for i in d)/2
a=set([0])
b=set(range(1,40))
s=0
while len(a) < 40:
k=min([d[i][j],j] for i in a for j in b if d[i][j]>0)
s+=k[0]
a.add(k[1])
b.remove(k[1])
print m,s,m-s
|