Go: package main

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

const BIG_COST = 3000

const (
	ST_NOT_YET = iota
	ST_JOINED
)

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	nNodes := scanInt(sc)
	costs := make([][]int, nNodes)

	for r := 0; r < nNodes; r++ {
		costs[r] = make([]int, nNodes)
		for c := 0; c < nNodes; c++ {
			costs[r][c] = scanInt(sc)
		}
	}

	states := make([]int, nNodes)
	minCosts := make([]int, nNodes)

	for i := 1; i < nNodes; i++ {
		minCosts[i] = BIG_COST
	}

	for {
		var u int
		minCost := BIG_COST

		for i := 0; i < nNodes; i++ {
			if states[i] != ST_JOINED && minCosts[i] < minCost {
				minCost = minCosts[i]
				u = i
			}
		}

		// 全てのノードが MST に JOIN できたので終了。
		if minCost == BIG_COST {
			break
		}

		states[u] = ST_JOINED

		for v := 0; v < nNodes; v++ {
			cost := costs[u][v]

			// MST内の u と MST外の v との辺の重みをそれぞれ minCosts[v] として
			// 保存しておく。このうち辺の重みが最小となる v が次のループで MST に JOIN する。
			if states[v] != ST_JOINED && cost != -1 && cost < minCosts[v] {
				minCosts[v] = cost
			}
		}
	}

	sum := 0
	for _, cost := range minCosts {
		sum += cost
	}
	fmt.Println(sum)
}

func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	v, _ := strconv.Atoi(sc.Text())
	return v
}

C++: #include<iostream>
#include<vector>
#include<algorithm>

using namespace std;

struct edge{
	int u;	//?????????
	int v;	//??°??????
	int cost;	//??????
	//???????????????????????§?????§?°???¢????????????
	bool operator<(const edge& another) const {
			return cost < another.cost;
	}
};

const int MAX_N = 100;
const int MAX_M = 10000;

edge es[MAX_M];
int par[MAX_N];
int rank[MAX_N];

void initial(int n){		//?????????
	for(int i=0;i<n;i++){
		par[i] = i;
		rank[i] = 0;
	}
}

int find(int x){		//??¨??????????±???????
	if(par[x] == x){
		return x;
	}else{
		return par[x] = find(par[x],par);
	}
}

void unite(int x,int y){	//x???????????¨??¨y???????????¨?????????
	x = find(x,par);	//x???????????¨??????????????????x??¨??????
	y = find(y,par);	//y???????????¨??????????????????y??¨??????
	if(x == y) return;
	if(rank[x] < rank[y]){
		par[x] = y;
	}else{
		par[y] = x;
		if(rank[x] == rank[y])
			rank[x]++;
	}
}

bool same(int x,int y){	//x,y???????????¨???????????§?????????
	return find(x,par) == find(y,par);
}

bool comp(const edge& e1,const edge& e2){
	return e1.cost < e2.cost;
}

int kruskal(int n,int m){
	initial(n,par,rank);
	int res = 0;
	for(int i=0;i<m;i++){
		edge e = es[i];
		if(!same(e.u,e.v,par)){
			unite(e.u,e.v,par,rank);
			res += e.cost;
		}
	}
	return res;
}

int main(){
	int n,m=0,k,l,x;
	cin >> n;	//????????°?????\???
	int cnt=0;
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			cin >> x;
			if(x != -1){
				edge e;
				e.from = i; e.to = j; e.cost = x;
				es[m] = e;
				m++;
			}
		}
	}
	sort(es,es+m);
	cout << kruskal(n,m) << "\n";
}

C++: #include <bits/stdc++.h>
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 (color[i] != BLACK && d[i] < minv) {
        minv = d[i];
        u = 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++) {
    sum += d[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;
};

C++: #include<stdio.h>
#include<algorithm>
using namespace std;
int maxn=1<<21;
int n;
int a[105][105];
int color[105];
int d[105];
int p[105];
int f()
{
    int u;int minx;
    for(int i=0;i<105;i++)
    {
        d[i]=maxn;
        p[i]=-1;
    }
    d[0]=0;
    while(1)
    {
        minx=maxn;
        u=-1;
        for(int i=0;i<n;i++)
        {
            if(minx>d[i]&&color[i]==0)
            {
                u=i;
                minx=d[i];
            }
        }
        if(u==-1) break;
        color[u]=1;
        for(int i=0;i<n;i++)
        {
            if(color[i]==0&&a[u][i]!=maxn)
            {
                if(d[i]>a[u][i])
                {
                    d[i]=a[u][i];
                    p[i]=u;
                    color[i]=0;
                }
            }
        }
    }
    int sum=0;
    for(int i=0;i<n;i++)
    {
        if(p[i]!=-1) sum+=a[i][p[i]];
    }
    return sum;
}
int main()
{
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            int x;
            scanf("%d",&x);
            a[i][j]=(x==-1)?maxn:x;
        }
    }
    printf("%d\n",f());
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
static const int INFTY = 2000000000;
static const int NIL = -1;
static const int MAX = 100;
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;   //d[i]??????T??????????????§?????????V????±?????????????i?????????????????????
        p[i] = -1;
        color[i] = WHITE;
    }

    d[0] = 0;

    while(1){
        minv = INFTY;
        u = -1;
        for(int i=0;i<n;i++){
            if(d[i] < minv && color[i] != BLACK){    //?¢????????????§????????????????????????INFTY??§?????????????????¢???
                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){  //2??????????????¶???????????????????
                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(){
    scanf("%d",&n);

    for(int i=0;i<n;i++){
        for(int k=0;k<n;k++){
            int e; scanf("%d",&e);
            M[i][k] = (e==-1) ? INFTY : e;
        }
    }

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

    return 0;
}

C++: #include <algorithm>
#include <vector>
#include <utility>
#include <iostream>
using namespace std;
typedef pair<int, pair<int,int> > Edge;

int* uni;
void init(int N)
{
    uni = new int[N];
    for (int i = 0; i < N; i++)
    {
        uni[i] = -1;
    }
}

int root(int a)
{
    if (uni[a] < 0) return a;
    else return uni[a] = root(uni[a]);
}

bool connect(int a, int b)
{
    a = root(a);
    b = root(b);
    if (a == b) return false;
    if (uni[a] > uni[b]) swap( a, b);
    uni[a] += uni[b];
    uni[b] = a;
    return true;
}

int main(){
  int n;
  cin >> n;
  vector<Edge> graph(n);
  for (int i=0;i<n;i++){
    for(int j=0;j<n;j++){
      int tmp;
      cin >> tmp;
      if (tmp != -1){
         graph.emplace_back(tmp, make_pair(i,j));
      }
    }
  }
  sort(graph.begin(), graph.end());
  init(n);
  int sum=0;
  for (auto ite=graph.begin();ite != graph.end();ite++){
    if (connect(ite->second.first, ite->second.second))
        sum+=ite->first;
  }
  cout << sum << endl;
}