Go: package main

import (
	"bufio"
	"container/heap"
	"fmt"
	"os"
	"strconv"
)

type Node struct {
	id     int
	weight int
}

func init() {
	sc.Split(bufio.ScanWords)
}

type NodeHeap []Node

func (h NodeHeap) Len() int           { return len(h) }
func (h NodeHeap) Less(i, j int) bool { return h[i].weight < h[j].weight }
func (h NodeHeap) Swap(i, j int)      { h[i], h[j] = h[j], h[i] }

func (h *NodeHeap) Push(x interface{}) {
	*h = append(*h, x.(Node))
}

func (h *NodeHeap) Pop() interface{} {
	old := *h
	n := len(old)
	x := old[n-1]
	*h = old[0 : n-1]
	return x
}

var sc = bufio.NewScanner(os.Stdin)

func scanInt() int {
	num, _ := strconv.Atoi(scanString())
	return num
}

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

func init() {
	sc.Split(bufio.ScanWords)
}

func main() {
	n := scanInt()

	list := make([][]int, n)
	seen := make([]bool, n)
	for i := range list {
		list[i] = make([]int, n)
		for j := range list[i] {
			list[i][j] = scanInt()
		}
	}

	start := Node{id: 0, weight: 0}
	h := &NodeHeap{start}
	heap.Init(h)

	sum := 0
	for h.Len() > 0 {
		node := heap.Pop(h).(Node)
		if seen[node.id] {
			continue
		}
		seen[node.id] = true
		sum += node.weight

		for i, w := range list[node.id] {
			if w < 0 {
				continue
			}
			heap.Push(h, Node{id: i, weight: w})
		}
	}

	fmt.Println(sum)
}

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

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

int main(void)
{
	int n, now = 0, flag[101] = {};
	long ans = 0, an[100][100], min[100];

	cin >> n;

	for(int i = 0; i < n; i++) {
		for(int j = 0; j < n; j++)
			cin >> an[i][j];
		min[i] = LONG_MAX;
	}

	do {
		long key = LONG_MAX, key_i;
		flag[now] = 1;
		flag[100] = 0;

		for(int i = 0; i < n; i++) {
			if(an[now][i] != -1 && an[now][i] < min[i] && flag[i] == 0)
				min[i] = an[now][i];
			if(flag[i] == 0 && min[i] < key) {
				key = min[i];
				key_i = i;
			}
			if(flag[i] == 1)
				flag[100]++;
		}

		now = key_i;
		ans += key;

	}while(flag[100] != n - 1);

	cout << ans << endl;

	return 0;
}