Go: package main

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

// Define disjoint-set forests

type vertexNo int

type vertex struct {
	number vertexNo
	rank int
	parent *vertex
}

func makeSet(number vertexNo) vertex {
	return vertex {
		number: number,
		rank: 0,
		parent: nil,
	}
}

func union(x, y *vertex) *vertex {
	return link(findSet(x), findSet(y))
}

func link(x, y *vertex) *vertex {
	if x.rank > y.rank {
		y.parent = x
		return x
	} else {
		x.parent = y
		if x.rank == y.rank {
			y.rank += 1
		}
		return y
	}
}

func findSet(v *vertex) *vertex {
	if v.parent == nil {
		return v
	}
	v.parent = findSet(v.parent)
	return v.parent
}

// Function to calculate minimum spanning tree

type matrix [][]int

type edge struct {
	one vertexNo
	two vertexNo
	weight int
}

const(
	MAX_VERTICES = 100
)

func newEdgesFromMatrix(matrix matrix) []edge {
	edges := make([]edge, 0, MAX_VERTICES * MAX_VERTICES)

	for i := 0; i < len(matrix); i++ {
		row := matrix[i]
		for j := i; j < len(row); j++ {
			if row[j] >= 0 {
				edge := edge{one: vertexNo(i+1), two: vertexNo(j+1), weight: row[j]}
				edges = append(edges, edge)
			}
		}
	}

	return edges
}

func solve(matrix matrix) int {
	nVertices := len(matrix)

	// initialize vertices
	vertices := make([]vertex, nVertices)
	for i := 0; i < nVertices; i++ {
		vertices[i] = makeSet(vertexNo(i+1))
	}

	// prepare edges sorted in the ascending order of weight
	edges := newEdgesFromMatrix(matrix)
	sort.Slice(edges, func(i, j int) bool {
		return edges[i].weight < edges[j].weight
	})

	unionCount := 0
	weightSum := 0
	for unionCount < nVertices-1 {
		for i := 0; i < len(edges); i++ {
			edge := edges[i]
			v1 := &vertices[edge.one-1]
			v2 := &vertices[edge.two-1]
			rep1 := findSet(v1)
			rep2 := findSet(v2)
			if rep1.number == rep2.number {
				continue
			}
			union(rep1, rep2)
			edges = append(edges[0:i], edges[i+1:]...)
			weightSum += edge.weight
			unionCount++
			break
		}
	}

	return weightSum
}

// main

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

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

	matrix := parseMatrix(n, sc)

	edges := newEdgesFromMatrix(matrix)
	sort.Slice(edges, func(i, j int) bool {
		return edges[i].weight < edges[j].weight
	})

	fmt.Println(solve(matrix))
}

func parseMatrix(n int, sc *bufio.Scanner) [][]int {
	matrix := make([][]int, n)
	for i := 0; i < n; i++ {
		matrix[i] = make([]int, n)
		for j := 0; j < n; j++ {
			x, err := nextInt(sc)
			if err != nil {
				panic(err)
			}
			matrix[i][j] = x
		}
	}
	return matrix
}

func nextText(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

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

C++: #include<iostream>
#include<unordered_set>
#define N 105
#define UNDEFINED -1
static const int INF = (1<<21);

int n,a;
int d[N], p[N];
int G[N][N];

int prim() {
    int u, min;
    std::unordered_set<int> hasVisited;
    for(int i = 0; i < N; i++) {
        d[i] = INF;
        p[i] = UNDEFINED;
    }
    d[0] = 0;
    while(true) {
        min = INF;
        u = UNDEFINED;
        for(int i = 0; i < n; i++) {
            if(d[i] < min && hasVisited.find(i) == hasVisited.end()) {
                u = i;
                min = d[i];
            }
        }
        if (u == UNDEFINED) break;
        hasVisited.insert(u);
        for(int i = 0; i < n; i++) {
            if(hasVisited.find(i) == hasVisited.end() && G[u][i] != UNDEFINED) {
                if(d[i] > G[u][i]) {
                    d[i] = G[u][i];
                    p[i] = u;
                }
            }
        }
    }
    int sum = 0;
    for(int i = 0; i < n; i++) {
        if(p[i] != UNDEFINED) {
            sum += G[i][p[i]];
        }
    }
    return sum;
}

int main() {
    std::cin >> n;
    for(int i = 0; i < n; i++) {
        for(int j = 0; j < n; j++) {

            std::cin >> a;
            G[i][j] = a;
        }
    }
    std::cout << prim() << std::endl;
    return 0;
}

C++: #include <iostream>
#include <stack>
#define N 100
#define WHITE 0
#define GREY 1
#define BLACK 2
#define INFTY 100000
using namespace std;

int color[N];//color用来记录访问状态
int M[N][N];//M[N][N]是图的邻接矩阵表示法
int d[N],p[N];
//d[N]用来记录连接T内顶点与V-T内顶点的边中，权值最小的边的权值
//p[n]用来记录MST中顶点v的父节点
void init_color_d()
{
    for(int i = 0; i < N; i++)
    {
        color[i] = WHITE;
        d[i] = INFTY;
    }
}
int prim(int n)
{
    init_color_d();
    d[0] = 0;//起始点为0结点，距离设置为0
    p[0] = -1;
    int u;
    while(true)//访问结点为u
    {
        int mincost = INFTY;
        for(int i = 0; i < n; i++)
        {
            if(color[i]!=BLACK&&d[i]<mincost)//求最小值
            {
                mincost = d[i];
                u = i;
            }
        }
        if(mincost==INFTY)break;//这时候表示没有取到最小值，即最小生成树已经完成
        color[u] = BLACK;//因为u是最小值，那么就先标志为访问完
        for(int v = 0; v < n; v++)
        {
            if(color[v]!=BLACK&&M[u][v]!=-1)//如果v没有被选入到MST集合里面，并且此边存在
            {
                if(M[u][v]<d[v])
                {
                    d[v] = M[u][v];//d[v]记录最小值
                    p[v] = u;//记录父节点
                    color[v] = GREY;//设置为已经尝试过此结点
                }
            }
        }
    }
    int sum = 0;
    for(int i = 0;i < n;i++){
        if(p[i]!=-1)sum+=M[i][p[i]];
    }
    return sum;
}

int main()
{
    int n;
    cin >> n;
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j <n; j++)
        {
            cin >> M[i][j];
        }
    }
    cout<<prim(n)<<endl;

    return 0;
}

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

vector<int> P;

int root(int v){ //根を返す
    if(P[v] == v) return v;
    else return root(P[v]);
}

bool is_same_set(int a, int b){ // 同じならtrue
    return root(a) == root(b);
}

void unite(int a, int b){ // aの根をbの根にくっつける
    P[root(a)] = root(b);
}

int main(){
    int n; cin >> n;
    P.resize(n); //　Pは作りかけの森を想定する
    for(int i = 0; i < n; i++){
        P[i] = i;
    }
    vector<tuple<int, int, int>> vec; //<コスト,始点, 終点>を格納する配列
    for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++){
            int a; cin >> a;
            if(a >= 0 && i < j){
                vec.push_back(make_tuple(a, i, j));
            }
        }
    }
    sort(vec.begin(), vec.end()); //コスト順にソート
    int ans = 0;
    for(auto v: vec){
        int cost = get<0>(v), str = get<1>(v), end = get<2>(v);
        if(is_same_set(str, end)) continue; //繋がっている
        //もし繋がっていなければ
        ans += cost; // コストを足す
        unite(str, end); //つなげてあげる
    }
    cout << ans << endl;
}

C++: #include <iostream>
#include <utility>
#include <vector>
#include <algorithm>

using namespace std;

int P[10010];
void init(int N){
    for(int i=0;i<=N;i++)
        P[i] = i;
}
int root(int a){
    if(P[a]==a)
        return a;
    return(P[a] = root(P[a]));
}
bool is_same_set(int a, int b){
    return root(a) == root(b);
}
void unit(int a, int b){
    P[root(a)] = root(b);
}

int main(){
    int n; cin >> n;
    pair<int, pair<int,int> > a;
    vector< pair<int, pair<int,int> > > e;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            int x; cin >> x;
            if(x!=-1){
                e.push_back(make_pair(x, make_pair(i,j)));
            }
        }
    }
    sort(e.begin(),e.end());
    int ans = 0;
    init(n);
    for(vector< pair<int, pair<int,int> > >::iterator it = e.begin();it != e.end();it++){
        int s = it->second.first;
        int g = it->second.second;
        if(!is_same_set(s,g)){
            unit(s,g);
            ans += it->first;
        }
    }
    cout << ans << endl;
}