Go: package main

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

const (
	white int = iota
	gray
	black
)
const INFTY int = 2001

var G [][]int
var color []int
var d []int
var p []int
var N int

func prim() int {
	u := 0
	d[u] = 0
	for {
		minCost := INFTY
		for i:=0; i<N; i++ {
			if color[i] != black && d[i] < minCost {
				minCost = d[i]
				u = i
			}
		}

		if minCost == INFTY {
			break
		}

		color[u] = black

		for v:=0; v<N; v++ {
			if color[v] != black && G[u][v] != -1 {
				if G[u][v] < d[v] {
					d[v] = G[u][v]
					p[v] = u
					color[v] = gray
				}
			}
		}
	}
	// sum
	sum := 0
	for i:=0; i<N; i++ {
		if p[i] != -1 {
			sum += G[i][p[i]]
		}
	}
	return sum
}




func initVariable(N int) {
	G = make([][]int, N)
	for i:=0; i<N; i++ {
		G[i] = make([]int, N)
	}
	color = make([]int, N)
	d = make([]int, N)
	p = make([]int, N)
	for i:=0; i<N; i++ {
		d[i] = INFTY
		p[i] = -1
	}
}

func nextInt(scanner *bufio.Scanner) int {
	scanner.Scan()
	num, _ := strconv.Atoi(scanner.Text())
	return num
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	N = nextInt(scanner)
	initVariable(N)
	for i:=0; i<N; i++ {
		for j:=0; j<N; j++ {
			G[i][j] = nextInt(scanner)
		}
	}
	fmt.Printf("%d\n", prim())
}

C++: #include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>

#define INF 2001
#define MAX 101

using namespace std;

int MAT[MAX][MAX];
int N;

int prim(){
    int sum;
    bool complete[MAX];
    int dist[MAX];
    int parent[MAX];
    
    for (int i = 0; i < N; i++) {
        complete[i] = false;
        dist[i] = INF;
        parent[i] = -1;
    }
    
    dist[0] = 0;
    
    while (1) {
        int u = -1;
        int min = INF;
        
        for (int i = 0; i < N; i++) {
            if (min > dist[i] && !complete[i]) {
                u = i;
                min = dist[i];
            }
        }
        
        if (u == -1) break;
        
        complete[u] = true;
        
        for (int v = 0; v < N; v++) {
            if (!complete[v] && MAT[u][v] != INF) {
                if (dist[v] > MAT[u][v]) {
                    dist[v] = MAT[u][v];
                    parent[v] = u;
                }
            }
        }
    }
    
    sum = 0;
    
    for (int i = 1; i < N; i++) {
        sum += MAT[parent[i]][i];
    }
    
    return sum;
}

int main(){
    cin >> N;
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            scanf("%d",&MAT[i][j]);
            if (MAT[i][j] == -1) MAT[i][j] = INF;
        }
    }
    
    cout << prim() << endl;
    
    return 0;
}

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

#if 0
所用時間:

感想:

#endif

int N, buff, M = 0, SUM = 0, A[101][101], cost[5000], dpt[5000], dst[5000], P[100001]; //A[Y][X]
vector<pair<int, int> > edges;

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); //aのrootをbの子にする
}

void preprocess(){//辺の前処理
  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];
        dpt[M] = r;
        dst[M] = c;
        M++;
      }
    }
  }

  //for(int i = 0; i < M; ++i) cout << cost[i] << endl;

  for(int i = 0; i < M; ++i){
    edges.push_back(make_pair(cost[i], i));
  }
  // for(int i = 0; i < M; ++i) cout << edges[i].first << " " << edges[i].second << endl;

  sort(edges.begin(), edges.end());

  // for(int i = 0; i < M; ++i) cout << edges[i].first << " " << edges[i].second << endl;

}

void solve(){
  int ans = 0;
  preprocess();

  init(N);

  for(int i = 0; i < M; ++i){
    int index = edges[i].second;
    if(is_same_set(dpt[index], dst[index])) continue;
    else {
      unite(dpt[index], dst[index]);
      ans += cost[index];
    }
  }
  cout << ans << endl;
}

int main(){

cin >> N;

for(int i = 0; i < N; ++i){
  for(int j = 0; j < N; ++j){
    cin >> buff;
    A[i][j] = buff;
    }
  }

  //行列の表示
  // cout << "Matrix:" << endl;
  // for(int row=0; row < N; ++row){
  //   for(int col = 0; col < N; ++col){
  //     cout << A[row][col];
  //   }
  //   cout << endl;
  // }

  solve();
}

C++: #include "bits/stdc++.h"

using namespace std;

using ll = long long;
using ld = long double;
using P = pair<string, int>;
constexpr ld EPS = 1e-12;
constexpr int INF = numeric_limits<int>::max() / 2;
constexpr int MOD = 1e9 + 7;

template <typename T>
void printv(const vector<T> &v)
{
    int sz = v.size();
    for (int i = 0; i < sz; i++)
    {
        cout << v[i] << " \n"[i == sz - 1];
    }
}

struct UnionFind
{
    vector<int> data;
    UnionFind(int size) : data(size, -1) {}
    bool unite(int x, int y)
    {
        x = root(x);
        y = root(y);
        if (x != y)
        {
            if (data[y] < data[x])
                swap(x, y);
            data[x] += data[y];
            data[y] = x;
        }
        return x != y;
    }
    bool find(int x, int y)
    {
        return root(x) == root(y);
    }
    int root(int x)
    {
        return data[x] < 0 ? x : data[x] = root(data[x]);
    }
    int size(int x)
    {
        return -data[root(x)];
    }
};

struct Edge
{
    int from, to, cost;
    Edge(int from, int to, int cost) : from(from), to(to), cost(cost) {}
};

bool operator<(const Edge &e, const Edge &f)
{
    return e.cost > f.cost;
}

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);

    int n;
    cin >> n;
    priority_queue<Edge> es;
    int res = 0;
    UnionFind uf(n);
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            int a;
            cin >> a;
            if (a == -1)
                continue;
            if (j <= i)
                continue;
            es.push(Edge{i, j, a});
        }
    }
    while (!es.empty())
    {
        Edge e = es.top();
        es.pop();
        if (uf.find(e.from, e.to))
            continue;
        res += e.cost;
        uf.unite(e.from, e.to);
    }
    cout << res << endl;
}

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;
}