蓝桥杯 安慰奶牛Python版

 

nodenum,edgenum = map(int,input().split())
vexlist = []
for i in range(nodenum):
c = eval(input())
vexlist.append(c)
edgelist = []
for j in range(edgenum):
s,e,l = map(int,input().split())
s = s-1 #注意此处,会影响后面的group取值
e = e-1
l = l * 2 + vexlist[s]+ vexlist[e]
edgelist.append([s,e,l])
res = []
edgelist.sort(key=lambda a:a[2])
group = [[i] for i in range(nodenum)]
for edge in edgelist:
for i in range(len(group)):
if edge[0] in group[i]:
m = i
if edge[1] in group[i]:
n = i
if m != n:
res.append(edge)
group[m] += group[n]
group[n] = []
minsum = min(vexlist)
for edge in res:
minsum += edge[2]
print(minsum)