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 <bits/stdc++.h>
#define For(i, a, b) for(int (i)=(a); (i)<(b); ++(i))
#define rFor(i, a, b) for(int (i)=(a)-1; (i)>=(b); --(i))
#define rep(i, n) For((i), 0, (n))
#define rrep(i, n) rFor((i), (n), 0)
#define fi first
#define se second
using namespace std;
typedef long long lint;
typedef pair<int, int> pii;

typedef struct UnionFindTree{
  vector<int> par;
  vector<int> treerank;
  
  UnionFindTree(int n){
    par.resize(n); treerank.resize(n);
    rep(i, n) par[i] = i, treerank[i] = 0;
  }
  
  int find(int x){
    if(par[x] == x) return x;
    return par[x] = find(par[x]);
  }
  
  void unite(int x, int y){
    x = find(x); y = find(y);
    if(x == y) return;
    if(treerank[x] < treerank[y]) swap(x, y);
    if(treerank[x] == treerank[y]) treerank[x]++;
    par[y] = x;
    return;
  }
  
  bool same(int x, int y){
    return find(x) == find(y);
  }
}UF;
 
template<class T> struct edge{
    int from, to; T cost;
    edge(int f, int t, T c){
        from = f; to = t; cost = c;
    }
};
 
template<class T> struct Graph{
    vector<vector<edge<T>>> G;
    int n;
    
    Graph(int n_){
        n = n_;
        G.resize(n);
    }
    
    void add_edge(int f, int t, T c){
        G[f].emplace_back(f, t, c);
    }
    
    vector<T> dijkstra(int s){
        using P = pair<T, int>;
        priority_queue<P, vector<P>, greater<P>> que;
        vector<T> d(n, numeric_limits<T>::max());
        d[s] = 0;
        que.push(P((T)0, s));
        while(!que.empty()){
            P p = que.top(); que.pop();
            int v = p.second;
            if(d[v] < p.first) continue;
            for(edge<T> &e : G[v]){
                if(d[e.to] > d[v] + e.cost){
                    d[e.to] = d[v] + e.cost;
                    que.push(P(d[e.to], e.to));
                }
            }
        }
        return d;
    }
    
    T kruskal(){
        vector<edge<T>> E;
        rep(i, n)for(edge<T> &e: G[i]) E.push_back(e);
        sort(E.begin(), E.end(), [](const edge<T> &e1, const edge<T> &e2){return e1.cost < e2.cost;});
        UF uf(n);
        T res = 0;
        for(edge<T> &e: E){
            if(!uf.same(e.from, e.to)){
                uf.unite(e.from, e.to);
                res += e.cost;
            }
        }
        return res;
    }
    
    vector<int> toposo(){
        vector<int> ret(n), in(n, 0);
        rep(i, n)for(edge<T> &e: G[i]) ++in[e.to];
        int cur = 0;
        stack<int> st;
        rep(i, n)if(!in[i]) st.push(i);
        while(!st.empty()){
            int v = st.top(); st.pop();
            ret[cur++] = v;
            for(edge<T> &e: G[v]){
                --in[e.to];
                if(!in[e.to]) st.push(e.to);
            }
        }
        return ret;
    }
};

int main(){
    int n;
    scanf("%d", &n);
    Graph<int> g(n);
    rep(i, n)rep(j, n){
        int a;
        scanf("%d", &a);
        if(a >= 0) g.add_edge(i, j, a);
    }
    printf("%d\n", g.kruskal());
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
struct Node{
	int f,e,w;
};
inline bool cmp(Node a,Node b){
	return a.w<b.w;
}
int bc[100+30];
int bcj(int x){
    if(bc[x]==x) return bc[x];
    return bc[x]=bcj(bc[x]);
}
int ans,n;
vector<Node> edl;
int main(){
	cin>>n;
	for(int i=0;i<n;++i) bc[i]=i;
	for(int i=0;i<n;++i){
		for(int j=0;j<n;++j){
			Node x;
			x.f=i;x.e=j;
			cin>>x.w;
			if(x.w!=-1) edl.push_back(x);
		}
	}
	sort(edl.begin(),edl.end(),cmp);
	int tot=0,xi=0;
	while(tot<n-1){
		int a=edl[xi].f,b=edl[xi].e;
		if(bc[bcj(a)]!=bcj(b)){
			bc[bcj(a)]=bcj(b);
			++tot;
			ans+=edl[xi].w;
		}
		++xi;
	}
	cout<<ans<<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 n;
int M[MAX][MAX];

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

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

  d[0] = 0;

  while(1)
    {
      minv = INFTY;

      u = -1;

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

  return sum;

}


int main()
{
  int e;
  int i,j;
  
 cin >>n;

  for(i = 0; i < n;i++)
    {
      for(j = 0; j < n; j++)
	{
	 
	  cin >> e;
	 M[i][j] = (e == -1) ? INFTY : e;
        }
    }

 cout << prim() << endl;
  return 0;
  
}

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

#define INF INT32_MAX

int main()
{
	int n;
	cin >> n;
	vector<vector<int>> cost( n, vector<int>( n ) );
	for( int i = 0; i < n; i++ ) {
		for( int j = 0; j < n; j++ ) {
			int c;
			cin >> c;
			cost[i][j] = INF;
			if( c >= 0 ) cost[i][j] = c;
		}
	}

	vector<int> mincost( n, INF );
	vector<int> used( n );
	mincost[0] = 0;
	int ans = 0;
	while( true ) {
		int v = -1;
		for( int u = 0; u < n; u++ ) {
			if( used[u] == 0 && ( v < 0 || mincost[u] < mincost[v] ) ) v = u;
		}
		if( v < 0 ) break;
		used[v] = 1;
		ans += mincost[v];
		for( int u = 0; u < n; u++ ) {
			mincost[u] = min( mincost[u], cost[v][u] );
		}
	}

	cout << ans << endl;
}