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 <utility>
#include <iostream>
#include <algorithm>
using namespace std;

int root(int a,int P[])
{
    if(P[a] == a)
        return a;
    else    return (P[a] = root(P[a],P));
}
int is_same_set(int a,int b,int P[])
{
    return root(a,P) == root(b,P);
}

int main()
{
    int n,i,j,M=0,minsum=0;
    cin >> n;
    //init(n);
    int a[n][n],cost[n*n],src[n*n],dst[n*n];
    for(i=0;i<n;i++)
        for(j=0;j<n;j++)
            cin >> a[i][j];
    for(int r=0;r<n;r++)
        for(int c=r+1;c<n;c++)
            if(a[r][c] > -1)
            {
                cost[M] = a[r][c];
                src[M] = r;
                dst[M] = c;
                M++;
            }
    pair<int,int> edeges[M];
    int P[n];
    for(i=0;i<n;i++)
        P[i] = i;
    for(i=0;i<M;i++)
    {
        edeges[i].first = cost[i];
        edeges[i].second = i;
    }
    sort(edeges,edeges+M);
    for(i=0;i<M;i++)
    {
        if(is_same_set(src[edeges[i].second], dst[edeges[i].second], P))
            continue;
        else    {
                    P[root(src[edeges[i].second],P)] = root(dst[edeges[i].second],P);
                    minsum += edeges[i].first;
                }
    }
    cout << minsum << endl;
    return 0;
}

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

vector< vector < int > > adjMatrix;

int findMSTCost(int n) {
  vector< bool > visited (n, false);
  visited[0] = true;
  int cost = 0;
  for (int numberVertices = 1; numberVertices < n; numberVertices++) {
    int minEdge = 3000, minVertex = n;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
	if ((i != j) &&
	    (visited[i] == true) &&
	    (visited[j] == false) &&
	    (adjMatrix[i][j] != -1) &&
	    (adjMatrix[i][j] < minEdge)) {
	  minEdge = adjMatrix[i][j];
	  minVertex = j;
	}
      }
    }
    cost += minEdge;
    visited[minVertex] = true;
  }
  return cost;
}

int main(int argc, char **argv) {
  ios_base::sync_with_stdio(false);
  int n;
  cin >> n;
  adjMatrix = vector< vector < int > > (n, vector< int > (n, 0));
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      cin >> adjMatrix[i][j];
  cout << findMSTCost(n) << endl;
  return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

typedef long long int ll;
typedef pair<ll, ll> pll;

#define FOR(i, n, m) for(ll (i)=(m);(i)<(n);++(i))
#define REP(i, n) FOR(i,n,0)
#define OF64 std::setprecision(10)

const ll MOD = 1000000007;
const ll INF = (ll) 1e15;

struct UnionFind {
    UnionFind(int n) {
        rank.assign(n, 0);
        parent.resize(n);
        for (int i = 0; i < n; ++i) {
            parent[i] = i;
        }
    }

    int find(int x) {
        if (x == parent[x])
            return x;
        return parent[x] = find(parent[x]);
    }

    bool same(int x, int y) {
        return find(x) == find(y);
    }

    void unit(int x, int y) {
        x = find(x);
        y = find(y);
        if (x == y)
            return;

        if (rank[x] < rank[y]) {
            parent[x] = y;
        }
        else {
            parent[y] = x;
            if (rank[x] == rank[y])
                rank[x]++;
        }
    }

    vector<int> rank;
    vector<int> parent;
};

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    ll N;
    cin >> N;
    priority_queue<tuple<pll, ll>, vector<tuple<pll, ll>>, function<bool(tuple<pll, ll>, tuple<pll, ll>)>> q(
            [](tuple<pll, ll> a, tuple<pll, ll> b) {
                return get<1>(a) > get<1>(b);
            });
    REP(i, N) {
        REP(j, N) {
            ll a;
            cin >> a;
            if (a < 0)
                continue;
            q.push(make_tuple(pll(i, j), a));
        }
    }
    UnionFind uf(N);
    ll ans = 0;
    while (!q.empty()) {
        auto t = q.top();
        q.pop();
        pll p = get<0>(t);
        if (uf.same(p.first, p.second))
            continue;
        uf.unit(p.first, p.second);
        ans += get<1>(t);
    }
    cout << ans << endl;
    return 0;
}

C++: #include <stdio.h>
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;

int n, M[MAX][MAX];

int prim() {
    int d[n], p[n];
    int color[n];
    
    for(int i=0; i<n; i++) {
        color[i] = WHITE;
        d[i] = INFTY;
        p[i] = -1;
    }
    
    d[0] = 0;
    
    while(1) {
        int u;
        int mincost = INFTY;
        for (int i=0; i<n; i++) {
            if (color[i] != BLACK && d[i] < mincost) {
                mincost = d[i];
                u = i;
            }
        }
        
        if (mincost == INFTY) break;
        
        color[u] = BLACK;
        
        for (int v=0; v<=n; v++) {
            if (color[v] != BLACK && M[u][v] != INFTY) {
                if(M[u][v] < d[v]) {
                    d[v] = M[u][v];
                    p[v] = u;
                    color[v] = GRAY;
                }
            }
        }
    }
    
    int sum = 0;
    for (int i=0; i<n; i++) {
        if(p[i] != -1) sum += M[p[i]][i];
    }
    return sum;
}

int main() {
    scanf("%d", &n);
    for (int i=0; i<n; i++) {
        for (int j=0; j<n; j++) {
            int tmp;
            scanf("%d", &tmp);
            M[i][j] = (tmp == -1) ? INFTY : tmp;
        }
    }
    
    int sum = prim();
    printf("%d\n", sum);
    
    return 0;
}