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
	num := 0
	for len(edges) > 0 && num < n-1 {
		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
		num += 1
	}
	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<bits/stdc++.h>
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 unite(int a, int b) {
	P[root(a)] = root(b);
}

signed main() {
	int g;
	cin >> g;
	vector<pair<int, pair<int, int>>> a;
	int weight;

	init(g);

	for (int i = 0; i < g; ++i) {
		for (int j = 0; j < g; ++j) {
			cin >> weight;
			if (i > j and weight != -1) {
				a.push_back(make_pair(weight, make_pair(i, j)));
			}
		}
	}

	sort(a.begin(), a.end());

	int ans = 0;

	for (auto e : a) {
		//cout << e.first << ' ' << e.second.first << ' ' << e.second.second << endl;
		if (!is_same_set(e.second.first, e.second.second)) {
			unite(e.second.first, e.second.second);
			ans += e.first;
		}
		else {
			continue;
		}
	}
	cout << ans << endl;
}

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

using namespace std;

struct edge{
	int u;	//?????????
	int v;	//??°??????
	int cost;	//??????
	//???????????????????????§?????§?°???¢????????????
	bool operator<(const edge& another) const {
			return cost < another.cost;
	}
};

const int MAX_N = 100;
const int MAX_M = 10000;

edge es[MAX_M];
int par[MAX_N];
int rank[MAX_N];

void initial(int n){		//?????????
	for(int i=0;i<n;i++){
		par[i] = i;
		rank[i] = 0;
	}
}

int find(int x){		//??¨??????????±???????
	if(par[x] == x){
		return x;
	}else{
		return par[x] = find(par[x],par);
	}
}

void unite(int x,int y){	//x???????????¨??¨y???????????¨?????????
	x = find(x,par);	//x???????????¨??????????????????x??¨??????
	y = find(y,par);	//y???????????¨??????????????????y??¨??????
	if(x == y) return;
	if(rank[x] < rank[y]){
		par[x] = y;
	}else{
		par[y] = x;
		if(rank[x] == rank[y])
			rank[x]++;
	}
}

bool same(int x,int y){	//x,y???????????¨???????????§?????????
	return find(x,par) == find(y,par);
}

bool comp(const edge& e1,const edge& e2){
	return e1.cost < e2.cost;
}

int kruskal(int n,int m){
	initial(n,par,rank);
	int res = 0;
	for(int i=0;i<m;i++){
		edge e = es[i];
		if(!same(e.u,e.v,par)){
			unite(e.u,e.v,par,rank);
			res += e.cost;
		}
	}
	return res;
}

int main(){
	int n,m=0,k,l,x;
	cin >> n;	//????????°?????\???
	int cnt=0;
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			cin >> x;
			if(x != -1){
				edge e;
				e.from = i; e.to = j; e.cost = x;
				es[m] = e;
				m++;
			}
		}
	}
	sort(es,es+m);
	cout << kruskal(n,m) << "\n";
}

C++: #include <bits/stdc++.h>
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 (color[i] != BLACK && d[i] < minv) {
        minv = d[i];
        u = 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++) {
    sum += d[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;
};

C++: #include<stdio.h>
#include<algorithm>
using namespace std;
int maxn=1<<21;
int n;
int a[105][105];
int color[105];
int d[105];
int p[105];
int f()
{
    int u;int minx;
    for(int i=0;i<105;i++)
    {
        d[i]=maxn;
        p[i]=-1;
    }
    d[0]=0;
    while(1)
    {
        minx=maxn;
        u=-1;
        for(int i=0;i<n;i++)
        {
            if(minx>d[i]&&color[i]==0)
            {
                u=i;
                minx=d[i];
            }
        }
        if(u==-1) break;
        color[u]=1;
        for(int i=0;i<n;i++)
        {
            if(color[i]==0&&a[u][i]!=maxn)
            {
                if(d[i]>a[u][i])
                {
                    d[i]=a[u][i];
                    p[i]=u;
                    color[i]=0;
                }
            }
        }
    }
    int sum=0;
    for(int i=0;i<n;i++)
    {
        if(p[i]!=-1) sum+=a[i][p[i]];
    }
    return sum;
}
int main()
{
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            int x;
            scanf("%d",&x);
            a[i][j]=(x==-1)?maxn:x;
        }
    }
    printf("%d\n",f());
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
static const int INFTY = 2000000000;
static const int NIL = -1;
static const int MAX = 100;
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;   //d[i]??????T??????????????§?????????V????±?????????????i?????????????????????
        p[i] = -1;
        color[i] = WHITE;
    }

    d[0] = 0;

    while(1){
        minv = INFTY;
        u = -1;
        for(int i=0;i<n;i++){
            if(d[i] < minv && color[i] != BLACK){    //?¢????????????§????????????????????????INFTY??§?????????????????¢???
                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){  //2??????????????¶???????????????????
                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(){
    scanf("%d",&n);

    for(int i=0;i<n;i++){
        for(int k=0;k<n;k++){
            int e; scanf("%d",&e);
            M[i][k] = (e==-1) ? INFTY : e;
        }
    }

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

    return 0;
}