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>
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, A[MAX][MAX];

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

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

    while(1){
        minv = INFTY;
        for(int i = 0; i < n; i++){
            if(color[i] != BLACK && d[i] < minv){
                minv = d[i];
                u = i;
            }
        }

        if(minv == INFTY) break;

        color[u] = BLACK;

        for(int v = 0; v < n; v++){
            if(color[v] != BLACK && A[u][v] != INFTY){
                if(A[u][v] < d[v]){
                    d[v] = A[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;
            A[i][j] = (e == -1) ? INFTY : e;
        }
    }

    cout << prim() << endl;

    return 0;
}

C++: #include<bits/stdc++.h>
//cout<<setprecision(1)<<fixed<<endl;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int MAX_N=100000;
#define eps 1e-7
//#define Kongxiangzhouye

static const int MAX=100;
static const int INF=(1<<21);
static const int WHITE=0;
static const int GRAY=1;
static const int BLACK=2;

int begin[10000];
int to[10000];
int next[10000];
int weight[10000];
int e,n;
void add(int x,int y,int z){
	to[++e]=y;
	next[e]=begin[x];
	begin[x]=e;
	weight[e]=z;
}

struct p{
	int edge;
	int weight;
	bool operator < (const p& u)const{
		return weight>u.weight;
	}
};
int createtree(){
	priority_queue<p> q;
	int flag[100];
	memset(flag,0,sizeof(flag));
	int te=begin[1];
	while(to[te]!=-1){
		if(flag[to[te]]==0)
			q.push((p){te,weight[te]});
		te=next[te];
	}
	flag[1]=1;
	int ans=0;
	while(1){
		int flag2=1;
		for(int i=1;i<=n;i++){
			if(flag[i]==0){
				flag2=0;
				break;
			}
		}
		if(flag2==1)
			break;
		while(flag[to[q.top().edge]]==1)
			q.pop();
		p now=q.top();
		q.pop();
		ans=ans+now.weight;
		flag[to[now.edge]]=1;
		int te=begin[to[now.edge]];
		while(te!=-1){
			if(flag[to[te]]==0)
				q.push((p){te,weight[te]});
			te=next[te];
		}
	}
	return ans;
}
int main(){
	ios::sync_with_stdio(false);
	#ifdef Kongxiangzhouye
		freopen("in.txt","r",stdin);
		freopen("out.txt","w",stdout);
	#endif
	while(cin>>n){
		memset(begin,-1,sizeof(begin));
		memset(next,-1,sizeof(next));
		memset(to,-1,sizeof(to));
		e=0;
		for(int i=1;i<=n;i++){
			for(int j=1;j<=n;j++){
				int te;
				cin>>te;
				if(te!=-1)
					add(i,j,te);
			}
		}
		cout<<createtree()<<endl;
	}

	return 0;
}

C++: #include <iostream>
#include <iomanip>
#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <sstream>
#include <string>
#include <utility>
#include <map>
#include <memory>
#include <set>
#include <vector>
#include <deque>
#include <list>
#include <stack>
#include <queue>
// #include <array>

using namespace std;

class MSTree
{
public:
  int solve();
};

int MSTree::solve()
{
  int n;
  cin >> n;
  
  vector<vector<int>> adj(n, vector<int>(n));
  list<int> t, vt;

  for ( auto i = 0; i < n; ++i )
    for ( auto j = 0; j < n; ++j )
      cin >> adj[i][j];

  t.push_back( 0 );

  for ( auto i = 1; i < n; ++i )
    vt.push_back( i );

  int w = 0;

  while ( vt.size() )
    {
      int mw = 4000;
      list<int>::iterator itm;
      
      for ( auto ita = t.begin(); ita != t.end(); ++ita )
        for ( auto itb = vt.begin(); itb != vt.end(); ++itb )
          {
            if ( adj[*ita][*itb] >= 0 && mw > adj[*ita][*itb] )
              {
                itm = itb;
                mw = adj[*ita][*itb];
              }
          }
      
      w += mw;
      t.push_back( *itm );
      vt.erase( itm );
    }

  cout << w << endl;
    
  return 0;
}

int main()
{
  MSTree m;

  m.solve();

  return 0;
}

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

using namespace std;



int main() {
	int n,tmp;
	cin >> n;
	pair<int, pair<int, int>>* sorted_weight = new pair<int, pair<int, int>>[n * n];
	int* equiv = new int[n];


	int cnt_equiv = 0;
	int cnt_edge = 0;
	int cost = 0;
	
	for (int i = 0;i < n;i++) {
		for (int j = 0;j < n;j++) {
			cin >> tmp;
			sorted_weight[i * n + j] = make_pair(tmp, make_pair(i, j));
		}
		equiv[i] = 0;
	}
	sort(sorted_weight, sorted_weight + n * n);
	for (int i = upper_bound(sorted_weight, sorted_weight + n * n, make_pair(-1, make_pair(n, n))) - sorted_weight;cnt_edge<n-1;i++) {
		if (equiv[sorted_weight[i].second.second] == 0 || equiv[sorted_weight[i].second.first] == 0) {
			cnt_edge++;
			cost += sorted_weight[i].first;
			if (equiv[sorted_weight[i].second.second] == 0 && equiv[sorted_weight[i].second.first] == 0) {
				cnt_equiv++;
				equiv[sorted_weight[i].second.second] = equiv[sorted_weight[i].second.first] = cnt_equiv;
			}
			else if (equiv[sorted_weight[i].second.second] == 0) {
				equiv[sorted_weight[i].second.second] = equiv[sorted_weight[i].second.first];
			}
			else {
				equiv[sorted_weight[i].second.first] = equiv[sorted_weight[i].second.second];
			}
		}
		else if (equiv[sorted_weight[i].second.second] != equiv[sorted_weight[i].second.first]) {
			cnt_edge++;
			cost += sorted_weight[i].first;
			tmp = equiv[sorted_weight[i].second.second];
			for (int j = 0;j < n;j++) {
				if (equiv[j] == tmp)equiv[j] = equiv[sorted_weight[i].second.first];
			}
		}
	}
	
	cout << cost << endl;
}