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>
using namespace std;
static const int MAX = 100;
static const int INF = (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 u,minv;
	int d[MAX],p[MAX],color[MAX];
	
	for(int i=0;i<n;i++){
		d[i] = INF;
		p[i] = -1;
		color[i] = WHITE;
	}
	
	d[0] = 0;
	
	while(1){
		minv = INF;
		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 && M[u][v] != INF){
				if(d[v]>M[u][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[i][p[i]];
	}
	
	return sum;
}

int main()
{
	cin >> n;
	
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			int e;
			cin >> e;
			M[i][j] = (e==-1)?INF:e;
		}
	}
	
	cout << prim() <<endl;
	
	
	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;

int n,M[MAX][MAX];

int prim() {
  int u, minv;
  int d[MAX], p[MAX], color[MAX];

  for( int i = 0; i <n; i++){
    d[i] = INFTY;
    p[i] = -1;
    color[i] = WHITE;
  }

  d[0] = 0;

  while(1) {
  minv = INFTY;
  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 && M[u][v] != INFTY){
      if(d[v] > M[u][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[i][p[i]];
  }

  return sum;
}

int main(){
  cin >> n;

  for( int i = 0; i < n; i++){
    for(int j = 0; j <n;j++){
      int e; cin >>e;
      M[i][j] = (e == -1) ? INFTY : e;
    }
  }

  cout << prim() << endl;

  return 0;
}

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

int n;
int g[101][101];

int d[101];
char c[101];
int p[101];

void prim() {
    for (int i = 0; i < n; ++i) {
        d[i] = INT_MAX;
        c[i] = 'w';
    }
    c[0] = 'b';
    d[0] = 0;
    while (true) {
        int u, v;
        int mine = INT_MAX;
        for (int j = 0; j < n; ++j) {
            if (c[j] == 'b') {
                u = j;
                for (int k = 0; k < n; ++k) {
                    if(g[u][k] > -1 && g[u][k] <= d[k] && c[k] != 'b' && g[u][k] <= mine) {
                        d[k] = g[u][k];
                        mine = d[k];
                        v = k;
                    }
                }
            }
        }
        p[v] = u;
        c[v] = 'b';
        if(mine == INT_MAX) break;
    }
}

int main() {
    cin >> n;
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            int w;
            cin >> w;
            g[i][j] = w;
        }
    }
    prim();
    int sum;
    for (int k = 0; k < n; ++k) {
        sum += d[k];
    }
    cout << sum << endl;


    return 0;
}

C++: #include <stdio.h>
#include <vector>
#include <queue>
using namespace std;
struct side{
    int n;
    int m;
    int l;
};
side sides[100000];
void mergesort(int l,int r){
    if(l==r)return;
    side temp;
    if(l==r-1){
        if(sides[l].l>sides[r].l){
            temp=sides[l];
            sides[l]=sides[r];
            sides[r]=temp;
        }
        return;
    }
    int mid=(l+r)/2,now=0;
    side b[r-l+1];
    mergesort(l,mid-1);
    mergesort(mid,r);
    for(int i=0;i<=r-l;i++){
        if(sides[i+l-now].l<sides[mid+now].l){
            b[i]=sides[i+l-now];
            if(i+l-now==mid-1){
                for(i++;i<=r-l;i++){
                    b[i]=sides[mid+now];
                    now++;
                }
            }
        }
        else{
            b[i]=sides[mid+now];
            now++;
            if(mid+now>r){
                for(i++;i<=r-l;i++)
                    b[i]=sides[i+l-now];
            }
        }
    }
    for(int i=0;i<=r-l;i++)
        sides[i+l]=b[i];
}
int main(){
    int n,a,ans=0,c=1,now=0;
    bool tree[10000];
    scanf("%d",&n);
    tree[0]=true;
    for(int i=1;i<n;i++){
        tree[i]=false;
    }
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            scanf("%d",&sides[now].l);
            sides[now].n=i;
            sides[now].m=j;
            if(sides[now].l==-1)now--;
            now++;
        }
    }
    mergesort(0,now-1);
    while(c<n){
        a=0;
        while(tree[sides[a].n]==tree[sides[a].m])a++;
        ans+=sides[a].l;
        tree[sides[a].n]=tree[sides[a].m]=true;
        c++;
    }
    printf("%d\n",ans);
}

C++: #include<bits/stdc++.h>
using namespace std;

const int M = 1000000007;

vector<vector<int>> adj;


struct Point {
    int id;
    // double x, y;
    Point() {}
    Point(int id) : id(id) {}
    // Point(double x, double y): x(x), y(y) {}
    int distance(const Point &p) const {
        return adj[id][p.id] == -1 ? M : adj[id][p.id];
    }
};

int calc_mst(const vector<Point>& points) {
    int n = points.size();
    vector<int> dist(n, M);
    vector<bool> visited(n);
    int p = 0;
    int res = 0;
    while (true) {
        visited[p] = true;
        int mind = M, minj = -1;
        for (int j = 0; j < n; ++j) {
            if (visited[j]) continue;
            dist[j] = min(dist[j], points[p].distance(points[j]));
            if (mind > dist[j]) {
                mind = dist[j];
                minj = j;
            }
        }
        if (minj == -1) break;
        p = minj;
        res += mind;
    }
    return res;
}

int main(int argc, char **argv) {
    int n;
    cin >> n;
    adj = vector<vector<int>>(n, vector<int>(n));
    vector<Point> p(n);
    for (int i = 0; i < n; ++i) {
        p[i] = Point(i);
        for (int j = 0; j < n; ++j) {
            cin >> adj[i][j];
        }
    }
    cout << calc_mst(p) << '\n';
    return 0;
}