forked from tony9402/baekjoon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
44 lines (39 loc) · 911 Bytes
/
main.py
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
32
33
34
35
36
37
38
39
40
41
42
43
44
# Authored by : gusdn3477
# Co-authored by : -
# Link : http://boj.kr/eca68e03c7f640158b9c6a6841997682
import sys
def input():
return sys.stdin.readline().rstrip()
def union(x,y):
x, y = find_parent(x), find_parent(y)
if x < y:
parent[y] = x
else:
parent[x] = y
def find_parent(x):
if x != parent[x]:
parent[x] = find_parent(parent[x])
return parent[x]
N = int(input())
parent = [i for i in range(N+1)]
arr = []
ans = []
total = 0
ct = 0
for i in range(N):
arr.append(list(map(int, input().split())))
for i in range(N):
for j in range(N):
if i == j:
continue
ans.append((i,j,arr[i][j]))
ans.sort(key = lambda x : x[2])
for i in range(N*(N-1)):
a,b = find_parent(ans[i][0]), find_parent(ans[i][1])
if a != b:
union(a,b)
total += ans[i][2]
ct += 1
if ct == N-1:
break
print(total)