10423 전기가 부족해

정민용·2024년 3월 24일

백준

목록 보기
277/286

Prim Algorithm

import sys
import heapq
from collections import defaultdict

n, m, k = map(int, sys.stdin.readline().split())
visited = [0] * (n+1)
elect = list(map(int, sys.stdin.readline().split()))

graph = defaultdict(list)
for _ in range(m):
    u, v, w = map(int, sys.stdin.readline().split())
    graph[u].append([w, u, v])
    graph[v].append([w, v, u])

# 발전소를 시작 지점으로 해서 진행
candidate = []
for e in elect:
    visited[e] = 1
    for g in graph[e]:
        heapq.heappush(candidate, g)

total_weight = 0
while candidate:
    w, u, v = heapq.heappop(candidate)

    if visited[v] == 0:
        visited[v] = 1
        total_weight += w

        for edge in graph[v]:
            if visited[edge[2]] == 0:
                heapq.heappush(candidate, edge)

sys.stdout.write(str(total_weight))

10423 전기가 부족해

0개의 댓글