Go: package main

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

var scanner = bufio.NewScanner(os.Stdin)

func nextString() string {
	scanner.Scan()
	return scanner.Text()
}

func nextInt() int {
	n, err := strconv.Atoi(nextString())
	if err != nil {
		fmt.Printf("strconv.Atoi failed: %v\n", err)
	}
	return n
}

type ElementKey int
type Node struct {
	key    ElementKey
	parent *Node
	rank   int
}

func newNode(key ElementKey) *Node {
	node := Node{}
	node.key = key
	node.parent = &node
	node.rank = 0

	return &node
}

func union(x, y *Node) {
	xSet := findSet(x)
	ySet := findSet(y)
	link(xSet, ySet)
}

func link(x, y *Node) {
	if x.rank > y.rank {
		y.parent = x
	} else {
		x.parent = y

		if x.rank == y.rank {
			y.rank++
		}
	}
}

func findSet(x *Node) *Node {
	if x != x.parent {
		x.parent = findSet(x.parent)
	}
	return x.parent
}

func newDisjointSets(nSets int) *DisjointSets {
	sets := DisjointSets{}
	sets.keyToNode = map[ElementKey]*Node{}
	for i := 0; i < nSets; i++ {
		key := ElementKey(i)
		sets.keyToNode[key] = newNode(key)
	}
	return &sets
}

type DisjointSets struct {
	keyToNode map[ElementKey]*Node
}

func (sets *DisjointSets) union(x, y ElementKey) {
	xNode := sets.keyToNode[x]
	yNode := sets.keyToNode[y]
	union(xNode, yNode)
}

func (sets DisjointSets) findSet(x ElementKey) *Node {
	xNode := sets.keyToNode[x]
	return findSet(xNode)
}

type Edge struct {
	i, j   int
	weight int
}

func main() {
	scanner.Split(bufio.ScanWords)

	n := nextInt()
	sets := newDisjointSets(n)
	edges := []Edge{}
	a := make([][]int, n)
	for i := 0; i < n; i++ {
		a[i] = make([]int, n)
		for j := 0; j < n; j++ {
			a[i][j] = nextInt()
			if a[i][j] >= 0 {
				e := Edge{
					i:      i,
					j:      j,
					weight: a[i][j],
				}
				edges = append(edges, e)
			}
		}
	}
	sort.Slice(edges, func(i, j int) bool { return edges[i].weight < edges[j].weight })
	totalWeight := 0
	for _, edge := range edges {
		i := ElementKey(edge.i)
		j := ElementKey(edge.j)
		if sets.findSet(i) != sets.findSet(j) {
			totalWeight += edge.weight
			sets.union(i, j)
		}
	}
	fmt.Println(totalWeight)
}

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, G[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;
}

C++: #include <bits/stdc++.h>
using namespace std;

#define N_MAX 100

int M[N_MAX][N_MAX];

int main(){

  int n;
  scanf("%d", &n);

  for(int i=0; i<n*n; i++){
    scanf("%d", &M[i/n][i%n]);
  }

  /*
   * Find Mininum Spanning Tree using
   * Prim's Algorithm
  */
  vector<bool> is_belong_to_mst(n, false);
  is_belong_to_mst.at(0) = true;
  int total_cost = 0;
  for(int i=1; i<n; i++){
    // get t and adj
    vector<int> t;
    vector<int> adj;
    for(int i=0; i<n; i++){
      if(is_belong_to_mst.at(i)) t.push_back(i);
      else adj.push_back(i);
    }

    int min_cost = INT_MAX;
    int next_t = 0;
    for(int i=0; i<t.size(); i++){
      for(int j=0; j<adj.size(); j++){
        if(M[t.at(i)][adj.at(j)] != -1 && M[t.at(i)][adj.at(j)] < min_cost){
          min_cost = M[t.at(i)][adj.at(j)];
          next_t = adj.at(j);
        }
      }
    }

    is_belong_to_mst.at(next_t) = true;
    total_cost += min_cost;
  }

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

  return 0;
}

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

static const int MAX_NUM  = 101;
static const int NO_VISIT  = 0;
static const int NOW_VISIT = 1;
static const int END_VISIT = 2;

void MPT(int *pElem, int num)
{
    int i;
    int mincost;
    int target;
    int parent[MAX_NUM];
    int cost[MAX_NUM];
    int state[MAX_NUM] = {0};
    int *tmp;
    int total = 0;

    for (i = 0; i < num; i++) {
        parent[i] = -1;
        cost[i]   = 0x7FFFFFFF;
    }

    cost[0] = 0;

    for (;;) {
        mincost = 0x7FFFFFFF;
        for (i = 0; i < num; i++) {
            if ((state[i] != END_VISIT) && (mincost > cost[i])) {
                target  = i;
                mincost = cost[i];
            }
        }
        if (mincost == 0x7FFFFFFF) {
            break;
        }

        tmp = pElem + target * MAX_NUM;
        for (i = 0; i < num; i++) {
            if (*(tmp + i) < cost[i]) {
                cost[i]   = *(tmp + i);
                parent[i] = target;
                state[i]  = NOW_VISIT;
            }
        }
        state[target] = END_VISIT;
        total += cost[target];
    }

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

    return;
}

int main(void)
{
    int i, j;
    int num;
    int tmp;
    int elem[MAX_NUM][MAX_NUM];

    scanf("%d", &num);
    for (i = 0; i < num; i++) {
        for (j = 0; j < num; j++) {
            scanf("%d", &tmp);
            elem[i][j] = (tmp != -1) ? tmp : 0x7FFFFFFF;
        }
    }

    MPT(&elem[0][0], num);

    return 0;
}

C++: #include<iostream>
using namespace std;
static const int INF =1000000;
static const int WHITE =0;
static const int GRAY =1;
static const int BLACK =2;
static const int N =100;

int n,A[N][N];

int d[N],p[N],color[N];

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

   int sum =0;

   while(1){
     int mincost = INF;
     for (int i =0;i<n;i++){
       if(color[i]!=BLACK && d[i] < mincost){
         mincost = d[i];
         u =i;
       }
     }
     if(mincost == INF) break;
     sum += mincost;
     color[u] = BLACK;
     for(int j =0;j<n;j++){
       if(color[j] != BLACK && A[u][j] != INF){
         if(A[u][j] < d[j]){
           d[j] =A[u][j];
           p[j] =u;
           color[j] = GRAY;
         }
       }
     }
   }
   return sum;
}

int main(){
  cin >> n;

  for(int i=0;i<n;i++){
    for(int j =0;j<n;j++){
      cin >> A[i][j];
      if(A[i][j]==-1){
        A[i][j] = INF;
      }
    }
  }
  cout << prim() << endl;

  return 0;
}

C++: // J4180449 三宅亮輔
// 問題名
// - Disjoint Set
// 問題URL
// - https://onlinejudge.u-aizu.ac.jp/problems/DSL_1_A
// 所要時間
// - 30分
// 学んだこと
//  Union Find木の実装方法
//  蟻本を読んでroot関数にparの値を追加することが有効であると理解した。
//  アッカーマン関数の逆関数のオーダーになることがわかった。

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> P;

vector<P> v;
int n, q;

int par[100];

int root(int a) {
    if(a == par[a])
        return a;
    return par[a] = root(par[a]);
}

bool same(int a, int b) { return root(a) == root(b); }

void unite(int a, int b) {
    a = root(a);
    b = root(b);
    par[a] = b;
}

int main() {
    int ans = 0;
    cin >> n;
    for(int i = 0; i < n; i++)
        par[i] = i;

    for(int i = 0; i < n * n; i++) {
        int a;
        cin >> a;
        v.push_back(P(a, i));
    }
    sort(v.begin(), v.end());

    for(int i = 0; i < n * n; i++) {
        if(v[i].first != -1) {
            int x = v[i].second % n;
            int y = v[i].second / n;
            if(!same(x, y)) {
                unite(x, y);
                ans += v[i].first;
            }
        }
    }

    cout << ans << endl;
}