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 WHI=0;
static const int GRA=1;
static const int BLA=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]=WHI;
  }
  d[0]=0;
  while(114514){
    minv=INF;
    u=-1;
    for(int i=0;i<n;++i){
      if(minv>d[i] && color[i]!=BLA){
	u=i;
	minv=d[i];
      }
    }
    if(u==-1) break;
    color[u]=BLA;
    for(int v=0;v<n;++v){
      if(color[v]!=BLA && m[u][v]!=INF){
	if(d[v]>m[u][v]){
	  d[v]=m[u][v];
	  p[v]=u;
	  color[v]=GRA;
	}
      }
    }
  }
  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 WHITE = 0;
 static const int GRAY = 1;
 static const int BLACK =2;
 static const int MAX = 100;
 static const int INFTY = (1<<21);
 
 int n,M[MAX][MAX];
 
 int prim(){
 	int u,miv;
 	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){
 		u=-1;
 		miv = INFTY;
 		for(int j=0;j<n;j++){
 			if(color[j]!=BLACK&&d[j]<miv){
 				miv = d[j];
 				u=j;
 			}
 		}
 		if(u==-1) break;
 		
 		color[u] = BLACK;
 		
 		for(int i=0;i<n;i++){
 			if(color[i]!=BLACK&&M[u][i]!=INFTY){
 				if(d[i]>M[u][i]){
 					d[i] = M[u][i];
 					p[i] = u;
 					color[i] = 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 <set>
using namespace std;
    int d[101];
    int G[101][101];
void krusta(int n){
    int color[101],l,m;
    for(int i=1;i<=n;i++){
        d[i]=1000000;
        color[i]=0;
    }
    d[1]=0;
    while(1){
        m=1000000;
        for(int i=1;i<=n;i++){
            if(color[i]!=1 && d[i]<m){
                m=d[i];
                l=i;
            }
        }
        color[l]=1;
        if(m==1000000){
            break;
        }
        for(int i=1;i<=n;i++){
            if(color[i]!=1 && G[l][i]!=-1 && G[l][i]<d[i]){
                d[i]=G[l][i];
            }
        }
    }
}
int main(){
    int n,m;
    cin>>n;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            cin>>m;
            G[i][j]=m;
        }
    }
    krusta(n);
    int sum=0;
    for(int i=1;i<=n;i++){
        sum=sum+d[i];
    }
    cout<<sum<<endl;
    return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<math.h>
#include<string>
#include<string.h>
#include<stack>
#include<queue>
#include<vector>
#include<utility>
#include<set>
#include<map>
#include<stdlib.h>
#include<iomanip>

using namespace std;

#define ll long long
#define ld long double
#define EPS 0.0000000001
#define INF 1e9
#define rep(i,n) for(i=0;i<n;i++)
#define loop(i,a,n) for(i=a;i<n;i++)
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)

typedef vector<int> vi;
typedef pair<int,int> pii;


struct edge{ int u, v, cost; };

bool comp(const edge& e1, const edge& e2){

  return e1.cost < e2.cost;

}

#define MAX_N 100000
#define MAX_E 100000

int par[MAX_N];

int rank[MAX_N];

void init(int n){

  for(int i = 0 ; i < n ; i++){

    par[i] = i;

    rank[i] = 0;

  }

}

int find(int x){

  if(par[x] == x) return x;

  else return par[x] = find(par[x]);

}

void unite(int x, int y){

  x = find(x);

  y = find(y);

  if(x == y) return;

  if(rank[x] == rank[y]) par[x] = y;

  else par[y] = x;

  if(rank[x] == rank[y]) rank[x]++;
 
}

bool same(int x, int y){

  return find(x) == find(y);

}

edge es[MAX_E];

int V, E;

int kruskal(){

  sort(es, es+E, comp);

  init(V);

  int res = 0;

  for(int i = 0 ; i < E ; i++){

    edge e = es[i];

    if(!same(e.u, e.v)){

      unite(e.u, e.v);

      res += e.cost;

    }

  }

  return res;

}

int main(){
  int n;
  cin>>n;
  V=n;
  int i,j,k;
  E=0;
  rep(i,n)rep(j,n){
    int t;
    cin>>t;
    if(t<0)continue;
    es[E].u=i;
    es[E].v=j;
    es[E].cost=t;
    E++;
  }
  cout<<kruskal()<<endl;
}

C++: #include<iostream>
#include<utility>
#include<algorithm>
using namespace std;
int p[101];
void makeset(int N){
    for(int i=0;i<N;++i){
        p[i]=i;
    }
}
int root(int x){
    if(p[x]==x) return x;
    return (p[x]==root(p[x]));
}
void unite(int x,int y){
    p[root(x)]=root(y);
}
bool same(int x,int y){
    return root(x)==root(y);
}
int main(){
    int N,M=0;
    int A[101][101],cost[101],src[101],dst[101];
    cin>>N;
    makeset(N);
    for(int r=0;r<N;++r){
        for(int c=0;c<N;++c){
            cin>>A[r][c];
        }
        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+=1;
            }
        }
    }

    
    pair<int,int>edges[101];
    for(int i=0;i<M;++i){
        edges[i]=make_pair(src[i],dst[i]);
    }
    sort(edges,edges+M);
    sort(cost,cost+M);
    int total=0,num=0,flag=0;
    for(int i=0;i<M;++i){
        unite(edges[i].first,edges[i].second);
        total+=cost[i];
        num++;
        if(num==N-1){
            flag=1;
            break;
        }
    }
    if(flag){
        cout<<total<<endl;
    }
}