Post

[BOJ] 14950 - 정복자

[백준] 14950 - 정복자

기본적인 MST문제다. 딱히 주의할 사항도 없다.

소스코드
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
import heapq

n, m, t = map(int, input().split())
parent = [i for i in range(n+5)]
pq = []

def find(x):
    if parent[x] == x:
        return x
    parent[x] = find(parent[x])
    return parent[x]

def union(x, y):
    parent[find(y)] = find(x)
    find(y)

for i in range(m):
    a, b, c = map(int, input().split())
    heapq.heappush(pq, (c, a, b))

ans = 0
i = 0

while pq:
    c, a, b = map(int, heapq.heappop(pq))
    if find(a) != find(b):
        union(a,b)
        ans += c + i*t
        i+=1

print(ans)
This post is licensed under CC BY 4.0 by the author.

Comments powered by Disqus.