Go: package main

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

type color int

const (
	white color = iota // 未訪問
	mst                // MSTとして確定
)

// 頂点
type node struct {
	matrix   []int // 隣接行列
	minToMst int   // MSTへの重み最小値
	parent   int   // 最小値の相手
	color    color
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	n := eGetInt(scanner)

	nodes := make([]node, n)
	for i := 0; i < n; i++ {
		nodes[i].matrix = eAtoi((strings.Fields(eGetline(scanner))))
		nodes[i].parent = -1
		nodes[i].minToMst = math.MaxInt16
		nodes[i].color = white
	}

	// 初回はnode[0]がMSTに入るように,minToMstを設定
	nodes[0].minToMst = 0
	for { // 全部チェックするまで

		// どの頂点を新たにMSTに取り込むか
		min := math.MaxInt16
		u := -1
		for i := 0; i < n; i++ {
			if nodes[i].color == mst {
				continue
			}
			if nodes[i].minToMst < min {
				u = i
				min = nodes[i].minToMst
			}
		}

		if u == -1 {
			break
		}
		// MSTに追加する頂点決定
		nodes[u].color = mst

		// MSTに属していない頂点それぞれについて,MSTとの最小の重みを更新
		for i := 0; i < n; i++ {
			if nodes[i].color == mst {
				continue
			}
			if nodes[i].matrix[u] == -1 {
				// 接してない
				continue
			}
			if nodes[i].matrix[u] < nodes[i].minToMst {
				// node[u]との重みが最小
				nodes[i].minToMst = nodes[i].matrix[u]
				nodes[i].parent = u
			}
		}
	}

	sum := 0
	for _, v := range nodes {
		// node[0]はparentが入っていない
		if v.parent != -1 {
			sum += v.matrix[v.parent]
		}
	}
	fmt.Println(sum)
}

func eGetInt(scanner *bufio.Scanner) int {
	t := eAtoi(strings.Fields(eGetline(scanner)))
	return t[0]
}

func eGetline(p *bufio.Scanner) string {
	if !p.Scan() {
		if err := p.Err(); err != nil {
			log.Fatal(err.Error())
		}
		return ""
	}
	return p.Text()
}

func eAtoi(s []string) []int {
	ints := make([]int, len(s))
	var err error
	for i, e := range s {
		ints[i], err = strconv.Atoi(e)
		if err != nil {
			log.Fatal(err.Error())
		}
	}
	return ints
}

C++: #ifdef LOCAL_BUILD
#include "pch.h"
#define DLOG(msg) cout << "#" << __LINE__ << ":" << msg << endl;
#define DLOG_V(var) \
  cout << "#" << __LINE__ << ":" << #var << " : " << var << endl;
#else
#include <bits/stdc++.h>
#define DLOG(msg)
#define DLOG_V(var)
#endif
using namespace std;

struct Edge {
  int weight;
  int source;
  int destination;
};

int main() {
  int n;
  cin >> n;
  auto cmp = [](const Edge& lhs, const Edge& rhs) {
    return lhs.weight > rhs.weight;
  };
  vector<vector<Edge>> edges(n, vector<Edge>());
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      int w;
      cin >> w;
      if (w != -1) {
        Edge e {w, i, j};
        edges[i].push_back(move(e));
      }
    }
  }

  int total_weight = 0;
  unordered_set<int> contained;
  contained.insert(0);
  priority_queue<Edge, vector<Edge>, decltype(cmp)> q(cmp);
  for (auto&& e : edges[0]) {
    q.push(e);
  }
  while (static_cast<int>(contained.size()) < n) {
    auto e = q.top();
    q.pop();
    if (contained.find(e.destination) == contained.end()) {
      total_weight += e.weight;
      contained.insert(e.destination);
      for (auto&& edge : edges[e.destination]) {
        q.push(edge);
      }
    }
  }
  cout << total_weight << endl;
}

C++: #include <iostream>
#include <queue>
#include <vector>
#include <utility>
using namespace std;
typedef pair<int,int> Info;
int main(){
        int N;
        cin >> N;
        vector <vector<int> > adjancey(N,vector<int>(N));
        priority_queue<Info, vector<Info>,greater<Info> > descending;
        for(int i=0 ; i<N ; i++ )
                for(int j=0 ; j<N ; j++ )
                        cin >> adjancey[i][j];
        Info wn;
        int now=0,counter=0;
        vector <bool> past(N,false);
        while(true){
                past[now]=true;
                for(int i=0 ; i<N ; i++ ){
                        if(past[i])
                                continue;
                        if(adjancey[now][i]==-1)
                                continue;
                        wn.first=adjancey[now][i];
                        wn.second=i;
                        descending.push(wn);
                }
                wn=descending.top();descending.pop();
                now=wn.second;
                if(past[now])break;
                counter+=wn.first;
        }
        cout << counter << endl;
        return 0;
}

C++: #include <stdio.h>

#define MAX 101
#define INF 100000
#define FLAG 1
#define DEFLAG 0

int main(){
  int i,j,n,min,a[MAX][MAX],k,x[MAX],minNode,sum=0,g[MAX];
  
  scanf("%d",&n);
  for(i=0;i<n;i++){
    for(j=0;j<n;j++){
      scanf("%d",&a[i][j]);
    }
  }

  //calc
  k = 1;
  x[0] = 0;
  g[0] = FLAG;
  for(i=1;i<n;i++){
    g[i] = DEFLAG;
  }
  
  while(1){
    min = INF;
    
    for(i=0;i<k;i++){
      for(j=0;j<n;j++){
	if(min > a[x[i]][j] && a[x[i]][j]!= -1 && g[j] != FLAG){
	  min = a[x[i]][j];
	  minNode = j;
	}
      }
    }

    if(k!=n){
      x[k] = minNode;
      g[minNode] = FLAG;
      k++;
      sum += min;
    }else{ break; }
    
  }

  printf("%d\n",sum);
  
  return 0;
}

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

using namespace std;

const int N = 100;

enum {
    WHITE,
    GRAY,
    BLACK,
};

int main() {
    int A[N][N];

    int n;
    cin >> n;
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            int a;
            cin >> a;
            A[i][j] = a == -1 ? a : numeric_limits<int>::max();
        }
    }

    // ????????????
    vector<int> color(n, WHITE); // ??????????¨??????¶???
    vector<int> d(n, numeric_limits<int>::max());
    vector<int> p(n, -1);

    d[0] = 0;
    while (1) {
        int minv = numeric_limits<int>::min();
        int u = -1;
        for (int 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 (int v = 0; v < n; ++v) {
            if (color[v] != BLACK && A[u][v] != numeric_limits<int>::max()) {
                if (d[v] > A[u][v]) {
                    d[v] = A[u][v];
                    p[v] = u;
                    color[v] = GRAY;
                }
            }
        }
    }
    int sum;
    for (int i =0; i < n; ++i) {
        if (p[i] != -1)
            sum += A[i][p[i]];
    }
    cout << sum << endl;

    return 0;
}

C++: #define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
//#include "MyMath.h"
//#include "MyDisjointset.h"
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <functional>
#include <stdio.h>
using namespace std;
typedef pair<int, int> P;
const int INF = 2000000000;
int main() {
	int n; cin >> n;
	int m[101][101];
	for (int i = 0; i < n; i++)for (int j = 0; j < n; j++)
		cin >> m[i][j];
	int res = 0;
	int *d = new int[n];
	fill(d, d + n, INF); 
	d[0] = 0;
	bool *visit = new bool [n];
	priority_queue <P, vector<P>, greater<P> > q;
	q.push(P(d[0], 0));
	while (!q.empty()) {
		int f = q.top().first, s = q.top().second;
		//cout << "f " << f << " s " << s << endl;
		visit[s] = true;
		res += f;
		q.pop();
		for (int i = 0; i < n; i++) {
			if (i == 1) {
				//cout << !visit[i] << " " << m[s][i] << " " << d[i] << endl;
			}
			if (!visit[i] && m[s][i] != -1 && m[s][i] < d[i]) {
				d[i] = m[s][i];
				//cout << "i " << i << " d " << d[i] << endl;
				q.push(P(d[i], i));
			}
		}
	}
	cout << res << endl;
	delete[] d, visit;
	return 0;
}