Go: package main

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

var (
	n int
	M [MAX][MAX]int
)

const MAX = 100
const WHITE = 0
const GRAY = 1
const BLACK = 2

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Scan()
	n, _ = strconv.Atoi(sc.Text())

	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			sc.Scan()
			c, _ := strconv.Atoi(sc.Text())
			if c != -1 {
				M[i][j] = c
			} else {
				M[i][j] = math.MaxInt64
			}

		}
	}
	prim()
}

func prim() {
	var u int
	var minv int
	var d [MAX]int
	var p [MAX]int
	var color [MAX]int

	for i := 0; i < n; i++ {
		d[i] = math.MaxInt64
		p[i] = -1
		color[i] = WHITE
	}

	d[0] = 0

	for {
		minv = math.MaxInt64
		u = -1
		for 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 v := 0; v < n; v++ {
			if color[v] != BLACK && M[u][v] != math.MaxInt64 {
				if d[v] > M[u][v] {
					d[v] = M[u][v]
					p[v] = u
					color[v] = GRAY
				}
			}
		}
	}
	sum := 0
	for i := 0; i < n; i++ {
		if p[i] != -1 {
			sum += M[i][p[i]]
		}
	}
	fmt.Println(sum)

}

C++: #include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<math.h>
#include<string>
#include<string.h>
#include<stack>
#include<queue>
#include<vector>
#include<utility>
#include<set>
#include<map>
#include<stdlib.h>
#include<iomanip>

using namespace std;

#define ll long long
#define ld long double
#define EPS 0.0000000001
#define INF 1e9
#define rep(i,n) for(i=0;i<n;i++)
#define loop(i,a,n) for(i=a;i<n;i++)
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)

typedef vector<int> vi;
typedef pair<int,int> pii;


struct edge{ int u, v, cost; };

bool comp(const edge& e1, const edge& e2){

  return e1.cost < e2.cost;

}

#define MAX_N 100000
#define MAX_E 100000

int par[MAX_N];

int rank[MAX_N];

void init(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]);

}

void unite(int x, int y){

  x = find(x);

  y = find(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){

  return find(x) == find(y);

}

edge es[MAX_E];

int V, E;

int kruskal(){

  sort(es, es+E, comp);

  init(V);

  int res = 0;

  for(int i = 0 ; i < E ; i++){

    edge e = es[i];

    if(!same(e.u, e.v)){

      unite(e.u, e.v);

      res += e.cost;

    }

  }

  return res;

}

int main(){
  int n;
  cin>>n;
  V=n;
  int i,j,k;
  E=0;
  rep(i,n)rep(j,n){
    int t;
    cin>>t;
    if(t<0)continue;
    es[E].u=i;
    es[E].v=j;
    es[E].cost=t;
    E++;
  }
  cout<<kruskal()<<endl;
}

C++: #include<iostream>
#include<utility>
#include<algorithm>
using namespace std;
int p[101];
void makeset(int N){
    for(int i=0;i<N;++i){
        p[i]=i;
    }
}
int root(int x){
    if(p[x]==x) return x;
    return (p[x]==root(p[x]));
}
void unite(int x,int y){
    p[root(x)]=root(y);
}
bool same(int x,int y){
    return root(x)==root(y);
}
int main(){
    int N,M=0;
    int A[101][101],cost[101],src[101],dst[101];
    cin>>N;
    makeset(N);
    for(int r=0;r<N;++r){
        for(int c=0;c<N;++c){
            cin>>A[r][c];
        }
        for(int c=r+1;c<N;++c){
            if(A[r][c]>-1){
                cost[M]=A[r][c];
                src[M]=r;
                dst[M]=c;
                M+=1;
            }
        }
    }

    
    pair<int,int>edges[101];
    for(int i=0;i<M;++i){
        edges[i]=make_pair(src[i],dst[i]);
    }
    sort(edges,edges+M);
    sort(cost,cost+M);
    int total=0,num=0,flag=0;
    for(int i=0;i<M;++i){
        unite(edges[i].first,edges[i].second);
        total+=cost[i];
        num++;
        if(num==N-1){
            flag=1;
            break;
        }
    }
    if(flag){
        cout<<total<<endl;
    }
}

C++: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vi = vector<int>;    using vvi = vector<vi>;
using vb = vector<bool>;   using vvb = vector<vb>;
using vl = vector<ll>;     using vvl = vector<vl>;
using vd = vector<double>; using vvd = vector<vd>;

#define REP(i,n) for(auto i = 0 * n; i < (n); ++i)
#define ALL(c) (c).begin(), (c).end()
#define FOR(i,s,e) for (ll i = s; i < (ll)e; i++)
#define TEN(x) ((ll)1e##x)

class UnionFind {
	typedef size_t U;
	vector<int32_t> v;
public:
	UnionFind(U n) : v(n, -1) {}
	U find(U x) { return v[x] < 0 ? x : (v[x] = find((U)v[x])); }
	bool same(U x, U y) { return find(x) == find(y); }
	U size(U x) { return (U)-v[find(x)]; }
	void unite(U x, U y) {
		x = find(x); y = find(y);
		if (x != y) {
			if (-v[x] < -v[y]) swap(x, y);
			v[x] += v[y];
			v[y] = x;
		}
	}
};

struct Edge{ uint32_t to, id; };
typedef vector<vector<Edge>> Graph;

template<class T> struct Kruskal{
	typedef uint32_t U;
	template<class V> Kruskal(U n, const V & u, const V & v, const vector<T> & w) : g(n) {
		vector<U> es(w.size());
		iota(es.begin(), es.end(), 0);
		sort(es.begin(), es.end(), [&](U i, U j) { return w[i] < w[j]; });
		UnionFind uf(n);
		for (U i : es) {
			if (!uf.same(u[i], v[i])) {
				uf.unite(u[i], v[i]); cost += w[i];
				g[u[i]].push_back({ (U)v[i], i }); g[v[i]].push_back({ (U)u[i], i });
			}
		}
	}
	Graph g; T cost = 0;
};

int main() {
	cin.tie(0);
	ios_base::sync_with_stdio(false);
	cout << fixed << setprecision(50);

	ll n; cin >> n;
	vl u, v, w;
	REP(i, n) REP(j, n) {
		ll x; cin >> x;
		if (x != -1) {
			u.push_back(i);
			v.push_back(j);
			w.push_back(x);
		}
	}
	Kruskal<ll> mst(n, u, v, w);
	cout << mst.cost << endl;
}

C++: #include <iostream>

using namespace std;
static const int inf = 10000;
static const int emp = -1;
static const int black = 1;
static const int white = 0;

int   graph[124][124] = {{}};;
int d[124] = {};
int color[124] = {};
int p[124] = {};
int n;

int  prim(){

  for(int i = 0;i < n;i++){
    d[i] = inf;
    color[i] = white;
    p[i] = -1;
  }

  d[0] = 0;
  int u ,v;
 

  while(1){

    v = inf;
    u = -1;

    for(int i = 0;i < n;i++){
      if(d[i] < v && color[i] != black){
	v= d[i];
	u = i;
      }
    }
    //    cout << "u = " << u << endl; 

    if(u == -1) break;
    color[u] = black;

    // cout << 'a' << endl;
    for(int i = 0;i < n;i++){
      if(graph[u][i] != -1 && color[i] != black){
	if(graph[u][i] < d[i]){
	  d[i] = graph[u][i];
	  p[i]= u;
	  //  cout << "d = " << d[i] <<endl;
	  // cout << "i = " << i << endl;
	}
      }
    }
  }

  int sum = 0;
  for(int i = 0;i < n;i++){
    if(p[i] != -1) sum += graph[i][p[i]];
  }
  return sum;
}



int main(){

  cin >> n;

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



  cout <<  prim() << endl;

  return 0;
}

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

struct UnionFind{
  int n;
  vector<int> r, p;
  UnionFind() {}
  UnionFind(int sz): n(sz), r(sz, 1), p(sz, 0){ iota(p.begin(), p.end(), 0); }
  int find(int x) {
    return (x == p[x] ? x : p[x] = find(p[x]));
  }
  bool same(int x, int y) {
    return find(x) == find(y);
  }
  void unite(int x, int y){
    x = find(x); y = find(y);
    if ( x == y ) return;
    if ( r[x] < r[y] ) swap(x, y);
    r[x] += r[y];
    p[y] = x;
  }
};

struct Edge {
  int cost, to, from;
  bool operator<(const Edge& e) const {
    return cost < e.cost;    
  }  
};

int main() {
  int n;
  cin >> n;

  vector<Edge> G;
  for ( int i = 0; i < n; i++ ) {
    for ( int j = 0; j < n; j++ ) {
      int cost;
      cin >> cost;
      if ( j > i && cost >= 0 ) G.push_back({cost, i, j});      
    }
  }

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

  UnionFind uf(n);
  int cnt = 0;
  int sum = 0;
  for ( Edge e: G ) {
    int cost = e.cost, to = e.to, from = e.from;
    if ( uf.same(to, from) ) continue;
    sum += cost;
    if ( ++cnt >= n-1 ) break;
    uf.unite(to, from);    
  }

  cout << sum << endl;
  
  return 0;
}