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 <bits/stdc++.h>
using namespace std;
using Int = int_fast64_t;

class edge{
public:
	Int cost;
	size_t from, to;
	edge(Int c, size_t f, size_t t){
		cost = c;
		from = f;
		to = t;
	}
};
typedef vector<edge> edges;
typedef vector<edges> graph;
class unionfind{ // 0-indexed
public:
	vector<int> parent;
	vector<size_t> size;
	size_t num_of_disjointsets;
	unionfind(size_t n){
		parent.resize(n, -1);
		size.resize(n, 1);
		num_of_disjointsets = n;
	}

	size_t find(size_t x){
		if(parent[x] == -1) return x;
		return parent[x] = find(parent[x]);
	}

	size_t unite(size_t x, size_t y){
		x = find(x);
		y = find(y);
		if(x == y) return -1;
		if(size[x] < size[y]){
			parent[x] = y;
			size[y] += size[x];
			size[x] = 0;
			--num_of_disjointsets;
			return y;
		}else{
			parent[y] = x;
			size[x] += size[y];
			size[y] = 0;
			return x;
			--num_of_disjointsets;
		}
	}

	bool united(size_t x, size_t y){
		return find(x) == find(y);
	}
};
bool operator < (const edge &e, const edge &f){
	return e.cost != f.cost ? e.cost > f.cost : e.from != f.from ? e.from < f.from : e.to < f.to;
}
vector<edge> kruskal(graph &g){
	priority_queue<edge> pq;
	for(size_t i=0; i<g.size(); ++i)
		for(size_t j=0; j<g[i].size(); ++j)
			if(i < g[i][j].to)
				pq.push(g[i][j]);
	unionfind uf(g.size());
	vector<edge> res;
	while(!pq.empty()){
		edge e = pq.top(); pq.pop();
		if(!uf.united(e.from, e.to)){
			uf.unite(e.from, e.to);
			res.push_back(e);
		}
		if(uf.num_of_disjointsets == 1) break;
	}
	return res;
}

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	Int n; cin >> n;
	graph g(n);
	for(Int i=0; i<n; ++i)
		for(Int j=0; j<n; ++j){
			Int a; cin >> a;
			if(a != -1){
				g[i].push_back(edge(a, i, j));
				g[j].push_back(edge(a, j, i));
			}
		}
	vector<edge> mst = kruskal(g);
	Int ans = 0;
	for(auto i:mst) ans += i.cost;
	cout << ans << "\n";
}

C++: #include <iostream>
#include <vector>
#include <utility>
#include <algorithm>
using namespace std;
int n;
int A[110][110];
int P[10010] = {0};
void init(int N){
	for(int i=0;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(){
	while(cin >> n && n>0){
		init(n);
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++){
				cin >> A[i][j];
			}
		}
		int cost[10000],src[10000],dst[10000];
		vector< pair<int,int> > edges;
		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;
					//cout << cost[M] << endl;
					pair<int,int> edge = make_pair(cost[M],M);
					edges.push_back(edge);
					M++;
				}
			}
		}
		sort(edges.begin(),edges.end());
		long long  sum = 0;
		for(int i=0;i<M;i++){
			int n1 = src[edges[i].second],n2 = dst[edges[i].second];
			if(!is_same_set(n1,n2)){
				unite(n1,n2);
				sum += edges[i].first;
			}
		}
		cout << sum << endl;
	}
}

#if 0
所要時間は45分程度。クラスカル法を回す部分の実装に少し苦戦した。
#endif

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

using Edge = std::pair<uint32_t, uint32_t>;
using Graph = std::vector<std::vector<Edge>>;

int main() {
  uint32_t n = 0;
  std::cin >> n;
  Graph G(n);
  for (size_t i = 0; i < n; i++) {
    for (size_t j = 0; j < n; j++) {
      int32_t cost = 0;
      std::cin >> cost;
      if (cost != -1) G[i].push_back({j, cost});
    }
  }

  std::vector<uint32_t> min_cost(n, -1);
  min_cost[0] = 0;
  std::vector<bool> seen(n, false);
  while (!std::all_of(seen.begin(), seen.end(), [](bool v) { return v; })) {
    uint32_t current = -1, cost = -1;
    for (size_t i = 0; i < n; i++) {
      if (!seen[i] && (cost > min_cost[i])) {
        current = i;
        cost = min_cost[i];
      }
    }

    seen[current] = true;
    for (auto &&node : G[current]) {
      if (seen[node.first]) continue;
      min_cost[node.first] = std::min(node.second, min_cost[node.first]);
    }
  }

  std::cout << std::accumulate(min_cost.begin(), min_cost.end(), 0)
            << std::endl;

  return 0;
}

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 prim();

int n,M[MAX][MAX];

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;
  return 0;
}

int prim(){
  int u,minv,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(minv > d[i] && color[i]!=BLACK){
        u=i;
        minv=d[i];
      }
    }
    if(u==-1)break;
    color[u]=BLACK;

    for(int h=0;h<n;h++){
      if(color[h]!=BLACK && M[u][h]!=INFTY){
        if(d[h]>M[u][h]){
          d[h]=M[u][h];
          p[h]=u;
          color[h]=GRAY;
        }
      }
    }
  }
  int vv=0;
  for(int i=0;i<n;i++){
    if(p[i]!=-1) vv=vv+M[i][p[i]];
  }
  return vv;
}

C++: #include<bits/stdc++.h>
using namespace std;

struct Edge{
  int to,cost;
  bool operator<(const Edge&a)const{
    return cost>a.cost;
  }
};
struct Graph{
  Graph(int n):E(n),T(n),N(n){}
  vector<vector<Edge>>E,T;
  int N;
  void add_edge(int a,int b,int c){
    E[a].push_back({b,c});
    E[b].push_back({a,c});
  }
  void prim(int s){
    bool visited[N];
    priority_queue<pair<Edge,int>>Q;
    memset(visited,0,N);
    Q.push({{s,0},-1});
    while(!Q.empty()){
      Edge e=Q.top().first;
      int from=Q.top().second;Q.pop();
      if(visited[e.to])continue;
      visited[e.to]=1;
      if(from+1)T[from].push_back(e),T[e.to].push_back({from,e.cost});
      for(auto&a:E[e.to])
      if(!visited[a.to])Q.push({a,e.to});
    }
  }
  int get_cost(){
    int ret=0;
    for(auto&a:T)for(auto&b:a)ret+=b.cost;
    return ret/2;
  }
  vector<Edge>* get_MST(){
    return T.data();
  }
};

main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N;
    cin>>N;
    Graph G(N);
    for(int i=0;i<N;++i)
        for(int j=0;j<N;++j){
            int a;
            cin>>a;
            if(~a) G.add_edge(i,j,a);
        }
    G.prim(0);
    cout<<G.get_cost()<<endl;
}