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++: #include <cstdio>
#include <vector>
#include <algorithm>
#include <queue>

using namespace std;

template <class T>
using gp_queue=priority_queue<T, deque<T>, less<T>>;

using Weight=int;
using DArray=vector<Weight>;
using DMatrix=vector<DArray>;

static const Weight INF=1<<29;

struct Edge {
    size_t src, dst;
    Weight weight;
    Edge() {}
    Edge(size_t src, size_t dst, Weight weight=1):
        src(src), dst(dst), weight(weight)
    {}
};

bool operator<(const Edge &e, const Edge &f) {
    if (e.weight != f.weight) {
        return e.weight > f.weight;
    } else {
        return e.src!=f.src? e.src<f.src : e.dst<f.dst;
    }
}

using Edges=vector<Edge>;
using Vertex=vector<Edge>;
using Graph=vector<Vertex>;

void join(Graph &g, size_t s, size_t d, Weight w=1) {
    // non-directed
    g[s].push_back(Edge(s, d, w));
    g[d].push_back(Edge(d, s, w));
}

void connect(Graph &g, size_t s, size_t d, Weight w=1) {
    // directed
    g[s].push_back(Edge(s, d, w));
}

pair<Weight, Edges> minimum_spanning_tree(const Graph &g, size_t s=0) {
    /* Prim */
    size_t V=g.size();
    Edges T;
    Weight total=0;

    vector<bool> visited(V);
    gp_queue<Edge> q; q.push(Edge(-1, s, 0));
    while (!q.empty()) {
        Edge e=q.top(); q.pop();
        if (visited[e.dst]) continue;

        T.push_back(e);
        total += e.weight;
        visited[e.dst] = true;
        for (const Edge &f: g[e.dst])
            if (!visited[f.dst])
                q.push(f);
    }

    return make_pair(total, T);
}

int main() {
    size_t n;
    scanf("%zu", &n);

    Graph g(n);
    for (size_t i=0; i<n; ++i)
        for (size_t j=0; j<n; ++j) {
            Weight a;
            scanf("%d", &a);

            if (a > -1)
                connect(g, i, j, a);
        }

    printf("%d\n", minimum_spanning_tree(g).first);
    return 0;
}

C++: #include<iostream>
using namespace std;
static const int MAX = 100;
static const int INFTY = (1<<21);
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;

struct Node{
  int color, dist, parent;
};

int n, M[MAX][MAX];
Node node[MAX];

void Printnodes(){
  string col[3] = {"WHITE", "GRAY ", "BLACK"};
  for(int i = 0; i < n; i++){
    cout << "nodeID : " << i ;
    cout << " color : " << col[node[i].color];
    cout << " parent : " << node[i].parent;
    cout << " dist : " << node[i].dist << endl;
  }
  cout << endl;
}

int main(){
  cin >> n;

  for(int i = 0; i < n; i++){
    for(int j = 0; j < n; j++){
      cin >> M[i][j];
    }
    node[i].color = WHITE;
    node[i].dist = INFTY;
    node[i].parent = -1;
  }

  int p = 0;
  node[p].color = BLACK;
  node[p].dist = 0;

  while(true){
    //Printnodes();

    for(int i = 0; i < n; i++){
      if(M[p][i] > 0 && node[i].color != BLACK){
        node[i].dist = M[p][i];
        node[i].parent = p;
        node[i].color = GRAY;
      }
    }

    int min_d = INFTY;
    p = -1;
    for(int i = 0; i < n; i++){
      if(node[i].dist < min_d && node[i].color != BLACK){
        min_d = node[i].dist;
        p = i;
      }
    }

    if(p == -1)
      break;

    node[p].color = BLACK;
    //cout << "node " << p << " turns to BLACK!" << endl;
  }

  int sum = 0;

  for(int i = 0; i < n; i++){
    if(node[i].parent >= 0)
      sum += M[i][node[i].parent];
  }
  cout << sum << endl;
  return 0;
}

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

struct Edge {
	int row, column, weight;
};

void divide_graph(bool[], int);

int n;
Edge edges[99]{};

int main()
{
	int in_tmp, min{}, matrix[100][100]{};

	std::cin >> n;
	for (int i = 0; i < n; ++i)
		for (int j = 0; j < n; ++j)
		{
			std::cin >> in_tmp;
			matrix[i][j] = in_tmp;
		}

	int rest = n - 1, pre_size = 0;
	std::vector<int> queue{ 0 };
	bool visited[100]{ true };
	for (int i = 1; rest; ++i)
	{
		int now_size = queue.size();
		for (int j = pre_size; j < now_size; ++j)
			for (int k = 0; k < n; ++k)
			{
				int tmp = matrix[queue[j]][k];
				if (tmp != -1 && !visited[k])
				{
					visited[k] = true;
					rest--;
					edges[queue.size() - 1] = { queue[j], k , tmp};
					queue.push_back(k);
				}
			}
		pre_size = now_size;
	}
	for (int i = 0; i < n - 1; ++i)
	{
		bool conne_row[100]{};
		divide_graph(conne_row, i);

		for (int j = 0; j < n; ++j)
		{
			if (!conne_row[j]) continue;
			for (int k = 0; k < n; ++k)
			{
				if (conne_row[k]) continue;
				if (matrix[j][k] == -1 || matrix[j][k] >= edges[i].weight) continue;

				edges[i] = { j, k, matrix[j][k] };
			}
		}
	}

	for (int i = 0; i < n - 1; ++i)
		min += edges[i].weight;

	std::cout << min << std::endl;
	
	return 0;
}

void divide_graph(bool conne_row[], int max_edge)
{
	conne_row[edges[max_edge].row] = true;

	std::vector<int> queue = { edges[max_edge].row };
	int pre_size = 0;

	bool visit_edge[100]{};
	visit_edge[max_edge] = true;

	while (pre_size != queue.size())
	{
		int now_size = queue.size();
		for (int j = pre_size; j < now_size; ++j)
		{
			for (int k = 0; k < n - 1; ++k)
			{
				if (visit_edge[k]) continue;
				if (edges[k].row == queue[j])
				{
					visit_edge[k] = true;
					conne_row[edges[k].column] = true;
					queue.push_back(edges[k].column);
				}
				else if (edges[k].column == queue[j])
				{
					visit_edge[k] = true;
					conne_row[edges[k].row] = true;
					queue.push_back(edges[k].row);
				}
			}
		}
		pre_size = now_size;
	}
	return;
}