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 <cstring>
using namespace std;
#define int long long

main(){
  int n;
  cin>>n;
  int a[n][n];
  for(int i=0;i<n;i++)
    for(int j=0;j<n;j++)
      cin>>a[i][j];
  int ans=0;
  bool used[n];
  memset(used,0,sizeof(used));
  used[0]=1;
  int cost[n];
  for(int i=0;i<n;i++) cost[i]=a[0][i];
  while(1){
    int v=-1;
    for(int i=0;i<n;i++){
      if(used[i]||cost[i]<0) continue;
      if(v<0||cost[i]<cost[v]) v=i;
    }
    if(v<0) break;
    used[v]=1;
    ans+=cost[v];
    for(int i=0;i<n;i++){
      if(a[v][i]<0) continue;
      if(cost[i]<0||a[v][i]<cost[i]) cost[i]=a[v][i];
    }
  }
  cout<<ans<<endl;
}

C++: #include<iostream>
#include<list>
#include<vector>
const int INF = 2001;
using namespace std;
list<int> L;
bool ok(int i) {
    for (list<int>::iterator it = L.begin(); it != L.end(); it++) if (i == *it) return 0;
    return 1;
}
int main() {
    L.push_back(0);
    int n; cin >> n;
    int M[100][100];
    int tmp = INF;
    int id = 0;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            cin >> M[i][j];
            if (i == 0 && M[i][j] != -1) {
                if (M[i][j] < tmp) {
                    tmp = M[i][j];
                    id = j;
                }
            }
        }
    }
    L.push_back(id);
    int ans = tmp;
    tmp = INF;
    int OK[100];
    while (1) {
        tmp = INF;
        for (int i = 0; i < n; i++) OK[i] = ok(i); //reached?
        for (list<int>::iterator it = L.begin(); it != L.end(); it++ ) { //from reached
            for (int i = 0; i < n; i++) {
                if (M[*it][i] < tmp && M[*it][i] != -1 && OK[i]) {
                    tmp = M[*it][i];
                    id = i;
                }
            }
        }
        if (tmp == INF) break;
        ans += tmp;
        L.push_back(id);
    }
    cout << ans << endl;
    return 0;
    
}