Go: package main

import (
	"fmt"
	"sort"
)

type Edge struct {
	I int
	J int
	A int
}

func main() {
	var n int
	fmt.Scan(&n)

	g := make([][]int, n)
	edges := make([]Edge, 0, n*(n-1)/2)
	parents := make([]int, n)
	for i := 0; i < n; i++ {
		g[i] = make([]int, n)
		for j := 0; j < n; j++ {
			fmt.Scan(&g[i][j])
			if g[i][j] > 0 {
				edges = append(edges, Edge{i, j, g[i][j]})
			}
		}
		parents[i] = i
	}
	sort.SliceStable(edges, func(i, j int) bool { return edges[i].A > edges[j].A })

	sum := 0
	for len(edges) > 0 {
		edge := edges[len(edges)-1]
		edges = edges[:len(edges)-1]

		if same(edge.I, edge.J, &parents) {
			continue
		}
		union(edge.I, edge.J, &parents)
		sum += edge.A
	}
	fmt.Println(sum)
}



func root(id int, parents *[]int) int {
	if id == (*parents)[id] {
		return id
	}
	return root((*parents)[id], parents)
}

func union(i, j int, parents *[]int) {
	(*parents)[root(j, parents)] = (*parents)[root(i, parents)]
}

func same(i, j int, parents *[]int) bool {
	return root(i, parents) == root(j, parents)
}

C++: #include <iostream>
#include <cstdio>
#include <vector>
#include <list>

#define INFTY 1000000001

class Graph{
public:
	std::vector< std::vector<int> > A;
	int n;

	Graph(int n);

	void SetEdge(int a, int b, int weight);
	void SortEdge();
	void SortEdge_Rec1(int left, int right);
	void SortEdge_Rec2(int left, int mid, int right);

	int Kruskal();

};

Graph::Graph(int n){;
	this->n = n;
}

void Graph::SetEdge(int a, int b, int weight){
	std::vector<int> x;
	x.push_back(a); x.push_back(b); x.push_back(weight);
	A.push_back(x);
	return;
}

void Graph::SortEdge(){
	SortEdge_Rec1(0, A.size());
}

void Graph::SortEdge_Rec1(int left, int right){
	if (left + 1 < right){
		int mid = (left + right) / 2;
		SortEdge_Rec1(left, mid);
		SortEdge_Rec1(mid, right);
		SortEdge_Rec2(left, mid, right);
	}
}

void Graph::SortEdge_Rec2(int left, int mid, int right){
	int n1 = mid - left;
	int n2 = right - mid;
	std::vector< std::vector<int> > L(n1 + 1);
	for (int i = 0; i < n1; i++) L[i] = A[left + i];
	std::vector< std::vector<int> > R(n2 + 1);
	for (int i = 0; i < n2; i++) R[i] = A[mid + i];
	std::vector<int> x;
	x.push_back(0); x.push_back(0); x.push_back(INFTY);
	L[n1] = x;
	R[n2] = x;
	int i = 0;
	int j = 0;
	for (int k = left; k < right; k++){
		if (L[i][2] <= R[j][2]){
			A[k] = L[i];
			i = i + 1;
		}
		else{
			A[k] = R[j];
			j = j + 1;
		}
	}
}

int Graph::Kruskal(){
	std::vector<int> f(n);
	for (int i = 0; i < n; i++) f[i] = i;
	int branch = 0;

	while (!A.empty()){
		std::vector<int> e = A[0];
		A.erase(A.begin());
		if (f[e[0]] != f[e[1]]){
			int tmp = f[e[1]];
			for (int i = 0; i < n; i++) if (f[i] == tmp) f[i] = f[e[0]];
			branch += e[2];
		}
	}
	return branch;
}

int main(){
	int n, e;
	scanf("%d", &n);
	Graph G(n);
	for (int i = 0; i < n; i++){
		for (int j = 0; j < n; j++){
			scanf("%d", &e);
			if (j >= i && e != -1) G.SetEdge(i, j, e);
		}
	}

	G.SortEdge();

	printf("%d\n", G.Kruskal());

	return 0;
}

C++: #include <iostream>
using namespace std;
int n,m[100][100];
void mins(){
    int u,min;
    int d[100],p[100],color[100];
    for(int i=0;i<n;i++){
        d[i]=2001;
        p[i]=-1;
        color[i]=1;
    }
    d[0]=0;
    while(1){
        min=2001;
        u=-1;
        for(int i=0;i<n;i++){
            if(color[i]!=3&&d[i]<min){
                u=i;
                min=d[i];
            }
        }
        if(u==-1){
            break;
        }
        color[u]=3;
        for(int v=0;v<n;v++){
            if(color[v]!=3&&m[u][v]!=-1){
                if(d[v]>m[u][v]){
                    p[v]=u;
                    d[v]=m[u][v];
                    color[v]=2;
                }
            }
        }
    }
    int ans=0;
    for(int i=0;i<n;i++){
        if(p[i]!=-1){
            ans+=d[i];
        }
    }
    cout<<ans<<endl;
}
int main(){
    cin>>n;
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            cin>>m[i][j];
        }
    }
    mins();
    return 0;
}

C++: #include<stdio.h>
#define INFTY (1<<21)
int main(){
	int n,c[100]={},m[100][100],d[100],p[100],i,j,u,a,sum=0;
	scanf("%d",&n);
	for(i=0;i<n;i++){
		for(j=0;j<n;j++){
			scanf("%d",&a);
			m[i][j]=a==-1?INFTY:a;
		}
	}
	for(i=0;i<n;i++){
		d[i]=INFTY;
		p[i]=0;
	}
	p[0]=-1;
	d[0]=0;
	c[0]=1;
	while(1){
		int minv=INFTY;
		u=-1;
		for(i=0;i<n;i++){
			if(minv>d[i]&&c[i]!=2){
				minv=d[i];
				u=i;
			}
		}
		if(u==-1)break;
		c[u]=2;
		for(i=0;i<n;i++){
			if(c[i]!=2&&m[u][i]!=INFTY){
				if(d[i]>m[u][i]){
					d[i]=m[u][i];
					p[i]=u;
					c[i]=1;
				}
			}
		}
	}
	for(i=0;i<n;i++){
		if(p[i]!=-1){
			sum+=m[i][p[i]];
		}
	}
	printf("%d\n",sum);
	return 0;
}

C++: #include<iostream>
#include<stdlib.h>
using namespace std;

static const int MAX = 100;
static const int INF = (1<<21);
int color[MAX] = {0};
int M[MAX][MAX];
int d[MAX];

void prim(int n) {
    d[0] = 0;
    int minconst = INF, u, i;
    for ( i = 1; i < n; i++ ) d[i] = INF;

    while(1) {
        minconst = INF;
        for ( i = 0; i < n; i++ )
        {
            if ( color[i] != 1 && minconst > d[i] ) {
                minconst = d[i];
                u = i;
            }
        }

        if ( minconst == INF )
            break;

        color[u] = 1;

        for ( i = 0; i < n; i++ ) {
            if ( color[i] != 1 && M[u][i] != -1 ) {
                if ( M[u][i] < d[i] ) d[i] = M[u][i];
            }
        }
    }
}

int main() {
    int n, i, j;

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

    prim(n);

    int sum = 0;
    for ( i = 0; i < n; i++ ) {
        sum += d[i];
    }
    cout << sum << endl;
}

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