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 <iostream>
#include <vector>
#include <queue>
#include <cmath>
#include <algorithm>
#include <utility>

using namespace std;

// int search_xyz(int e);
int tree[100];

int root(int a);
bool is_same_set(int p, int q);
void joint(int x, int y);

int main()
{
    int n, tempa, k = 0;
    cin >> n;
    int e = n * (n - 1) / 2;
    pair<int, pair<int, int>

         >
        a[e];
    for (int i = 0; i < n; i++)
    {
        tree[i] = i;
        for (int j = 0; j < n; j++)
        {
            cin >> tempa;
            if (i < j)
            {
                a[k] = make_pair(tempa, make_pair(i, j));
                k++;
            }
        }
    }
    sort(a, a + e);
    int result = 0;
    for (int i = 0; i < e; i++)
    {
        if (a[i].first == -1)
            continue;

        if (is_same_set(a[i].second.first, a[i].second.second))
        {
            continue;
        }
        else
        {
            joint(a[i].second.first, a[i].second.second);
            result += a[i].first;
        }
    }
    cout << result << endl;
    return 0;
}

int root(int a)
{
    if (tree[a] == a)
    {
        return a;
    }
    else
    {
        tree[a] = root(tree[a]);
        return tree[a];
    }
}

bool is_same_set(int p, int q)
{
    return root(p) == root(q);
}

void joint(int x, int y)
{
    tree[root(x)] = root(y);
}

C++: #include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
#include <string>
#include <queue>
using namespace std;
const int MAXN = 10005;
const int INF = 1 << 30;
const int NIL = -1;
struct Node {
	int v, w, next;	//u->v
	friend bool operator<(const Node& a, const Node& b)
	{
		return a.w > b.w;
	}
	Node() {}
	Node(int vv, int ww) : v(vv), w(ww) {}
};
int cnt;
int head[MAXN];
Node edge[MAXN];




int prim(int n)
{
	
	bool vis[MAXN]{ false };
	priority_queue<Node> q;
	vis[1] = true;

	for (int i = head[1]; i != NIL; i = edge[i].next)
		q.push(edge[i]);
	int count = 0;
	int ans = 0;
	Node t;
	while (count < n - 1)
	{
		t = q.top();
		q.pop();
		if (vis[t.v])
			continue;
		vis[t.v] = true;
		ans += t.w;
		++count;
		for (int i = head[t.v]; i != NIL; i = edge[i].next)
			if (!vis[edge[i].v])
				q.push(edge[i]);

	}
	
	return ans;
}


void add(int u, int v, int w)
{
	edge[cnt].v = v;
	edge[cnt].w = w;
	edge[cnt].next = head[u];
	head[u] = cnt++;
}



int main(void)
{
	int n, m, u, v, w;
	scanf("%d", &n);
	int N = n * n;
	for (int i = 0; i <= N; ++i)
		head[i] = NIL;
	cnt = 0;
	for (int i = 1; i <= n; ++i)
	{
		for (int j = 1; j <= n; ++j)
		{
			scanf("%d", &w);
			if (w != NIL)
				add(i, j, w);
		}
	}
	cout << prim(n) << endl;
	return 0;
}

C++: #include <iostream>
#include <utility>
#include <string>
#include <algorithm>
#include <set>
#include <deque>
#include <vector>
#define MAX 150
#define rep(i,p) for(int i=0;i<p;i++)
#define rep1(i,p) for(int i=1;i<=p;i++)
#define pb push_back
using namespace std;
int n,temp,a,b,size;
int A[MAX];
int B[MAX];
int W[MAX];
int V[MAX];
pair<int,int> edge[MAX];

int root (int child) {
  int a = child;
  while (V[a] != a) {
    a = V[a];
  }
  return a;
}

int main(){
  size = 0;
  cin >> n;
  rep(i,n) 
    rep(j,n) {
      cin >> W[size];
      if (i <= j) {        
        A[size] = i;
        B[size] = j;
        edge[size].first = W[size];
        edge[size].second = size;
        size++;
      }
    }

  // sort by lenght of edge
  sort(edge, edge+size+1);
  // printing
  // rep(i,size+1) {
  //   cout << A[edge[i].second] << " " << B[edge[i].second]
  //        << " " << edge[i].first << endl;;
  // }
  int sum = 0;
  rep(i,n) V[i] = i;
  rep(i,size) {
    if (edge[i].first == -1) continue;
    temp = edge[i].second;
    // a , b : vertex
    a = A[temp]; 
    b = B[temp];
    if (root(a) != root(b)) {
      // ?????????????????????
      sum += edge[i].first;
      if (root(a) != a && root(b) == b){        
        V[b] = a;
      } else if (root(a) == a && root(b) != b) {
        V[a] = b;
      } else if (root(a) == a && root(b) == b) {        
        V[a] = b;
      }
    } else {
    }
  }
  cout << sum << endl;
  return 0;
}

C++: //Minimum Spannig Tree
#include <iostream>
#include <vector>
#include <climits>

using namespace std;

static const int N = 100;
static const int WHITE = 0;
static const int BLACK = 1;

int prim(vector<vector<int>> &G, int n) {
    int colors[n], d[n];
    for(int i = 0; i < n; i++) {
        colors[i] = WHITE;
        d[i] = INT_MAX;
    }

    d[0] = 0;
    int l = 0;
    while(1) {
        int u = -1;
        int mincost = INT_MAX;
        for(int i = 0; i < n; i++) {
            if(colors[i] == WHITE && d[i] < mincost) {
                mincost = d[i];
                u = i;
            }
        }
        if(u == -1) break;
        colors[u] = BLACK;
        l += mincost;
        for(int v = 0; v < n; v++)
            if(colors[v] == WHITE && G[u][v] != -1)
                if(G[u][v] < d[v])
                    d[v] = G[u][v];
    }
    return l;
}

int main() {
    int n;
    cin >> n;
    vector<vector<int>> G(n, vector<int>(n));
    for(int i = 0; i < n; i++)
        for(int j = 0; j < n; j++)
            cin >> G[i][j];

    cout << prim(G, n) << endl;
}