Buggy python submission (verdict: Runtime Error):
from collections import defaultdict
INF = 9999999999999999999999999999

def spanning_tree(G):
  n = len(G)
  T = {}
  added = set()
  min_c = INF
  for i in G:
    for j in G[i]:
      if G[i][j] < min_c:
        min_c = G[i][j]
        min_e = (i, j)
  T[min_e] = min_c
  added.add(min_e[0])
  added.add(min_e[1])
  while len(added) < n:
    min_c = INF
    for u in added:
      for v in G[u]:
        if v in added:
          continue
        if G[u][v] < min_c:
          min_c = G[u][v]
          min_e = (u, v)
    T[min_e] = min_c
    added.add(min_e[1])
  return T


G = {}

n, m = [int(v) for v in input().strip().split(' ')]
for i in range(n):
  G[i] = {}
for _ in range(m):
  s, t, c = [int(v) for v in input().strip().split(' ')]
  G[s][t] = c
  G[t][s] = c

T = spanning_tree(G)

s = 0
for e in T:
  s += T[e]

print(s)

Fixed submission:
from collections import defaultdict
from heapq import heappush, heappop
INF = 9999999999999999999999999999

def spanning_tree(G):
  n = len(G)
  T = {}
  added = set([0])
  heap = []
  for v in G[0]:
    heappush(heap, (G[0][v], (0, v)))
  while len(added) < n:
    while True:
      c, e = heappop(heap)
      if e[1] not in added:
        break
    T[e] = c
    added.add(e[1])
    for v in G[e[1]]:
      heappush(heap, (G[e[1]][v], (e[1], v)))

  return T


G = {}

n, m = [int(v) for v in input().strip().split(' ')]
for i in range(n):
  G[i] = {}
for _ in range(m):
  s, t, c = [int(v) for v in input().strip().split(' ')]
  G[s][t] = c
  G[t][s] = c

if m == 0:
  print(0)
  exit()

T = spanning_tree(G)

s = 0
for e in T:
  s += T[e]

print(s)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict
INF = 9999999999999999999999999999

def spanning_tree(G):
  n = len(G)
  T = {}
  added = set()
  min_c = INF
  for i in G:
    for j in G[i]:
      if G[i][j] < min_c:
        min_c = G[i][j]
        min_e = (i, j)
  T[min_e] = min_c
  added.add(min_e[0])
  added.add(min_e[1])
  while len(added) < n:
    min_c = INF
    for u in added:
      for v in G[u]:
        if v in added:
          continue
        if G[u][v] < min_c:
          min_c = G[u][v]
          min_e = (u, v)
    T[min_e] = min_c
    added.add(min_e[1])
  return T


G = {}

n, m = [int(v) for v in input().strip().split(' ')]
for i in range(n):
  G[i] = {}
for _ in range(m):
  s, t, c = [int(v) for v in input().strip().split(' ')]
  G[s][t] = c
  G[t][s] = c

if m == 0:
  print(0)
  exit()

T = spanning_tree(G)

s = 0
for e in T:
  s += T[e]

print(s)

Fixed submission:
from collections import defaultdict
from heapq import heappush, heappop
INF = 9999999999999999999999999999

def spanning_tree(G):
  n = len(G)
  T = {}
  added = set([0])
  heap = []
  for v in G[0]:
    heappush(heap, (G[0][v], (0, v)))
  while len(added) < n:
    while True:
      c, e = heappop(heap)
      if e[1] not in added:
        break
    T[e] = c
    added.add(e[1])
    for v in G[e[1]]:
      heappush(heap, (G[e[1]][v], (e[1], v)))

  return T


G = {}

n, m = [int(v) for v in input().strip().split(' ')]
for i in range(n):
  G[i] = {}
for _ in range(m):
  s, t, c = [int(v) for v in input().strip().split(' ')]
  G[s][t] = c
  G[t][s] = c

if m == 0:
  print(0)
  exit()

T = spanning_tree(G)

s = 0
for e in T:
  s += T[e]

print(s)