Go: package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 500001), 500000)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func main() {
	fp := os.Stdin
	if len(os.Args) > 1 {
		fp, _ = os.Open(os.Args[1])
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(os.Stdout)

	n := getNextInt(scanner)

	g := make([][]int, n)
	ans := make([][]int, n)

	inf := 1 << 30
	for i := 0; i < n; i++ {
		g[i] = make([]int, n)
		ans[i] = make([]int, n)
		for ii := 0; ii < n; ii++ {
			g[i][ii] = getNextInt(scanner)
			if g[i][ii] == -1 {
				g[i][ii] = inf
			}
			ans[i][ii] = -1
		}
	}

	im := make(IntMap, 1)

	im.increment(1)

	for len(im) < n {
		min := inf
		u := -1
		v := -1
		for i := range im {
			for ii := 0; ii < n; ii++ {
				if im.get(ii) != 0 {
					continue
				}
				if min > g[i][ii] {
					min = g[i][ii]
					u = i
					v = ii
				}
			}
		}
		im.increment(v)
		ans[u][v] = min
		ans[v][u] = min
	}

	sum := 0
	for i := 0; i < n-1; i++ {
		for ii := i + 1; ii < n; ii++ {
			if ans[i][ii] != -1 {
				sum += ans[i][ii]
			}
		}
	}

	fmt.Fprintln(writer, sum)
	writer.Flush()
}

// IntMap ...
type IntMap map[int]int

func (m IntMap) get(key int) int {
	if _, ok := m[key]; ok {
		return m[key]
	}
	return 0
}
func (m IntMap) increment(key int) {
	if _, ok := m[key]; ok == false {
		m[key] = 0
	}
	m[key]++
}

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

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

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

vector<edge> es;
int V,E; //頂点数、辺数
const int N = 1e5+10;

struct UF{
  const int N = 1e5+10;
  vector<int> par; //親
  vector<int> depth; //木の深さ

  void init(){
    par.resize(N);
    depth.resize(N);
    for(int i = 0; i < N; i++){
      par[i] = i;
      depth[i] = 0;
    }
  }

  //木の根を求める
  int root(int x){
    if(par[x] == x){
      return x;
    }
    else {
      return par[x] = root(par[x]);
    }
  }

  //xとyの属する集合を併合
  void unite(int x,int y){
    x = root(x);
    y = root(y);
    if(x == y) return;

    if(depth[x] < depth[y]) par[x] = y;
    else {
      par[y] = x;
      if(depth[x] == depth[y]) depth[x]++;
    }
  }
}uf;

int kruskal(){
  sort(es.begin(),es.end(),comp);
  uf.init();
  int min_cost = 0;
  for(int i = 0; i < es.size(); i++){
    edge e = es[i];
    if(e.cost == -1) continue;
    if(uf.root(e.u) != uf.root(e.v)){
      min_cost += e.cost;
      uf.unite(e.u,e.v);
    }
  }
  return min_cost;
}

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

  cin >> V;
  for(int i = 0; i < V; i++){
    for(int j = 0; j < V; j++){
      int cost; cin >> cost;
      es.push_back({i,j,cost});
    }
  }
  cout << kruskal() << endl;

  return 0;
}