Go: package main

import (
	"bufio"
	"container/heap"
	"fmt"
	"os"
	"strconv"
)

type Node struct {
	id     int
	weight int
}

func init() {
	sc.Split(bufio.ScanWords)
}

type NodeHeap []Node

func (h NodeHeap) Len() int           { return len(h) }
func (h NodeHeap) Less(i, j int) bool { return h[i].weight < h[j].weight }
func (h NodeHeap) Swap(i, j int)      { h[i], h[j] = h[j], h[i] }

func (h *NodeHeap) Push(x interface{}) {
	*h = append(*h, x.(Node))
}

func (h *NodeHeap) Pop() interface{} {
	old := *h
	n := len(old)
	x := old[n-1]
	*h = old[0 : n-1]
	return x
}

var sc = bufio.NewScanner(os.Stdin)

func scanInt() int {
	num, _ := strconv.Atoi(scanString())
	return num
}

func scanString() string {
	sc.Scan()
	return sc.Text()
}

func init() {
	sc.Split(bufio.ScanWords)
}

func main() {
	n := scanInt()

	list := make([][]int, n)
	seen := make([]bool, n)
	for i := range list {
		list[i] = make([]int, n)
		for j := range list[i] {
			list[i][j] = scanInt()
		}
	}

	start := Node{id: 0, weight: 0}
	h := &NodeHeap{start}
	heap.Init(h)

	sum := 0
	for h.Len() > 0 {
		node := heap.Pop(h).(Node)
		if seen[node.id] {
			continue
		}
		seen[node.id] = true
		sum += node.weight

		for i, w := range list[node.id] {
			if w < 0 {
				continue
			}
			heap.Push(h, Node{id: i, weight: w})
		}
	}

	fmt.Println(sum)
}

Python: import heapq

n = int(input())
A = [[int(i) for i in input().split()] for i in range(n)]
h = []
DP = {}
heapq.heappush(h,(0,0))
c = 0
while(len(DP)<n):
    tmp = heapq.heappop(h)
    c += tmp[0]
    DP[tmp[1]] = 1
    for i,j in enumerate(A[tmp[1]]):
        if j > 0 and not i in DP:
            heapq.heappush(h,(j,i))
print(c)

Python: def kruskal(n):
    cost = []
    t = set()
    while edge:
        cmin = min(weight)
        index = weight.index(cmin)
        e = edge[index]
        del weight[index]
        del edge[index]
        if not e <= t:
            cost.append(cmin)
            t |= e
    return sum(cost)

n = int(input())
edge = []
weight = []
for i in range(n):
    for j, c in enumerate(map(int, input().split())):
        if c != -1 and not {i, j} in weight:
            edge.append({i, j})
            weight.append(c)
print(kruskal(n))

Python: n=int(input())
edgeList=[]
for i in range(n):
 a=list(map(int,input().split()))
 for j in range(i):
  if a[j]>=0:edgeList+=[(a[j],i,j)]
rootList=[-1]*n
sumLength=0
def getRoot(x):
 r=rootList[x]
 if r<0:rootList[x]=x
 elif r!=x:rootList[x]=getRoot(r)
 return rootList[x]
edgeList.sort(key=lambda x:x[0])
for e in edgeList:
 x=getRoot(e[1])
 y=getRoot(e[2])
 if x!=y:
  sumLength+=e[0]
  rootList[x]=rootList[y]=min(x,y)
print(sumLength)

Python: n = int(input())
edgeList = []
for i in range(n):
    a = list(map(int, input().split()))
    for j in range(i):
        if a[j] != -1:
            edgeList.append([a[j], i, j])
rootList = [-1]*n
sumLength = 0

def getRoot(x):
    r = rootList[x]
    if r < 0:
        rootList[x] = x
    elif r != x:
        rootList[x] = getRoot(r)
    return rootList[x]
        

edgeList.sort(key=lambda x: x[0])
for e in edgeList:
    x = getRoot(e[1])
    y = getRoot(e[2])
    if x != y:
        sumLength += e[0]
        rootList[x] = rootList[y] = min(x, y)
print(sumLength)

Python: import heapq
from collections import defaultdict
v_num = int(input())
edges = defaultdict(list)
for i in range(v_num):
    data = [int(n) for n in input().split(" ")[1:]]
    for j in range(i + 1, v_num):
        if data[j] != -1:
            edges[i].append([data[j], j])
            edges[j].append([data[j], i])
queue = []
heapq.heapify(queue)
for c in edges[0]:
    heapq.heappush(queue, c)
visited = {0}
answer = 0
while len(visited) < v_num:
    q = heapq.heappop(queue)
    weight, one = q[0], q[1]
    if one in visited:
        continue
    answer += weight
    for e in edges[one]:
        if e[1] not in visited:
            heapq.heappush(queue, e)
    visited |= {one}
print(answer)

Python: 
def judge_reachable(graph,start,end,flag):
    flag[start]=1
    for vertex in graph[start]:
        if(flag[vertex]==1):
            continue
        if (vertex==end):
            return 1
        if(judge_reachable(graph,vertex,end,flag)):
            return 1
    return 0



graph_n=int(input())


edge_dict={}



for i in range(graph_n):
    tmp_row=list(map(int,input().split()))
    for j in range(graph_n):
        if(tmp_row[j]!=-1):
            try:
                edge_dict[tmp_row[j]].append([i,j])
            except:
                edge_dict[tmp_row[j]]=[[i,j]]


sorted_edge=sorted(edge_dict.items())


graph=[[] for i in range(graph_n)]

edge_num=0
cost=0

for edges in sorted_edge:
    for edge in edges[1]:
        flag=[0]*graph_n
        if(judge_reachable(graph,edge[0],edge[1],flag)==0):
            graph[edge[0]].append(edge[1])
            graph[edge[1]].append(edge[0])
            edge_num+=1
            cost+=edges[0]
            if(edge_num ==graph_n - 1):
                break    
    if(edge_num ==graph_n -1):
        break

print(cost)

Python: n = int(input())
edgeList = []


def getRoot(x):
    r = rootList[x]
    if r < 0:
        rootList[x] = x
    elif r != x:
        rootList[x] = getRoot(r)
    return rootList[x]
        
for i in range(n):
    a = list(map(int, input().split()))
    for j in range(i):
        if a[j] != -1:
            edgeList.append([a[j], getRoot(i), getRoot(j)])
rootList = [-1 for i in range(n)]
sumLength = 0
edgeList.sort(key=lambda x: x[0])
for e in edgeList:
    x = getRoot(e[1])
    y = getRoot(e[2])
    if x != y:
        sumLength += e[0]
        rootList[x] = rootList[y] = min(x, y)
print(sumLength)

Python: #coding:utf-8

def prim(n,G):
    dist=0
    visited=[False]*n
    cost=[1000000]*n
    pi=[0]*n
    cost[0]=0
    pi[0]=-1
    while True:
        mincost=1000000
        u=0
        for i in range(n):
            if visited[i]==False and cost[i]<mincost:
                mincost=cost[i]
                u=i
        if mincost==1000000:
            break
        visited[u]=True

        for i in range(n):
            if visited[i]==True or G[u][i]==-1:
                continue
            if G[u][i]<cost[i]:
                cost[i]=G[u][i]
                pi[i]=u
    print(sum(cost))
    
if __name__=="__main__":
    n=int(input())
    G=[]
    for i in range(n):
        G.append(list(map(int,input().split())))
    prim(n,G)