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)
}

C++: #include <cstdio>
#pragma warning(disable:4996)
#define MAX 101
#define INF 100000
#define FLAG 1
#define DEFLAG 0

int main() {
	int i, j, n, min, a[MAX][MAX], k, x[MAX], minNode, sum = 0, g[MAX];

	scanf("%d", &n);
	for (i = 0; i<n; i++) {
		for (j = 0; j<n; j++) {
			scanf("%d", &a[i][j]);
		}
	}
	k = 1;
	x[0] = 0;
	g[0] = FLAG;
	for (i = 1; i<n; i++) {
		g[i] = DEFLAG;
	}
	while (1) {
		min = INF;
		for (i = 0; i<k; i++) {
			for (j = 0; j<n; j++) {
				if (min > a[x[i]][j] && a[x[i]][j] != -1 && g[j] != FLAG) {
					min = a[x[i]][j];
					minNode = j;
				}
			}
		}
		if (k != n) {
			x[k] = minNode;
			g[minNode] = FLAG;
			k++;
			sum += min;
		}
		else { break; }

	}
	printf("%d\n", sum);
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

#define rep(i, n) for (int i=0; i<(n); ++i)
using ll = long long;
using P = pair<int, int>;

const int INF = 1 << 21;
const int MAX = 103;
const int WHITE = 0;
const int GRAY = 1;
const int BLACK = 2;
int n, G[MAX][MAX];

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

  fill((int*)d, (int*)(d+MAX), INF);
  memset(p, -1, sizeof(p));
  memset(color, WHITE, sizeof(color));

  d[0] = 0;
  while (true) {
    minv = INF;
    u = -1;
    rep(i,n) {
      if (minv > d[i] && color[i] != BLACK) {
	u = i;
	minv = d[i];
      }
    }
    if (u == -1) break;
    color[u] = BLACK;
    rep(v,n) {
      if (color[v] != BLACK && G[u][v] != INF) {
	if (d[v] > G[u][v]) {
	  d[v] = G[u][v];
	  p[v] = u;
	  color[v] = GRAY;
	}
      }
    }
  }
  
  int sum = 0;
  rep(i,n) {
    if (p[i] != -1) sum += G[i][p[i]];
  }
  return sum;
}

int main() {
  cin >> n;
  rep(i,n) rep(j,n) {
    int e;
    cin >> e;
    G[i][j] = (e == -1) ? INF : e;
  }

  cout << prim() << endl;
  return 0;
}

C++: #include <cstdio>
#define INF 1<<30

int n, M[100][100];

int prim() {
    int e, minv, minx, s;
    int p[n], d[n], v[n];

    for (e = 0; e < n; e++) {
        p[e] = -1;
        d[e] = INF;
        v[e] = 0;
    }

    d[0] = 0;

    while (1) {
        minv = INF;
        minx = -1;
        for (e = 0; e < n; e++) {
            if (minv > d[e] && v[e] != 1) {
                minx = e;
                minv = d[e];
            }
        }

        if (minx == -1) break;
        v[minx] = 1;
        
        for (e = 0; e < n; e++) {
            if (v[e] != 1 && M[minx][e] != INF) {
                if (d[e] > M[minx][e]) {
                    d[e] = M[minx][e];
                    p[e] = minx;
                }
            }
        }
    }

    s = 0;
    for (e = 0; e < n; e++) {
        if (p[e] != -1) s += M[e][p[e]];
    }

    return s;
}

int main(void) {
    int i, j, v;
    scanf("%d", &n);

    for (i = 0; i < n; i++) {
        for (j = 0; j < n; j++) {
            scanf("%d", &v);
            M[i][j] = (v == -1) ? INF : v;
        }
    }

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

    return 0;
}