map int input split sum for in range append for in range map int input

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
n, m = map(int, input().split())
a = []
W = []
sum = 0
for i in range(n):
W.append([0]*n)
for i in range(m):
A, B, C = map(int, input().split())
W[A - 1][B - 1] = C
W[B - 1][A - 1] = C
INF = float('inf')
ostov = []
col = [i for i in range(n)]
for k in range(n - 1):
minDist = 1e10
for i in range(n):
for j in range(n):
if W[i][j] == 0:
W[i][j] = INF
if col[i] != col[j] and W[i][j] < minDist:
iMin = i
jMin = j
minDist = W[i][j]
sum += minDist
ostov.append((iMin, jMin))
c = col[jMin]
for i in range(n):
if col[i] == c:
col[i] = col[iMin]
print(sum)