Go: package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 500001), 500000)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func main() {
	fp := os.Stdin
	if len(os.Args) > 1 {
		fp, _ = os.Open(os.Args[1])
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(os.Stdout)

	n := getNextInt(scanner)

	g := make([][]int, n)
	ans := make([][]int, n)

	inf := 1 << 30
	for i := 0; i < n; i++ {
		g[i] = make([]int, n)
		ans[i] = make([]int, n)
		for ii := 0; ii < n; ii++ {
			g[i][ii] = getNextInt(scanner)
			if g[i][ii] == -1 {
				g[i][ii] = inf
			}
			ans[i][ii] = -1
		}
	}

	im := make(IntMap, 1)

	im.increment(1)

	for len(im) < n {
		min := inf
		u := -1
		v := -1
		for i := range im {
			for ii := 0; ii < n; ii++ {
				if im.get(ii) != 0 {
					continue
				}
				if min > g[i][ii] {
					min = g[i][ii]
					u = i
					v = ii
				}
			}
		}
		im.increment(v)
		ans[u][v] = min
		ans[v][u] = min
	}

	sum := 0
	for i := 0; i < n-1; i++ {
		for ii := i + 1; ii < n; ii++ {
			if ans[i][ii] != -1 {
				sum += ans[i][ii]
			}
		}
	}

	fmt.Fprintln(writer, sum)
	writer.Flush()
}

// IntMap ...
type IntMap map[int]int

func (m IntMap) get(key int) int {
	if _, ok := m[key]; ok {
		return m[key]
	}
	return 0
}
func (m IntMap) increment(key int) {
	if _, ok := m[key]; ok == false {
		m[key] = 0
	}
	m[key]++
}

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