-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10-3.py
More file actions
50 lines (36 loc) · 1.11 KB
/
10-3.py
File metadata and controls
50 lines (36 loc) · 1.11 KB
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
45
46
47
48
49
50
"""
@file 10-3.py
@brief 도시 분할 계획
@desc 백준 1647번, 최소 신장 트리, 크루스칼 알고리즘
전체 그래프에서 2개의 최소 신장 트리 만들기
-> 크루스칼 알고리즘으로 최소 신장 트리 찾은 후 최소 신장 트리를 구성하는 간선 중에서 가장 비용이 큰 간선을 제거함
"""
def find_parent(parent, x):
if parent[x] != x:
parent[x] = find_parent(parent, parent[x])
return parent[x]
def union_parent(parent, a, b):
a = find_parent(parent, a)
b = find_parent(parent, b)
if a < b:
parent[b] = a
else:
parent[a] = b
v, e = map(int, input().split())
parent = [0] * (v + 1)
edges = []
result = 0
for i in range(1, v + 1):
parent[i] = i
for i in range(e):
a, b, cost = map(int, input().split())
edges.append((cost, a, b))
edges.sort()
last = 0 # 최소 신장 트리 간선 중 가장 비용이 큰 간선
for edge in edges:
cost, a, b = edge
if find_parent(parent, a) != find_parent(parent, b):
union_parent(parent, a, b)
result += cost
last = cost
print(result - last)