42 lines
859 B
Python
42 lines
859 B
Python
import heapq
|
|
import sys
|
|
|
|
input = sys.stdin.readline
|
|
|
|
def solve() :
|
|
N, M = map(int, input().split())
|
|
|
|
graph = [[] for _ in range(N+1)]
|
|
|
|
for _ in range(M) :
|
|
A, B, C = map(int, input().split())
|
|
graph[A].append((B, C))
|
|
graph[B].append((A, C))
|
|
|
|
pq = []
|
|
visited = [False] * (N+1)
|
|
heapq.heappush(pq, (0,1))
|
|
count = 0
|
|
totalCost = 0
|
|
maxCost = 0
|
|
|
|
while pq :
|
|
cost, node = heapq.heappop(pq)
|
|
|
|
if visited[node] : continue
|
|
|
|
visited[node] = True
|
|
count += 1
|
|
totalCost += cost
|
|
maxCost = max(cost, maxCost)
|
|
|
|
if count == N : break
|
|
|
|
for next, n_cost in graph[node] :
|
|
if not visited[next] :
|
|
heapq.heappush(pq, (n_cost, next))
|
|
|
|
print(totalCost-maxCost)
|
|
|
|
solve()
|
|
|