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 INF = 1 << 20;
const int MAX_N = 110;

int main() {
  int cost[MAX_N][MAX_N];
  int mincost[MAX_N];
  bool used[MAX_N];
  int n, a, res;

  cin >> n;

  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      cin >> a;
      cost[i][j] = a == -1 ? INF : a;
    }
  }

  for (int i = 0; i < n; ++i) {
    mincost[i] = INF;
    used[i] = false;
  }

  mincost[0] = 0;
  res = 0;

  while (true) {
    int v = -1;
    for (int u = 0; u < n; ++u) {
      if (!used[u] && (v == -1 || mincost[u] < mincost[v])) v = u;
    }

    if (v == -1) break;

    used[v] = true;
    res += mincost[v];

    for (int u = 0; u < n; ++u) {
      mincost[u] = min(mincost[u], cost[v][u]);
    }
  }

  cout << res << endl;
  
  return 0;
}

C++: #include <iostream>
#include <vector>
#include <numeric>
#include <limits>

using namespace std;

const int N = 100;

enum {
    WHITE,
    GRAY,
    BLACK,
};

int main() {
    int A[N][N];

    int n;
    cin >> n;
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            int a;
            cin >> a;
            A[i][j] = a == -1 ? numeric_limits<int>::max() : a;
        }
    }

    // ????????????
    vector<int> color(n, WHITE); // ??????????¨??????¶???
    vector<int> d(n, numeric_limits<int>::max());
    vector<int> p(n, -1);

    d[0] = 0;
    while (1) {
        int minv = numeric_limits<int>::max();
        int 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 && A[u][v] != numeric_limits<int>::max()) {
                if (d[v] > A[u][v]) {
                    d[v] = A[u][v];
                    p[v] = u;
                    color[v] = GRAY;
                }
            }
        }
    }
    int sum;
    for (int i = 0; i < n; ++i) {
        if (p[i] != -1)
            sum += A[i][p[i]];
    }
    cout << sum << endl;

    return 0;
}

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

const long MAX = 100;
const long INFTY = (1<<21);
const long WHITE = 0;
const long GRAY = 1;
const long BLACK = 2;

long n, M[MAX][MAX];

long prim(){
	long u, minv;
	long d[MAX], p[MAX], color[MAX];

	// d: minimum dist. p: parent

	for (long i = 0; i < n; i++){
		d[i] = INFTY;
		p[i] = -1;
		color[i] = WHITE;
	}

	d[0] = 0;

	while (1){
		minv = INFTY;
		// u: node which has minv
		u = -1;
		for (long i = 0; i < n; i++){
			if (minv > d[i] && color[i] != BLACK){
				u = i;
				minv = d[i];
			}
		}
		// cut all nodes
		if (u == -1) break;
		color[u] = BLACK; // already selected
		for (long 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 (long i = 0; i < n; i++){
		if (p[i] != -1) sum += M[i][p[i]];
	}

	return sum;
}

int main(){
	cin >> n;

	for (long i = 0; i < n; i++){
		for (long j = 0; j < n; j++){
			long e;
			cin >> e;
			//M[i][j] = (e == -1) ? INFTY : e;
			if (e == -1) M[i][j] = INFTY;
			else M[i][j] = e;
		}
	}

	cout << prim() << endl;
}

C++: #include <stdio.h>
#include <stdlib.h>
#define MAX 101
#define rep(i,n) for(int i=0;i<n;i++)
#define infinity 1e9
typedef struct tuple{
    int a;
    int b;
    int w;
}tuple;
int n,t=0,cost=0;
int G[MAX][MAX],tags[MAX];
tuple v[5010];
int cmpAscW(const void * n1, const void * n2){
    if (((tuple *)n1)->w > ((tuple *)n2)->w)return 1;
	else if (((tuple *)n1)->w < ((tuple *)n2)->w)return -1;
	else return 0;
}
int min(int x,int y){
    if(x < y)return x;
    else return y;
}
int max(int x,int y){
    if(x<y)return y;
    else return x;
}
void Kruskal(){
    int m,temp=0;
    rep(i,t){
        if(tags[v[i].a] != tags[v[i].b]){
            temp = max(tags[v[i].a],tags[v[i].b]);
            m = min(tags[v[i].a],tags[v[i].b]);
            tags[v[i].a] = m;
            tags[v[i].b] = m;
            cost += v[i].w;
            for(int j=m;j<n;j++){
                if(tags[j] == temp)tags[j]=m;
            }
        }
    }
}
int main(){
    scanf("%d",&n);
    rep(i,5010){
        v[i].w = infinity;
    }
    rep(i,n){
        tags[i] = i;
        rep(j,n){
            scanf("%d",&G[i][j]);
            if(i<j){
                if(G[i][j] != -1){
                    v[t].a = i;
                    v[t].b = j;
                    v[t].w = G[i][j];
                    t++;
                }
            }
        }
    }
    qsort(v, t, sizeof(tuple), cmpAscW);
    Kruskal();
    printf("%d\n",cost);
    return 0;
}

C++: 
#include <iostream>
#include <vector>
#include <algorithm>
#include <utility>
using namespace std;
int P[1010];
int cost[5000];
int src[5000];	
int dst[5000];
int A[100 + 10][100 + 10];
int M = 0;
vector< pair<int,int> > edges;

void init(int N)
{
    for(int i = 1; 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 = 0;
	cin >> n; 
	for(int r = 0; r < n; ++r)
	{
		for(int c = 0; c < n; ++c)
		{
			cin >> A[r][c];
		}
	}
	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;
				edges.push_back(make_pair(cost[M],M));
				M += 1;
			}
		}
	}
	sort(edges.begin(),edges.end());
	int sum = 0;
	init(n);
	for(int i = 0; i < M; ++i)
	{
		if(is_same_set(src[edges[i].second], dst[edges[i].second]) == 1) continue;
			unite(dst[edges[i].second],src[edges[i].second]);
			sum += edges[i].first;
	}
	cout << sum << endl;
}