Go: package main

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

const (
	WHITE int = iota
	GRAY
	BLACK
)

type graph struct {
	G [][]int
	C []int
	D []int
	P []int
}

var INFTY = 1 << 32

func constructGraph() *graph {
	sc := bufio.NewScanner(os.Stdin)
	sc.Scan()
	n, _ := strconv.Atoi(sc.Text())

	g := make([][]int, n)
	c := make([]int, n)
	d := make([]int, n)
	p := make([]int, n)

	for i := 0; i < n; i++ {
		sc.Scan()
		fields := strings.Fields(sc.Text())
		g[i] = toIntSlice(fields)
	}

	return &graph{g, c, d, p}
}

func (g *graph) initMst() {
	for i := 0; i < len(g.G); i++ {
		g.C[i] = WHITE
		g.D[i] = INFTY
	}
	g.D[0] = 0
	g.P[0] = -1
}

func (g *graph) primMst() int {
	g.initMst()

	var u int
	for {
		mincost := INFTY
		for i := 0; i < len(g.G); i++ {
			if g.C[i] != BLACK && g.D[i] < mincost {
				mincost = g.D[i]
				u = i
			}
		}

		if mincost == INFTY {
			break
		}

		g.C[u] = BLACK

		for i := 0; i < len(g.G); i++ {
			if g.C[i] != BLACK && g.existEdge(u, i) {
				if g.G[u][i] < g.D[i] {
					g.D[i] = g.G[u][i]
					g.P[i] = u
				}
			}
		}
	}

	totalCost := 0
	for _, cost := range g.D {
		totalCost += cost
	}
	return totalCost
}

func (g *graph) existEdge(u, v int) bool {
	return g.G[u][v] != INFTY && g.G[v][u] != INFTY
}

func toIntSlice(s []string) []int {
	a := make([]int, len(s))
	for i, v := range s {
		num, _ := strconv.Atoi(v)
		if num == -1 {
			num = INFTY
		}
		a[i] = num
	}
	return a
}

func main() {
	g := constructGraph()
	fmt.Println(g.primMst())
}

C++: #pragma region kyomukyomupurin

/**
 *    author  : 𝒌𝒚𝒐𝒎𝒖𝒌𝒚𝒐𝒎𝒖𝒑𝒖𝒓𝒊𝒏
 *    created : 2020-04-24 11:40:03
 **/

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <chrono>
#include <cmath>
#include <complex>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

using int64 = long long;

template <class T>
using binary_heap = std::priority_queue<T, std::vector<T>, std::greater<T>>;

template <class T, class U>
std::ostream& operator<<(std::ostream& os, const std::pair<T, U>& p) {
  return os << '(' << p.first << ", " << p.second << ')';
}

template <class T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& vec) {
  int flag = 0;
  for (auto e : vec) os << (flag++ ? ", " : "{") << e;
  return os << '}';
}

template <class T>
std::ostream& operator<<(std::ostream& os, const std::set<T>& st) {
  int flag = 0;
  for (auto e : st) os << (flag++ ? ", " : "{") << e;
  return os << '}';
}

template <class T, class U>
std::ostream& operator<<(std::ostream& os, const std::map<T, U>& mp) {
  int flag = 0;
  for (auto e : mp) os << (flag++ ? ", " : "{") << e;
  return os << '}';
}

template <class T>
std::istream& operator>>(std::istream& is, std::vector<T>& vec) {
  for (T& e : vec) is >> e;
  return is;
}

#define all(_) begin(_), end(_)
#define rall(_) rbegin(_), rend(_)

#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]: ", debug_out(__VA_ARGS__)
#else
#define debug(...)
#endif

void debug_out() { std::cerr << '\n'; }

template <class Head, class... Tail>
void debug_out(Head&& head, Tail&&... tail) {
  std::cerr << head;
  if (sizeof...(Tail) != 0) std::cerr << ", ";
  debug_out(std::forward<Tail>(tail)...);
}

#pragma endregion kyomukyomupurin


class UnionFind {
 public:
  UnionFind(int n) : n_(n) { initialize(); }

  int root(int x) {
    assert(0 <= x && x < n_);
    if (parent_[x] == -1)
      return x;
    else
      return parent_[x] = root(parent_[x]);
  }

  bool same(int x, int y) {
    assert(0 <= x && x < n_ && 0 <= y && y < n_);
    return root(x) == root(y);
  }

  void unite(int x, int y) {
    assert(0 <= x && x < n_ && 0 <= y && y < n_);
    x = root(x);
    y = root(y);
    if (x == y) return;
    if (rank_[x] < rank_[y]) std::swap(x, y);
    if (rank_[x] == rank_[y]) ++rank_[x];
    parent_[y] = x;
    return;
  }

 private:
  const int n_;
  std::vector<int> parent_;
  std::vector<int> rank_;

  void initialize() {
    parent_.assign(n_, -1);
    rank_.assign(n_, 0);
  }
};

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  int n; cin >> n;
  vector<vector<int>> a(n, vector<int>(n)); cin >> a;

  vector<pair<int, pair<int, int>>> v;

  for (int i = 0; i < n; ++i) {
    for (int j = i; j < n; ++j) {
      if (a[i][j] == -1) continue;
      v.emplace_back(a[i][j], make_pair(i, j));
    }
  }

  sort(all(v));
  debug(v);

  UnionFind uf(n);

  int ans = 0;

  for (auto e : v) {
    if (uf.same(e.second.first, e.second.second)) continue;
    ans += e.first;
    uf.unite(e.second.first, e.second.second);
  }

  cout << ans << endl;

  return 0;
}

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

#define MAX_NODE 10000

using namespace std;

typedef struct{
	int from,to,weight;
} Edge;

typedef struct{
	int parent,rank;
} Node;

Node node[MAX_NODE];
vector<Edge> edge;

void init(int n){
	for(int i=0;i<n;i++){
		node[i].parent=i;
		node[i].rank=0;
	}
}

int find(int n){
	return node[n].parent==n?n:node[n].parent=find(node[n].parent);
}

void unite(int x,int y){
	int xr=find(x);
	int yr=find(y);
	if(node[xr].rank>node[yr].rank){
		node[yr].parent=node[xr].parent;
	}else if(xr!=yr){
		node[xr].parent=node[yr].parent;
		if(node[xr].rank==node[yr].rank){
			node[yr].rank++;
		}
	}
}

bool operator<(const Edge& left,const Edge& right){
	return (left.weight<right.weight?1:0);
}

int main(){
	int n,tmp,w=0;
	Edge e;
	cin>>n;
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			cin>>tmp;
			if(tmp!=-1){
				e.from=i;
				e.to=j;
				e.weight=tmp;
				edge.push_back(e);
			}
		}
	}
	sort(edge.begin(),edge.end());
	init(n);
	for(int i=0;i<edge.size();i++){
		if(find(edge[i].from)==find(edge[i].to)){
			continue;
		}
		w+=edge[i].weight;
		unite(edge[i].from,edge[i].to);
	}
	cout<<w<<endl;
	return 0;
}

C++: #include <iostream>
#include <ios>
#include <vector>
#include <queue>
#include <utility>
#include <tuple>

using namespace std;
int Es[100][100] = {{0}};

int MST(int n){
    vector<bool> used(n, false);
    priority_queue< pair<int, int> > pq;
    pq.emplace(0, 0);
    int dist = 0;
    int node = 0;
    int total_dist = 0;
    while (! pq.empty()){
        tie(dist, node) = pq.top();
        pq.pop();
        if (used[node]) continue;
        used[node] = true;
        total_dist -= dist;
        for (int new_node = 0; new_node != n; ++new_node){
            if (used[new_node] || Es[node][new_node] == -1) continue;
            pq.emplace(-Es[node][new_node], new_node);
        }
    }
    return total_dist;
}

int main()
{
    ios::sync_with_stdio(false);
    int n = 0;
    cin >> n;
    for (int i = 0; i != n; ++i){
        for (int j = 0; j != n; ++j){
            cin >> Es[i][j];
        }
    }
    cout << MST(n) << endl;
    return 0;
}

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

#define check cout<<"-------"<<endl
#define debug(x) cout<<x<<endl

#define N 101
#define MAX 2001

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    int a[N][N];
    int list[MAX];
    
    int n;
    cin>>n;
    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= n; j++) {
            cin>>a[i][j];
        }
    }
    
    int sum = 0, min, mini, i, j;
    int lp = n-1;
    list[0] = 1;
    for(i = 1; i <= n; i++) a[i][1] = -1;
    while(lp--) {
        min = 2001;
        for(i = 0; list[i] !=0; i++) {
            for(j = 1; j <= n; j++) {
                if(min > a[ list[i] ][j] && a[ list[i] ][j] != -1) {
                    min = a[ list[i] ][j];
                    mini = j;
                }
            }
        }
        sum += min;
        list[i] = mini;
        for(int j = 1; j <= n; j++) a[j][mini] = -1;
    }
    
    cout<<sum<<endl;
    
    return 0;
}