Go: package main

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

const BIG_COST = 3000

const (
	ST_NOT_YET = iota
	ST_JOINED
)

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	nNodes := scanInt(sc)
	costs := make([][]int, nNodes)

	for r := 0; r < nNodes; r++ {
		costs[r] = make([]int, nNodes)
		for c := 0; c < nNodes; c++ {
			costs[r][c] = scanInt(sc)
		}
	}

	states := make([]int, nNodes)
	minCosts := make([]int, nNodes)

	for i := 1; i < nNodes; i++ {
		minCosts[i] = BIG_COST
	}

	for {
		var u int
		minCost := BIG_COST

		for i := 0; i < nNodes; i++ {
			if states[i] != ST_JOINED && minCosts[i] < minCost {
				minCost = minCosts[i]
				u = i
			}
		}

		// 全てのノードが MST に JOIN できたので終了。
		if minCost == BIG_COST {
			break
		}

		states[u] = ST_JOINED

		for v := 0; v < nNodes; v++ {
			cost := costs[u][v]

			// MST内の u と MST外の v との辺の重みをそれぞれ minCosts[v] として
			// 保存しておく。このうち辺の重みが最小となる v が次のループで MST に JOIN する。
			if states[v] != ST_JOINED && cost != -1 && cost < minCosts[v] {
				minCosts[v] = cost
			}
		}
	}

	sum := 0
	for _, cost := range minCosts {
		sum += cost
	}
	fmt.Println(sum)
}

func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	v, _ := strconv.Atoi(sc.Text())
	return v
}

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