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<algorithm>
#include<stack>
#include<queue>
using namespace std;
int main(){
	int n, a, sum = 0;
	int temp = 1000000;
	int n1, n2;
	int r1[101] = {0}, r2[101] = { 0 };
	int m[101][101] = { 0 };
	cin >> n;
	for (int i = 0; i < n; i++){
		for (int l = 0; l < n; l++){
			cin >> a;
			m[i][l] = a;
		}
	}
	for (int i = 0; i < n-1; i++){
		for (int l = 0; l < n; l++){
			cout << m[i][l] << "   " << i <<endl;

			if (m[i][l] == -1) continue;
			if (m[i][l] < temp&&r1[l] == 0 && r2[i] == 0){
				temp = m[i][l];
				n1 = i; n2 = l;
			}
		}
		if (temp != 1000000){
			sum += temp;
			cout << i + 1 << endl;
			cout << temp <<endl;
			r1[n1] = -1; r2[n2] = -1;
			cout << n1  << n2 << endl;
			n1 = 0; n2 = 0;
			temp = 1000000;
		}
	}
	cout << sum << endl;
	return 0;
}*/
/*
5
-1 2 3 1 -1
2 -1 -1 4 -1
3 -1 -1 1 1
1 4 1 -1 3
-1 -1 1 3 -1
*/
#include<queue>
#include<stack>
#include<vector>
#include<iostream>
#include<algorithm>
using namespace std;
#define WHITE 0
#define BLACK 1
#define GRAY 2
#define INFTY 1000000
int color[101];
int m[101][101];
int d[101];
int p[101];
int mincost = INFTY;
int n;
int yy;


void prim(int u){
	while (1){
		mincost = INFTY;
		for (int i = 0; i < n - 1; 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 - 1; 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 main(){
	int a[10] = { 0 };
	for (int i = 0; i < 101; i++){
		color[i] = INFTY; d[i] = INFTY; p[i] = -1;
	}
	for (int i = 0; i < 101; i++){
		for (int l = 0; l < 101; l++){
			m[i][l] = 0;
		}
	}
	cin >> n;
	for (int i = 0; i < n; i++){
		for (int l = 0; l < n; l++){
			cin >> m[i][l];
		}
	}
	prim(0);
	int sum = 0;
	for (int i = 0; i < n; i++){
		sum += p[i];
	}
	cout << sum*(-1) << endl;
	return 0;
}

C++: #include<stdio.h>

#define INFTY (1<<21)
#define MAX 100
#define WHITE 0
#define GRAY 1
#define 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(){
  
  scanf("%d",&n);

  for(int i = 0; i < n; i++){
    for(int j = 0; j < n; j++){
      int e;
      scanf("%d",&e);
      M[i][j] = (e == -1) ? INFTY : e;
    }
  }

  printf("%d\n",prim());

  return 0;
}

C++: #include <tuple>
#include <iostream>
#include <algorithm>
using namespace std;
int P[10010]; 
tuple<int,int,int> a[10010];
void init(int N) { 
    for (int i=0; i<N; ++i) P[i] = i;
}
int root(int 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(void){
    int n,value;
    cin >> n;
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            cin >> value;
            a[n*i+j] = make_tuple(value,i,j);
        }
    }
    init(n*n);
    sort(a,a+n*n);
    int sum =0;
    int x,y;
    for(int i =0;i<n*n;i++){
        x = get<1>(a[i]);
        y = get<2>(a[i]);
        if(is_same_set(x,y)!=1){
            if(get<0>(a[i])!=-1){
                sum += get<0>(a[i]);
                unite(x,y);
            }
        }
    }
    cout << sum << endl;
}

C++: #include<iostream>
#include<vector>
#include<algorithm>

int color(std::vector<std::vector<int> > v) {
	std::vector<int> color(v.size(), 0);
	int count = 0, color_count = 1;
	for (int i = 0; i <= 2000; i++) {
		for (size_t j = 0; j < v.size(); j++) {
			for (size_t k = j + 1; k < v.size(); k++) {
				if (v[j][k] == i && ((color[k] == 0 || color[j] == 0) || (color[k] != color[j]))) {
					if (color[k] == 0) {
						if (color[j] != 0) {
							color[k] = color[j];
						} else {
							color[k] = color[j] = color_count;
							color_count++;
						}
					} else {
						if (color[j] == 0) {
							color[j] = color[k];
						} else {
							int num = color[j];
							std::replace(color.begin(), color.end(), num, color[k]);
						}
					}
					count += i;
				}
			}
		}
	}
	return count;
}

int main() {
	int num;
	std::cin >> num;

	std::vector<std::vector<int> > v(num);

	for (int i = 0; i < num; i++) {
		for (int j = 0; j < num; j++) {
			int n;
			std::cin >> n;
			v[i].push_back(n);
		}
	}

	std::cout << color(v) << std::endl;

	return 0;
}