Go: package main

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

var scanner = bufio.NewScanner(os.Stdin)

func nextString() string {
	scanner.Scan()
	return scanner.Text()
}

func nextInt() int {
	n, err := strconv.Atoi(nextString())
	if err != nil {
		fmt.Printf("strconv.Atoi failed: %v\n", err)
	}
	return n
}

type ElementKey int
type Node struct {
	key    ElementKey
	parent *Node
	rank   int
}

func newNode(key ElementKey) *Node {
	node := Node{}
	node.key = key
	node.parent = &node
	node.rank = 0

	return &node
}

func union(x, y *Node) {
	xSet := findSet(x)
	ySet := findSet(y)
	link(xSet, ySet)
}

func link(x, y *Node) {
	if x.rank > y.rank {
		y.parent = x
	} else {
		x.parent = y

		if x.rank == y.rank {
			y.rank++
		}
	}
}

func findSet(x *Node) *Node {
	if x != x.parent {
		x.parent = findSet(x.parent)
	}
	return x.parent
}

func newDisjointSets(nSets int) *DisjointSets {
	sets := DisjointSets{}
	sets.keyToNode = map[ElementKey]*Node{}
	for i := 0; i < nSets; i++ {
		key := ElementKey(i)
		sets.keyToNode[key] = newNode(key)
	}
	return &sets
}

type DisjointSets struct {
	keyToNode map[ElementKey]*Node
}

func (sets *DisjointSets) union(x, y ElementKey) {
	xNode := sets.keyToNode[x]
	yNode := sets.keyToNode[y]
	union(xNode, yNode)
}

func (sets DisjointSets) findSet(x ElementKey) *Node {
	xNode := sets.keyToNode[x]
	return findSet(xNode)
}

type Edge struct {
	i, j   int
	weight int
}

func main() {
	scanner.Split(bufio.ScanWords)

	n := nextInt()
	sets := newDisjointSets(n)
	edges := []Edge{}
	a := make([][]int, n)
	for i := 0; i < n; i++ {
		a[i] = make([]int, n)
		for j := 0; j < n; j++ {
			a[i][j] = nextInt()
			if a[i][j] >= 0 {
				e := Edge{
					i:      i,
					j:      j,
					weight: a[i][j],
				}
				edges = append(edges, e)
			}
		}
	}
	sort.Slice(edges, func(i, j int) bool { return edges[i].weight < edges[j].weight })
	totalWeight := 0
	for _, edge := range edges {
		i := ElementKey(edge.i)
		j := ElementKey(edge.j)
		if sets.findSet(i) != sets.findSet(j) {
			totalWeight += edge.weight
			sets.union(i, j)
		}
	}
	fmt.Println(totalWeight)
}

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

// Kruskal法
//   証明: http://drken1215.hatenablog.com/entry/20121223/1356230697

class UnionFind {
    vector<int> par;
    int n;
public:
    UnionFind(int sz) : par(sz, -1) {}
    int find(int x) {
        if (par[x] < 0) return x;
        return par[x] = find(par[x]);
    }
    bool unite(int x, int y) {
        x = find(x); y = find(y);
        if (x == y) return false;
        if (par[x] > par[y]) swap(x, y);
        par[x] += par[y];
        par[y] = x;
        return true;
    }
    bool same(int x, int y) { return find(x) == find(y); }
    int size(int x) { return -par[find(x)]; }
};

int kruskal(vector<vector<P>> &edge) {
    int n = edge.size();
    typedef pair<int, P> PP;
    vector<PP> weights;
    for (int i = 0; i < n; i++) {
        for (auto &e : edge[i]) weights.push_back({e.second, {i, e.first}});
    }
    sort(weights.begin(), weights.end());
    UnionFind uf(n);
    int ret = 0;
    for (auto &w : weights) {
        int from = w.second.first;
        int to = w.second.second;
        if (uf.unite(from, to)) ret += w.first;
    }
    return ret;
};


// verified
//   http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_2_A&lang=jp
void AOJ_GRL_2_A() {
    int V, E;
    cin >> V >> E;
    vector<vector<P>> edge(V);
    for (int i = 0; i < E; i++) {
        int s, t, w;
        cin >> s >> t >> w;
        edge[s].push_back({t, w});
    }
    cout << kruskal(edge) << endl;
}

// verified
//   http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_12_A&lang=jp
void AOJ_ALDS1_12_A() {
    int n;
    cin >> n;
    vector<vector<P>> edge(n);
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            int w;
            cin >> w;
            if (w < 0) continue;
            if (j < i) continue;
            edge[i].push_back({j, w});
        }
    }
    cout << kruskal(edge) << endl;
}

int main() {
    // AOJ_GRL_2_A();
    AOJ_ALDS1_12_A();
    return 0;
}

C++: #include <iostream>
#define N 100
#define WHITE 0
#define BLACK 1
#define INF 10000

using namespace std;

int M[N][N],n,d[N];

void prim(){
  int color[n],p[n];
  for(int i=0;i<n;i++){
    color[i]=WHITE;
    d[i]=INF;
  }
  d[0]=0;
  p[0]=-1;
  while(1){
    int mincost=INF;
    int u;
    for(int i=0;i<n;i++){
      if(color[i]!=BLACK && d[i]<mincost){
        mincost=d[i];
        u=i;
      }
    }
    if(mincost==INF) break;
    //connect u to p[u]
    color[u]=BLACK;
    for(int v=0;v<n;v++){
      if(color[v]!=BLACK && M[u][v]!=-1){
        if(M[u][v]<d[v]){
          d[v]=M[u][v];
          p[v]=u;
        }
      }
    }
  }
}

int main(){
  cin >> n;
  for(int i=0;i<n;i++){
    for(int j=0;j<n;j++) cin >> M[i][j];
  }
  prim();
  int sum=0;
  for(int i=0;i<n;i++) sum+=d[i];
  cout << sum << endl;
}

C++: #if 0
????????£????????????1.5h
??¢?????????????????§??????????????¢??°????????£????????????sum????¨??????¨????????????????????£????????§?????????
???????????¢??????????????§????????????????????????????????????"-1"???????????????????????????????????§?°???????????????????
#endif

#include <iostream>
#include <algorithm>
#include <utility>
using namespace std;

int P[10000+10];

void init(int N){
	for(int i=0;i<=N;++i) P[i]=i;
}
int root(int a){
	if(P[a]==a)return a;
	else 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(){
	int N;
	cin>>N;
	int A[N][N];
	
	for(int i=0; i<N;i++){
		for(int j=0;j<N;j++){
			cin>>A[j][i]; //???????????\???
		}
	}

	int M = 0; 
	int cost[N*N],src[N*N],dst[N*N];
	for (int r=0; r<N;r++)
		for (int c=0; c<r;c++) //r<c???????§?????????????
			if (A[r][c] > -1) {
				cost[M] = A[r][c];
				src[M] = r;
				dst[M] = c;
				M += 1;
			}
	pair<int,int> edges[100000+10];
	for(int i=0;i<M;i++)
		edges[i]=make_pair(cost[i],i); //first???????????????second???ID
	sort(edges,edges+M);
	
	//for(int i=0;i<M;++i)
		//cout<<edges[i].first<<endl;

	init(N);
	int sum=0;
	for(int i=0;i<M;i++){
		if(is_same_set(src[edges[i].second],dst[edges[i].second])==true){
			unite(src[edges[i].second],dst[edges[i].second]);
			sum+=edges[i].first;
		}
	}
		cout<<sum<<endl;
}

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, 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(minv>d[i] && color[i] !=BLACK){
        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){
        if(d[v]>M[u][v]){
          d[v]=M[u][v];
          p[v]=u;
        }
      }
    }
  }
  int sum=0;
  for(int i=0;i<n;i++){
    if(p[i]!=-1)sum+=M[i][p[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;
}