Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

const (
	MAX_N      = 200
	MAX_WEIGHT = 2000
)

type Edge struct {
	start  int
	end    int
	weight int
}

type ShortestInfo struct {
	edges    []Edge
	shortest int
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	n, err := nextInt(sc)
	if err != nil {
		panic(err)
	}

	shortestList := make([]ShortestInfo, n)
	for i := 1; i < n; i++ {
		shortestList[i] = ShortestInfo{edges: []Edge{}, shortest: MAX_N*MAX_WEIGHT + 1}
	}

	adjacentMatrix := make([][]int, n)
	for i := 0; i < n; i++ {
		adjacentMatrix[i] = make([]int, n)
		for j := 0; j < n; j++ {
			v, err := nextInt(sc)
			if err != nil {
				panic(err)
			}
			adjacentMatrix[i][j] = v
		}
	}

	mst([]int{1}, shortestList, adjacentMatrix)
	fmt.Println(shortestList)
	fmt.Println(calculateMinWeight(shortestList))
}

func calculateMinWeight(shortestList []ShortestInfo) int {
	visitedList := make([]bool, len(shortestList))
	visitedList[0] = true
	sumWeight := 0
	for !visitedAll(visitedList) {
		shortestInfo := shortestList[0]
		shortestList = shortestList[1:]
		edges := shortestInfo.edges
		for _, edge := range edges {
			if !visitedList[edge.end-1] {
				visitedList[edge.end-1] = true
				sumWeight += edge.weight
			}
		}
	}
	return sumWeight
}

func visitedAll(visitedList []bool) bool {
	for _, v := range visitedList {
		if !v {
			return false
		}
	}
	return true
}

func mst(nodeStack []int, shortestList []ShortestInfo, adjacentMatrix [][]int) {
	for len(nodeStack) > 0 {
		currentNode := nodeStack[0]
		nodeStack = nodeStack[1:]
		currentShortest := getShortest(currentNode, shortestList)
		adjacents := findAdjacents(currentNode, adjacentMatrix)
		for i := 0; i < len(adjacents); i++ {
			adj := adjacents[i]
			s := getShortest(adj.end, shortestList)
			if currentShortest.shortest+adj.weight < s.shortest || (currentShortest.shortest+adj.weight == s.shortest && len(currentShortest.edges)+1 > len(s.edges)) {
				setShortest(adj.end, adj, currentShortest, shortestList)
				nodeStack = append(nodeStack, adj.end)
			}
		}
	}
}

func getShortest(node int, shortestList []ShortestInfo) ShortestInfo {
	return shortestList[node-1]
}

func setShortest(node int, edge Edge, currentShortest ShortestInfo, shortestList []ShortestInfo) {
	newEdges := make([]Edge, len(currentShortest.edges)+1)
	for i := 0; i < len(newEdges)-1; i++ {
		newEdges[i] = currentShortest.edges[i]
	}
	newEdges[len(newEdges)-1] = edge
	shortestInfo := ShortestInfo{edges: newEdges, shortest: currentShortest.shortest + edge.weight}
	shortestList[node-1] = shortestInfo
}

func findAdjacents(currentNode int, adjacentMatrix [][]int) []Edge {
	adjacents := adjacentMatrix[currentNode-1]
	edges := []Edge{}
	for i, v := range adjacents {
		if v > 0 {
			edges = append(edges, Edge{start: currentNode, end: i + 1, weight: v})
		}
	}
	return edges
}

func nextInt(sc *bufio.Scanner) (int, error) {
	sc.Scan()
	return strconv.Atoi(sc.Text())
}

Python: # minimum_spanning_tree
def prim(adj_matrix, n):
    dist = [float("inf") for _ in range(n)]  # 訪問確定したノードからの最短距離
    visited = [False for _ in range(n)]  # ノードの訪問が確定したか
    parent = [-1 for _ in range(n)]  # start -> endを保存するためのstart？
    cost = 0
    inf = float("inf")

    start = 0
    visited[start] = True
    dist[start] = 0
    while True:
        # startから直接つながっている未訪問ノードをもとに、距離の最小値を更新する
        for end in range(n):
            if not visited[end] and adj_matrix[start][end] > -1 and adj_matrix[start][end] < dist[end]:
                dist[end] = adj_matrix[start][end]
                parent[end] = start

        min_cost = inf
        for nextnode in range(n):
            # dist_nodeの中から最もコストの短いノードを探す。そのノードは訪問確定。それを次回のstartとする。
            if not visited[nextnode] and dist[nextnode] < min_cost:
                min_cost = dist[nextnode]
                start = nextnode
        if min_cost == inf:
            break

        visited[start] = True
        cost += min_cost

    return parent, cost


def main():
    n = int(input())
    adj_matrix = []
    for _ in range(n):
        row = input().split()
        adj_matrix.append(list(map(int, row)))
    parent, cost = prim(adj_matrix, n)
    print(cost)


if __name__ == "__main__":
    main()

Python: n = int(input())
M = [[float("inf") for i in range(n)] for i in range(n)]
for i in range(n):
 L = list(map(int, input().split()))
 for j,v in enumerate(L):
  if v != -1:
   M[i][j] = v

def prim():
 color = [0] * n
 d = [float("inf")] * n
 p = [-1] * n
 d[0] = 0

 while True:
  minv = float("inf")
  u = -1
  for i in range(n):
   if minv > d[i] and color[i] != 2:
    u = i
    minv = d[i]
  if u == -1: break
  color[u] = 2
  for v in range(n):
   if color[v] != 2 and M[u][v] != float("inf"):
    if d[v] > M[u][v]:
     d[v] = M[u][v]
     p[v] = u
     color[v] = 1
 sum = 0
 for i in range(n):
  if p[i] != -1:
   sum += M[i][p[i]]
 return str(sum)

print(prim())

Python: def prim():
    global n
    global ct
    color=[0]*n
    d=[float('inf')]*n
    d[0]=0
    while True:
        mincost=float('inf')
        for i in xrange(n):
            if color[i]!=2 and d[i]<mincost:
                mincost=d[i]
                u=i
        if mincost==float('inf'):
            break
        ct+=d[u]
        color[u]=2
        for v in xrange(n):
            if color[v]!=2 and m[u][v]!=-1:
                if m[u][v]<d[v]:
                    d[v]=m[u][v]
                    color[v]=1
    
if __name__=='__main__':
    n=input()
    m=[map(int,raw_input().split()) for _ in xrange(n)]
    ct=0
    prim()
    print(ct)

Python: INFTY = 1 << 21
WHITE = 0
GRAY = 1
BLACK = 2
d = [INFTY for i in range(n)]
p = [-1 for i in range(n)]
color = [WHITE for i in range(n)]

n = int(input())
M = [list(map(int, input().split())) for i in range(n)]
for i in range(n):
    for j in range(n):
        if (M[i][j] == -1):
            M[i][j] = INFTY

def prim():
    global M, d, p, color
    d[0] = 0
    while(1):
        minv = INFTY
        u = -1
        for i in range(n):
            if (minv > d[i] and color[i] != BLACK):
                u = i
                minv = d[i]
        if (u == -1):
            break
        color[u] = BLACK
        for v in range(n):
            if (color[v] != BLACK and M[u][v] != INFTY):
                if (d[v] > M[u][v]):
                    d[v] = M[u][v]
                    p[v] = u
                    color[v] = GRAY
    
    return sum([M[i][p[i]] for i in range(n) if (p[i] != -1)])

print(prim())

Python: INF = 10000

def prim():
	color = ["WHITE" for _ in range(n)]
	d = [INF for _ in range(n)]
	pi = [None for _ in range(n)]
	d[0] = 0

	while True:
		mincost = INF
		for i in range(n):
			if color[i] != "BLACK" and d[i] < mincost:
				mincost = d[i]
				u = i
		if mincost == INF:
			break
		color[u] = "BLACK"
		for v in range(n):
			if color[v] != "BLACK" and M[u][v] != -1:
				if M[u][v] < d[v]:
					d[v] = M[u][v]
					pi[v] = u
					color[v] = "GRAY"
	sum = 0
	for u in range(n):
		if pi[u] != None:
			sum += M[u][pi[u]]
	return sum

if __name__ == "__main__":
	n = int(input())
	M = []
	for _ in range(n):
		M.append(list(map(int, input().split())))
	print(prim())

Python: # Graph II - Minimum Spanning Tree
def mst(length, adj_m):
    """ Prims's Algorithm """
    cost = 0
    X,Y = [0], [i+1 for i in range(length-1)]
    while len(Y):
        min_cost = 2001
        ey = -1
        for x in X:
            for y in Y:
                t = adj_m[x][y]
                if not t == -1:
                    if min_cost > t:
                        min_cost = t
                        ey = y
        X.append(ey)
        Y.remove(ey)
        cost += min_cost
    return cost

n = int(input())
adj_m = [[-1 for _ in range(n)] for _ in range(n)]
for j in range(n):
    for i,v in enumerate(input().split()):
        adj_m[j][i] = int(v)
min_weight = mst(n, adj_m)
print(min_weight)