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<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;
    while(1){
        minv=INFTY;
        u=-1;
        for(int i=0;i<n;i++){
            if(minv>d[i]&&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;
                }
            }
        }
    }
    int sum=0;
    for(int i=0;i<n;i++){
        if(p[i]!=-1) sum+=M[i][p[i]];
    }
    return sum;
}

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

C++: #include <bits/stdc++.h>

#define rep(i, a, b) for (int i = a; i < (int)(b); ++i)
#define rrep(i, a, b) for (int i = b - 1; i >= (int)(a); --i)

using namespace std;

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    int n;
    cin >> n;

    vvi g(n, vi(n, INT_MAX));

    rep(i, 0, n) {
        rep(j, 0, n) {
            cin >> g[i][j];
            if (g[i][j] == -1) { g[i][j] = INT_MAX; }
        }
    }

    vector<bool> seen(n, false);
    vi d(n, INT_MAX);
    vi p(n, -1);

    d[0] = 0;

    while (true) {
        // Mimimum cost to connect T and V-T.
        int minv = INT_MAX;
        // A vertex in V-T, which has minimum cost.
        int u = -1;

        rep(i, 0, n) {
            if (!seen[i] && minv > d[i]) {
                u = i;
                minv = d[i];
            }
        }
        if (u == -1) { break; }

        // Append u into T.
        seen[u] = true;

        // Calculate minimum distance between u and v in V-T.
        rep(v, 0, n) {
            if (!seen[v] && g[u][v] != INT_MAX) {
                if (d[v] > g[u][v]) {
                    d[v] = g[u][v];
                    p[v] = u;
                }
            }
        }
    }

    int sum = 0;
    rep(i, 0, n) {
        if (p[i] != -1) { sum += g[i][p[i]]; }
    }
    cout << sum << endl;

    return 0;
}

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

int main() {
    int i, j, n, res = 0;
    cin >> n;
    int a[n][n];

    for (i=0; i<n; i++) {
        for (j=0; j<n; j++) {
            cin >> a[i][j];
        }
    }

    vector<int> v;
    set<int> cons;
    v.push_back(0);
    while (v.size() != n) {
        int nearest_node = 9999;
        int nearest_distance = 9999;
        for (i=0; i<v.size(); i++) {
            for (j=0; j<n; j++) {
                if ((a[i][j] != -1) && (a[i][j] < nearest_distance) && (cons.count(j) == 0)) {
                    nearest_node = j;
                    nearest_distance = a[i][j];
                }
            }
        }
        res += nearest_distance;
        v.push_back(nearest_node);
        cons.insert(nearest_node);
    }
    cout << res << endl;
    return 0;
}

C++: #define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <iostream>
using namespace std;
const int num = 100;
int v[num][num], n, c[num], p[num], d[num];;
inline int mini(int k) {
	for (int i = 0; i < n; i++) {
		d[i] = 99999999;
	}

	d[k] = 0;
	p[k] = -1;
	while (1) {
		int minv = 99999999, u = -1;
		for (int i = 0; i < n; i++) {
			if (d[i] < minv && c[i] == 0) {
				minv = d[i];
				u = i;
			}
		}
		if (u == -1) break;
		for (int i = 0; i < n; i++) {
			if (v[u][i] != -1 && c[i] == 0) {
				if (d[i] > v[u][i]) {
					d[i] = v[u][i];
					p[i] = u;
				}
			}
		}
		c[u] = 1;
	}
	int sum = 0;
	for (int i = 0; i < n; i++) {
		if(p[i] != -1) sum += v[i][p[i]];
	}
	return sum;
}
int main() {
	cin >> n;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			cin >> v[i][j];
		}
	}
	for (int i = 0; i < n; i++) {
		c[i] = 0;
	}
	cout << mini(0) << endl;
	for (int i = 0; i < n; i++) {
		if (p[i] != -1)cout << p[i] << "  " << v[i][p[i]] << endl;
	}
	return 0;
}