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: def prim():
    INFTY = float('inf')
    color = ['WHITE' for _ in range(n)]
    d = [INFTY for _ in range(n)]
    p = [None for _ in range(n)]
    d[0] = 0
    p[0] = -1

    while True:
        mincost = INFTY
        for i in range(n):
            if color[i] != 'BLACK' and d[i] < mincost:
                mincost = d[i]
                u = i
        
        if mincost == INFTY:
            break

        color[u] = 'BLACK'

        for v in range(n):
            if color[v] != 'BLACK' and A[u][v] != -1:
                if A[u][v] < d[v]:
                    d[v] = A[u][v]
                    p[v] = u
                    color[v] = 'GRAY'
    return sum(d)

n = int(input())
A = []
for _ in range(n):
    l = list(map(int, input().split()))
    A.append(l)

print(prim())

Python: N = int(input())
M = [list(map(int, input().split())) for i in range(N)]
INF = 2001
MAX = 100
d = [INF for i in range(N)]
d[0] = 0
p = [MAX for i in range(N)]
p[0] = -1
mst = [False for i in range(N)]

while True:
  min_w = INF
  for i in range(N):
    if not mst[i] and d[i] < min_w:
      min_w = d[i]
      u = i
  if min_w == INF:
    break
  mst[u] = True
  for v in range(N):
    if not mst[v] and M[u][v] != -1:
      if M[u][v] < d[v]:
        d[v] = M[u][v]
        p[v] = u

print(sum(d))

Python: n = int(input())
adj = [[0 for i in range(n)] for j in range(n)]

for i in range(n):
    tmp = list(map(int, input().split()))
    u = tmp[0]
    u = u -1
    k = tmp[1]
    v = tmp[2:] -1
    for j in range(k):
        adj[u][v[j]] = 1
        
for i in range(n):
        print(" ".join(list(map(str, adj[i]))))

Python: def main():

    N = 100

    n = int(input())
    adj = [[0 for i in range(n)] for j in range(n)]

    for i in range(n):
        tmp = list(map(int, input().split()))
        u = tmp[0]
        u = u -1
        k = tmp[1]
        v = tmp[2:]
        for j in range(k):
            adj[u][v[j]-1] = 1

    for i in range(n):
        for j in range(n):
            print(adj[i][j], end=" ")
        print()
    return 0

if __name__ == "__main__":
    return main()

C++: #include<iostream>
using namespace std;
static const int MAX = 100;
static const int INFTY = (1 << 21);
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;

int n, M[MAX][MAX];

int prim() {
	int u, minv;
	int d[MAX], p[MAX], color[MAX];
	
	for(int i = 0; i < n; i++) {
		d[i] = INFTY;
		p[i] = -1;
		color[i] = WHITE;
	}
	
	d[0] = 0;
	color[0] = GRAY;
	
	while( 1 ) {
		minv = INFTY;
		u = -1;
		for(int i = 0; i < n; i++) {
			if(minv > d[i] && color[i] != BLACK) {
				minv = d[i];
				u = i;
			}
		}
		if(u == -1) break;
		color[u] = BLACK;
		for(int v = 0; v < n; v++) {
			if(color[v] != BLACK && M[u][v] != INFTY) {
				if(d[v] > M[u][v]) {
					d[v] = M[u][v];
					p[v] = u;
				}
			}
		} 
		
	}
	
	int sum = 0;
	for(int i = 0; i < n; i++) {
		if(p[i] != -1) sum += M[i][p[i]];
	}
	
	return sum;
}

int main() {
	int e; 
	cin >> n;
	for(int i = 0; i < n; i++) {
		for(int j = 0; j < n; j++) {
		    cin >> e;
		    M[i][j] = (e == -1) ? INFTY : e;
		}
	}
	
	cout << prim() << endl;
	return 0; 
}

C++: #include <cstdio>
static const int INFTY = (1 << 21);
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;

int n, **M;

int prim() {
    int u, minv;
    int d[n], p[n], color[n];

    for (int i = 0; i < n; i++) {
        d[i] = INFTY;
        p[i] = -1;
        color[i] = WHITE;
    }

    d[0] = 0;

    while (1) {
        minv = INFTY;
        u = -1;
        for (int i = 0; i < n; i++) {
            if (d[i] < minv && color[i] != BLACK) {
                u = i;
                minv = d[i];
            }
        }
        if (u == -1) {
            break;
        }
        color[u] = BLACK;
        for (int v = 0; v < n; v++) {
            if (color[v] != BLACK && M[u][v] != INFTY) {
                if (d[v] > M[u][v]) {
                    d[v] = M[u][v];
                    p[v] = u;
                    color[v] = GRAY;
                    // printf("d[%d] = %d (parent: %d)\n", v, d[v], u);
                }
            }
        }
    }

    int sum = 0;
    for (int i = 0; i < n; i++) {
        if (p[i] != -1) {
            sum += M[i][p[i]];
        }
    }

    return sum;
}

int main() {
    scanf("%d", &n);

    M = new int*[n];
    for (int i = 0; i < n; i++) {
        M[i] = new int[n];
        for (int j = 0; j < n; j++) {
            int a;
            scanf("%d", &a);
            M[i][j] = (a == -1) ? INFTY : a;
        }
    }

    printf("%d\n", prim());

    return 0;
}

C++: #include<iostream>
#include<algorithm>
using namespace std;

#define INF 2000

int n;
int cost[110][110], mincost[110];
bool used[110];

int prim(){
  
  for(int i = 0;i < n;i++){
    mincost[i] = INF;
    used[i] = false;
  }
  
  mincost[0] = 0;
  int ans = 0;
  
  while(true){
    
    int v = -1;
    
    for(int u = 0;u < n;u++){
      if(!used[u] && (v == -1 || mincost[u] < mincost[v])) v = u;
    }
    
    if(v == -1) break;
    used[v] = true;
    ans += mincost[v];
    
    for(int u = 0;u < n;u++){
      mincost[u] = min(mincost[u], cost[v][u]);
    }
  }
  
  return ans;
}

int main(){
  
  cin >> n;
  for(int i = 0;i < n;i++){
    for(int j = 0;j < n;j++){
      cin >> cost[i][j];
      if(cost[i][j] == -1) cost[i][j] = INF;
    }
  }
  
  cout << prim() << endl;
  
  return 0;
}