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<bits/stdc++.h>
using namespace std;
typedef	tuple<int,int,int>T;
typedef vector<T>VT;
int main(void){
	int n;
	int box[101][101];
	int i,j;
	int per[101];
	int ran[101];
	int x,y;
	int sum;
	VT v;
	cin>>n;
	for(i=1;i<=n;i++){
		for(j=1;j<=n;j++){
			cin>>box[i][j];
		}
	}
	for(i=1;i<=n;i++){
		for(j=1;j<=n;j++){
			if(box[i][j]!=-1){
				v.push_back(make_tuple(box[i][j],i,j));
			}
		}
	}
	sort(v.begin(),v.end());
	for(i=1;i<=n;i++){
		per[i]=i;
		ran[i]=1;
	}
	sum=0;
	for(i=0;i<v.size();i++){
		x=get<1>(v[i]);
		y=get<2>(v[i]);
		while(x!=per[x])	x=per[x];
		while(y!=per[y])	y=per[y];
		if(x!=y){
			sum+=get<0>(v[i]);
			if(ran[x]>ran[y])		per[y]=x;
			else if(ran[x]<ran[y])	per[x]=y;
			else{
				per[x]=y;
				ran[x]++;
			}
		}
	}
	cout<<sum<<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 = 0;
    for (int k = 0; k < n; ++k) {
        sum += d[k];
    }
    cout << sum << endl;


    return 0;
}

C++: #include"iostream"
#include"vector"
using namespace std;

int main()
{
  int n;
  vector<int>list;
  int w=0;
  cin>>n;
  vector<vector<int> > A(n,vector<int>(n));
  for(int i=0;i<n;i++)
    {
      for(int j=0;j<n;j++)
        {
          cin>>A[i][j];
        }
    }
  list.push_back(0);
  int mini=2001;
  while(list.size()<n)
    {
      mini=2001;
      int k,l;
      for(int i=0;i<list.size();i++)
        {
          for(int j=0;j<n;j++)
            {
              if(A[list[i]][j]>=0)
                {
                  if(A[list[i]][j]<mini)
                    {
                      int f=1;
                      for(int s=0;s<list.size();s++)
                        {
                          if(j==list[s])
                            {
                              f=0;
                              break;
                            }
                        }
                      if(f)
                        {
                          mini=A[list[i]][j];
                          k=j;
                          l=list[i];
                        }
                    }
                }
            }
        }
      list.push_back(k);
      w+=mini;
    }
  cout<<w<<endl;
  return 0;
}

C++: #include<stdio.h>
using namespace std;
const int sss=1<<21;
int n;
int a[1000][1000];
int d[1000],p[10000],color[1000];
int prim(){
	int u,minv;
	for(int i=0;i<n;i++){
		d[i]=sss;
		p[i]=-1;
	}
	d[0]=0;
	while(1){
		minv=sss;
		u=-1;
		for(int i=0;i<n;i++){
			if(minv>d[i]&&color[i]!=1){
				u=i;
				minv=d[i];
			}
		}
		if(u==-1) break;
		color[u]=1;
		for(int v=0;v<n;v++){
			if(color[v]!=1&&a[u][v]!=sss){
				if(d[v]>a[u][v]){
					d[v]=a[u][v];
					p[v]=u;
				}
			}
		}
	}
	int sum=0;
	for(int i=0;i<n;i++){
		if(p[i]!=-1) {
			sum+=a[i][p[i]];
		}
	}
}
int main(){
	scanf("%d",&n);
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			int e;scanf("%d",&e);
			a[i][j]=e==-1?sss:e;
		}
	}
	printf("%d\n",prim());
	return 0;
}

C++: #include<iostream>
#include<algorithm>
#define INF 99999
using namespace std;

int G[110][110] = {};

int prim(int n)
{
    int mini[110];
    int used[110];

    //init
    for(int i = 0;i < n;i++){
        mini[i] = INF;
        used[i] = 0;
    }
    mini[0] = 0;
    int ans = 0;

    while(true)
    {
        int v = -1;
        for(int i = 0;i < n;i++){
            if(!used[i] && (v == -1 || mini[i] < mini[v])) v = i;
        }
        if(v == -1) break;
        //ans = -1;

        used[v] = 1;
        ans += mini[v];
        //cout << ans << endl;

        for(int i = 0;i < n;i++){
            mini[i] = min(mini[i], G[v][i]);
        }
    }
    return ans;
}

int main()
{
    int n;
    cin >> n;

    for(int i = 0;i < n;i++){
        for(int j = 0;j < n;j++){
            int cost;
            cin >> cost;
            if(cost == -1) G[i][j] = INF;
            else G[i][j] = cost;
        }
    }

    cout << prim(n) << endl;
    return 0;
}

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


int P[10010]; // 0 から 10000 までの頂点を取り扱い可能
void init(int N) { // 初期化 はじめは全ての頂点はバラバラ
  for (int i=0; i<N; i++) P[i] = i;
}
int root (int a) { //aの大表現を求める
  if (P[a] == a) return a;
  return (P[a] = root(P[a]));
}
bool is_same_set (int a, int b){
  return root(a) == root(b);
}
void unite(int a, int b){
  P[root(a)] = root(b);
}


int main(){
  int n;
  cin >> n;
  tuple<int,int,int> cost[10010]; //Array for cost
  int count = 0;
  for (int i = 0; i < n; i ++){
    for (int j = 0; j < n; j++){
      int t;
      cin >> t;
      if (t >= 0){
      get<1>(cost[count]) = i;
      get<2>(cost[count]) = j;
      get<0>(cost[count]) = t;
      count++;
    }
    }
  }
  sort(cost,cost + n*n);//sortする

  init(n + 10);

  int ans = 0;
  int anscount = 0;//このcountがnになった時点で終了する
  int k = 0;
  while (anscount != n-1){
    if (is_same_set(get<1>(cost[k]), get<2>(cost[k]))) {
      k ++;
    }
    else{
      unite(get<1>(cost[k]), get<2>(cost[k]));
      ans += get<0>(cost[k]);
      k++;
      anscount++;
    }
  }


  cout << ans << endl;
  return 0;
}