๋ฌธ์
๋ฐฑ์ค ์จ๋ผ์ธ ์ ์ง - 6497๋ฒ
ํ์ด ๊ณผ์
๋ชจ๋ ๋์๋ฅผ ์ต์ ๋น์ฉ์ผ๋ก ์ฐ๊ฒฐํ๋ ๋ฐฉ๋ฒ์ ์ฐพ๋ ๋ฌธ์ ์
๋๋ค.ํฌ๋ฃจ์ค์นผ ์๊ณ ๋ฆฌ์ฆ
์ ํ์ฉํด์ MST
๋ฅผ ๊ตฌ์ถํ๊ณ ์ ์ฝ ๊ฐ๋ฅํ ๋น์ฉ์ ๊ณ์ฐํ๋ฉด ๋ฉ๋๋ค.
์ฝ๋
import sys
import heapq
sys.setrecursionlimit(10**6)
parent = []
def union(u, v):
u, v = find(u), find(v)
if u == v:
return
parent[u] = v
def find(u):
if parent[u] == u:
return u
parent[u] = find(parent[u])
return parent[u]
def solution(N, edges):
ret = 0
for _ in range(N):
cost, src, dst = heapq.heappop(edges)
if find(src) != find(dst):
ret += cost
union(src, dst)
return ret
if __name__ == '__main__':
while 1:
M, N = list(map(int, sys.stdin.readline().split()))
if M == 0:
break
total = 0
edges = []
for _ in range(N):
src, dst, cost = list(map(int, sys.stdin.readline().split()))
edges.append([cost, src, dst])
total += cost
parent = [i for i in range(M)]
heapq.heapify(edges)
answer = total - solution(N, edges)
print(answer)
๋ฐ์ํ
'๐ algorithm > boj' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
BOJ 17086 - ์๊ธฐ ์์ด2 (0) | 2021.03.08 |
---|---|
BOJ 13335 - ํธ๋ญ (0) | 2021.03.08 |
BOJ 6156 - Cow Contest (0) | 2021.03.08 |
BOJ 9625 - BABBA (0) | 2021.03.08 |
BOJ 11952 - ์ข๋น (0) | 2021.03.08 |
๐ฌ ๋๊ธ