Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

//util
var sc = bufio.NewScanner(os.Stdin)

func nextStr() string {
	sc.Scan()
	return sc.Text()
}

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func printList(list []int) {
	n := len(list)
	buf := bufio.NewWriter(os.Stdout)
	for _, v := range list[0 : n-1] {
		buf.Write([]byte(fmt.Sprintf("%d ", v)))
	}
	buf.Write([]byte(fmt.Sprintf("%d\n", list[n-1])))
	buf.Flush()
}

func max(a, b int) int {
	if a > b {
		return a
	} else {
		return b
	}
}

func min(a, b int) int {
	if a > b {
		return b
	} else {
		return a
	}
}

//solve
const (
	MAX   = 100
	INFTY = (1 << 21)
	WHITE = 0
	GRAY  = 1
	BLACK = 2
)

//V: グラフの頂点全体の集合
//T: MSTに属する頂点の集合
var (
	n     int
	M     [MAX][MAX]int //重みを記録する隣接行列
	color [MAX]int
	p     [MAX]int //MSTにおける頂点の親を記録
	d     [MAX]int //Tに属する頂点と、V−Tに属する頂点を繋ぐ辺の中で、重みが最小のものを記録
)

func prim() int {
	var u, minv int
	//初期化
	for i := 0; i < n; i++ {
		d[i] = INFTY
		p[i] = -1
		color[i] = WHITE
	}

	d[0] = 0

	for {
		//頂点uを決めていく
		//つまり、Tに属する頂点と、V−Tに属する頂点を繋ぐ辺の中で、重みが最小のものを決める
		minv = INFTY
		u = -1
		for i := 0; i < n; i++ {
			//未訪問かつ、辺の重みが最小となるuを選ぶ
			if minv > d[i] && color[i] != BLACK {
				u = i
				minv = d[i]
			}
		}

		if u == -1 {
			break
		}

		color[u] = BLACK
		for 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
				}
			}
		}
	}

	sum := 0
	for i := 0; i < n; i++ {
		if p[i] != -1 {
			sum += M[i][p[i]]
		}
	}
	return sum
}

func main() {
	sc.Split(bufio.ScanWords)
	n = nextInt()

	for i := 0; i < n; i++ {

		for j := 0; j < n; j++ {
			tmp := nextInt()
			if tmp == -1 {
				tmp = INFTY
			}
			M[i][j] = tmp
		}

	}

	fmt.Println(prim())

}

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

const int MAX = 100, INF = 1e9, white = 0, gray = 1, black = 2;
int G[MAX][MAX], color[MAX], d[MAX], p[MAX], n;

int prim(){
    int u, minv;
    for (int i = 0; i < n; i++)
    {
        d[i] = INF;
        p[i] = -1;
        color[i] = white;
    }
    d[0] = 0;
    while (true)
    {
        minv = INF;
        u = -1;
        for (int i = 0; i < n; i++)
        {
            if (color[i] != black && d[i] < minv)
            {
                minv = d[i];
                u = i;
            }
        }
        if (u == -1)
        {
            break;
        }
        color[u] = black;
        for (int i = 0; i < n; i++)
        {
            if (color[i] != black && G[u][i] != INF)
            {
                if (d[i] > G[u][i])
                {
                    d[i] = G[u][i];
                    p[i] = u;
                    color[i] = gray;
                }
                
            }
            
        }
        
    }
    int sum = 0;
    for (int i = 0; i < n; i++)
    {
        if (p[i] != -1){
            sum += G[i][p[i]];
        }
    }
    return sum;
}

int main(){
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            int v;
            cin >> v;
            G[i][j] = (v == -1) ? INF : v;
        }
    }
    cout << prim() << endl;
}

C++: #include <iostream>
#include <algorithm>
using namespace std;
int P[10000];
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()
{
  int N;
  cin >> N;
  int A[N][N];
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      int x;
      cin >> x;
      A[j][i] = x;
      // cout << "cost: " << x << " = " <<  j << "->"  << i << endl;
    }
  }
  // cout << "-----エッジ-------" << endl;
  int cost[10000], src[10000], dst[10000];
  int M = 0;
  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;
        // cout << "cost: " << cost[M] << " = " <<  src[M] << "->"  << dst[M] << endl;
        M += 1;
      }
    }
  }
  // cout << "M = " << M << " N = " << N << endl;
  init(N);
  pair<int, pair<int, int> > edges[M + 1];
  for (int m = 0; m < M; m++)
  {
    edges[m] = make_pair(cost[m], make_pair(src[m], dst[m]));
  }
  // cout << "-----sort前-------" << endl;
  // for (int i = 0; i < M; i++)
  //     cout << "cost: " << edges[i].first << " = " << edges[i].second.first << "->"  << edges[i].second.second << endl;
  sort(edges, edges+M);
  // cout << "-----sort後-------" << endl;
  // for (int i = 0; i < M; i++)
  //     cout << "cost: " << edges[i].first << " = " << edges[i].second.first << "->"  << edges[i].second.second << endl;
  int sum = 0;
  for (int i=0; i<M; i++) {
    if (is_same_set(edges[i].second.first, edges[i].second.second))
      continue;
    unite(edges[i].second.first, edges[i].second.second);
    sum += edges[i].first;
  }
  cout << sum << endl;
}