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 <bits/stdc++.h>

using namespace std;

int n;
const int N = 110;
int A[N][N];
typedef pair<int, int> P;
vector<P> G[N];

int min_cost = 0;

void prim() {
    priority_queue<P, vector<P>, greater<P> > heap;
    heap.push({0, 1});
    vector<bool> vis(n + 1, false);
    vector<int> dis(n + 1, INT_MAX);

    while (!heap.empty()) {
        int u = heap.top().second, cost = heap.top().first;
        heap.pop();
        if (vis[u]) continue;
        vis[u] = true;
        min_cost += cost;
        for (int i = 0; i < G[u].size(); i++) {
            int v = G[u][i].second, len = G[u][i].first;
            if (!vis[v] && len < dis[v]) {
                dis[v] = len;
                heap.push({len, v});
            }
        }
    }    
}

void kruskal() {

}

int main() {
    cin >> n;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            cin >> A[i][j];
            if (A[i][j] != -1) {
                G[i].push_back({A[i][j], j});
            }
        }
    }
    
    prim();
    cout << min_cost << endl;

    return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <utility>
using namespace std;
int P[10010];

void init(int N)
{
    for(int i = 1; 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);
}

int main()
{
	int n = 0;
	cin >> n; 
	int cost[5000];
	int src[5000];
	int dst[5000];
	int A[100 + 10][100 + 10];
	int M = 0;
	pair<int,int> p;
	vector<pair<int,int>> edges;

	for(int r = 0; r < n; ++r)
	{
		for(int c = 0; c < n; ++c)
		{
			cin >> A[r][c];
		}
	}
	
	for (int r = 0; r < n; ++r)
	{
		for(int c = r + 1; c < n; ++c)
		{
			if(A[r][c] > -1)
			{
				cost[M] = A[r][c];
				src[M] = r;
				dst[M] = c;
				edges.push_back(make_pair(cost[M],M));
				M += 1;
			}
		}
	}
	sort(edges.begin(),edges.end());
	int sum = 0;
	init(n);
	for(int i = 0; i < M; ++i)
	{
		if(is_same_set(src[i], dst[i]) == 1) continue;
		else
		{
			unite(src[i], dst[i]);
			sum += edges[i].first;
		}
	}
	cout << sum << endl;
}

C++: #include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
const int MAXN = (int)1e2 + 10;

int m,n;
int p[MAXN];
int a[MAXN][MAXN];
int vis[MAXN];
int d[MAXN];
const int INF = (int) 0x3f3f3f3f;

void work(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++) p[i]=i;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            scanf("%d",&a[i][j]);
        }
    }
    int ans=0;
    int cur=1,cnt=1;
    vis[1]=1;
    for(int i=2;i<=n;i++) d[i]=INF;
    for(int i=1;i<=n;i++)if(a[1][i]!=-1) d[i]=a[1][i];
    while(cnt<n){
        int id=0;
        for(int i=1;i<=n;i++)if(!vis[i]){
            if(id==0||d[id]>d[i]){
                id=i;
            }
        }
        vis[id]=1;
        cnt++;
        ans+=d[id];
        for(int i=1;i<=n;i++)if(!vis[i]&&a[id][i]!=-1){
            d[i]=min(d[i],a[id][i]);
        }
    }
    if(cnt==n) printf("%d\n",ans);
    else printf("-1\n");
}

int main(){
    work();
    return 0;
}

/*
5
-1 2 3 1 -1
2 -1 -1 4 -1
3 -1 -1 1 1
1 4 1 -1 3
-1 -1 1 3 -1
*/

C++: #include <iostream>
#include <limits>

constexpr int MAXN = 100;
int G[MAXN][MAXN];
int D[MAXN];
int P[MAXN];
int C[MAXN];
int n;
constexpr int MAXINT = 2001;

#define FOR(i,a,b) for(int i = (a); i<(b); ++i)

int main() {

    std::cin >> n;
    FOR(i, 0, n)
    FOR(j, 0, n)
    {
        int v;
        std::cin >> v;
        if(v < 0) G[i][j] = MAXINT;
        else G[i][j] = v;
    }

    FOR(i, 0, n) D[i] = MAXINT;

    int r = 0;
    D[r] = 0;
    P[r] = -1;
    while (true) {
        int mincost = MAXINT;
        int t;
        for (int i = 0; i < n; i++) {
            if ((C[i] == 0) && (D[i] < mincost)) {
                mincost = D[i];
                t = i;
            }
        }
        if (mincost == MAXINT) {
            break;
        }

        C[t] = 1;
        P[t] = r;

        for (int i = 0; i < n; i++) {
            if ((C[i] == 0) && (G[t][i] < D[i])) {
                D[i] = G[t][i];
            }
        }
        r = t;
    }

    int sum = 0;
    FOR(i, 0, n) {
        sum += D[i];
    }
    std::cout << sum << std::endl;

}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <queue>
#include <stack>
#include <map>
#define FOR(i,b,e) for(int i=b;i<e;i++)
#define REP(i,n) FOR(i,0,n)
#define INFTY 1000000
using namespace std;

const int N = 100;
int n;
int D[N], M[N][N];
bool V[N];

int prim() {
  int nn, minD, cost;

  REP(i, n) D[i] = INFTY;
  D[0] = 0;

  while (true) {
    minD = INFTY;
    REP(i, n) {
      if (!V[i] && D[i] < minD) {
        minD = D[i];
        nn = i;
      }
    }
    if (minD == INFTY) break;

    cost += D[nn];
    V[nn] = true;
    REP(i, n) {
      if (!V[i] && M[nn][i] != -1 && M[nn][i] < D[i]) {
        D[i] = M[nn][i];
      }
    }
  }

  return cost;
}

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

  cin >> n;
  REP(i, n) {
    REP(j, n) {
      cin >> M[i][j];
    }
  }

  cout << prim() << endl;
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <cmath>
#include <deque>
#include <iterator>
#include <map>
#include <queue>
#include <string>
#include <tuple>
#include <utility>
using namespace std;

using ll=long long;
template<class T> using V = vector<T>;
template<class T, class U> using P = pair<T, U>;
using vll = V<ll>;
using vvll = V<vll>;
#define rep(i, k, n) for (ll i=k; i<(ll)n; ++i)
#define REP(i, n) rep(i, 0, n)
template<class T> inline bool chmax(T& a, T b) {if (a<b) {a=b; return true;} return false;}
template<class T> inline bool chmin(T& a, T b) {if (a>b) {a=b; return true;} return false;}

vll parent(100);
vll size(100);

ll root(ll i) {
  if (parent.at(i) != i) parent.at(i) = root(parent.at(i));
  return parent.at(i);
}

bool same(ll i, ll j) {
  return root(i) == root(j);
}

void unite(ll i, ll j) {
  ll pi = root(i), pj = root(j);
  if (pi == pj) return;
  if (size.at(pi) < size.at(pj)) parent.at(pi) = pj;
  else if (size.at(pi) > size.at(pj)) parent.at(pj) = pi;
  else {
    if (pi < pj) {
      parent.at(pj) = pi; size.at(pi)++;
    } else {
      parent.at(pi) = pj; size.at(pj)++;
    }
  }
}

int main() {
  ll n; cin >> n;
  REP(i, n) parent.at(i) = i;
  priority_queue<vll, vvll, greater<vll> > pq;
  REP(i, n) REP(j, n) {
    ll c; cin >> c;
    if (i >= j) continue;
    if (c != -1) {
      vll p; p.push_back(c); p.push_back(i); p.push_back(j);
      pq.push(p);
    }
  }
  ll ans = 0;
  while (!pq.empty()) {
    vll q = pq.top(); pq.pop();
    if (root(q.at(1)) == root(q.at(2))) continue;
    else {
      ans += q.at(0);
      unite(q.at(1), q.at(2));
    }
  }
  cout << ans << endl;
  return 0;
}