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++: //MST
#if 0

#endif // 0
#include<iostream>
#include<vector>
#include<algorithm>
#define MAX 10010
using namespace std;
vector<int> s;
int root(int x)
{
    if(s[x]==x)return x;
    return root(s[x]);
}

int main()
{
    int cost[MAX],src[MAX],dst[MAX];
    pair<int,int> pos[MAX];
    int n,sum=0;
    cin>>n;
    s.resize(n);
    for(int i=0;i<n;i++)
    {
        s[i]=i;
    }
    int m=0;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            int a;
            cin>>a;
            if(a>=0&&j>i)
            {
                cost[m]=a;
                src[m]=i;
                dst[m]=j;
                pos[m].first=cost[m];
                pos[m].second=m;
                m++;
            }
        }
    }
    sort(pos,pos+m);
    int c=0;
    for(int i=0;i<m;i++)
    {
        int d=pos[i].second;
        if(root(src[d])!=root(dst[d]))
        {
            sum+=cost[d];
            s[root(src[d])]=root(dst[d]);
            c++;
        }
        if(c>=n-1)
            break;
    }

    cout<<sum<<endl;
    return 0;
}

C++: #include <iostream>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm>	// require sort next_permutation count __gcd reverse etc.
#include <cstdlib>	// require abs exit atof atoi 
#include <cstdio>		// require scanf printf
#include <functional>
#include <numeric>	// require accumulate
#include <cmath>		// require fabs
#include <climits>
#include <limits>
#include <cfloat>
#include <iomanip>	// require setw
#include <sstream>	// require stringstream 
#include <cstring>	// require memset
#include <cctype>		// require tolower, toupper
#include <fstream>	// require freopen
#include <ctime>		// require srand
#define rep(i,n) for(int i=0;i<(n);i++)
#define ALL(A) A.begin(), A.end()
#define INF 1<<20

using namespace std;

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

int cost[105][105];
int mincost[105];
bool used[105];

int prim (int n )
{
	memset (used, false, sizeof (used ) );
	rep (i, n ) mincost[i] = INF;

	mincost[0] = 0;
	int res = 0;

	while (true ){
		int v = -1;
		// X  に属さない頂点のうち X からの辺のコストが最小になる頂点を探す
		rep (u, n )
			if (!used[u] && (v == -1 || mincost[u] < mincost[v] ) ) v = u;

		if (v == -1 ) break;

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

		rep (u, n ) mincost[u] = min (mincost[u], cost[v][u] );
	} // end while

	return res;
}

int main()
{
	memset (cost, -1, sizeof (cost ) );
	ios_base::sync_with_stdio(0);
	int n; cin >> n;
	rep (i, n ) rep (j, n ) cost[i][j] = INF;

	rep (i, n ) rep (j, n ){
		int in; cin >> in;
		if (in == -1 ) in = INF;
		cost[i][j] = in;
	} // end rep

	int res = prim(n );
	cout << res << endl;
		
	return 0;
}

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

const int WHITE = 0;
const int GRAY = 1;
const int BLACK = 2;

int n;
vector<vector<int>> G;
vector<int> d;
vector<int> colors;

int mst(){
    int left = n;
    int res = 0;
    d[0] = 0;
    //int u = 0;
    while(left--){
        int min_id = -1;
        int cur_min = INT_MAX;
        for(int i = 0;i<n;i++){
            if(colors[i] !=BLACK && d[i] < cur_min){
                min_id = i;
                cur_min = d[i];
            }
        }
        if(min_id==-1) break;
        int u = min_id;
        //printf("%d\n",min_id);
        res += d[u];
        colors[u] = BLACK;
        for(int v =0;v<n;v++){
            if(colors[v]!=BLACK && G[u][v] < d[v]){
                d[v] = G[u][v];
            }
        }
    }
    return res;
}

int main()
{
    //freopen("data.txt","r",stdin);
    cin >> n;
    G.resize(n,vector<int>(n,INT_MAX));
    d.resize(n,INT_MAX);
    colors.resize(n,WHITE);
    int tmp;
    for(int i =0;i<n;i++){
        for(int j=0;j<n;j++){
            cin >> tmp;
            if(tmp!=-1) G[i][j] = tmp;
        }
    }
    cout << mst() <<endl;
    return 0;
}

C++: #include <iostream>
#include <vector>
#include <queue>
#define USED 1
using namespace std;
typedef pair<int, int> pi;
int graph[200][200];
int used[200];
// first:weight, second:key
priority_queue<pi, vector<pi>, greater<pi>> pqueue;

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      cin >> graph[i][j];   
    }
  }
  
 
  int ans = 0, key;
  pqueue.push(make_pair(0,0));
  for (int i = 0; i < n; i++) {
    pi p = pqueue.top();
    pqueue.pop();
    key = p.second;
    if (used[key] == USED) {
      i--;
      continue;
    }
    used[key] = USED;
    ans += p.first;
    //cout << "Distance: " << p.first << ", Key: " << p.second << endl;
    for (int j = 0; j < n; j++) {
      if (graph[key][j] != -1 && used[j] != USED) {
        pqueue.push(make_pair(graph[key][j],j));
      }
    }
  }
  cout << ans << endl;
}

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

int main(){
  long n;
  long G[100][100],d[100];
  long minc,mi,i;
  bool flg[i];
  long sum=0;
  
  cin >> n;
  for (i=0;i<n;i++){
    for (int j=0;j<n;j++){
      cin >> G[i][j];
      if (G[i][j]==-1) G[i][j]=INF;
    }
    d[i]=INF;
    flg[i]=false;
  }

  d[0]=0;
  while (true){
    minc=INF;
    for (i=0;i<n;i++){
      if (!flg[i]&&d[i]<minc){
	minc=d[i];
	mi=i;
      }
    }
    if (minc==INF) break;
    flg[mi]=true;
    for (i=0;i<n;i++){
      if (G[mi][i]==INF)continue;
      if (!flg[i]&&d[i]>G[mi][i]){
	d[i]=G[mi][i];
	//cout << d[i];
      }
    }
  }
  
  for (i=0;i<n;i++){
    sum+=d[i];
  }
  cout << sum<<endl;
  
  return 0;
}