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 <utility>
#include <algorithm>
using namespace std;

int m,n,x,ans;
int P[100+10],edge1[2000+10],edge2[2000+10];


int root(int a){
	if (P[a] == a) return a;
	return (P[a] = root(P[a]));
}

bool same(int a, int b){
	return root(a)==root(b);
}
	
void unite(int a, int b){
	P[root(a)] = root(b);
}

int main() {
	pair<int,int> edges[2000+10];
	cin >> n;
	
	for (int i=1; i<=n; i++){
		P[i] = i;
	}
	
	m = 0;
	for (int i=1; i<=n; i++){
		for (int j=1; j<=n; j++){
			cin >> x;
			if (x>-1){
				edge1[m] = i;
				edge2[m] = j;
				edges[m] = make_pair(x, m);
				m+=1;
			}
		}
	}
	
	sort(edges, edges+m);
	
	for (int i=0; i<m; i++){
		if (!same(edge1[edges[i].second], edge2[edges[i].second])){
			unite(edge1[edges[i].second], edge2[edges[i].second]);
			ans += edges[i].first;
		}
	}
	
	cout << ans << endl;
	
}

C++: #include <bits/stdc++.h>

#define INFTY 1000001
#define MAX 100

class Edge{
public:
    int target, cost;
    Edge(int target = 0, int cost = 0) : target(target), cost(cost) {}
};

std::vector<Edge> G[MAX];
bool visited[MAX];
int parent[MAX], key[MAX];
int V, minv, min_index;

int minKey() {
    minv = INFTY;
    for (int v = 0; v < V; v++)
        if (!visited[v] && key[v] < minv)
            minv= key[v], min_index = v;
    return min_index;
}


void printMST()
{
    int sum = 0;
    for (int i = 1; i < V; i++)
        sum += key[i];
    std::printf("%d\n", sum);

}

void prim() {

    for (int i = 0; i < V; i++) {
        key[i] = INFTY;
        visited[i] = false;
    }
    key[0] = 0;
    parent[0] = -1;

    int current, target, cost;

    for (int i = 0; i < V-1; i++) {
        current = minKey();
        visited[current] = true;

        for (unsigned long v = 0; v < G[current].size(); v++) {
            target = G[current][v].target;
            cost = G[current][v].cost;
            if (!visited[target] && cost < key[target])
                parent[target] = current, key[target] = cost;
        }
    }
    printMST();

}


int main()
{
    std::scanf("%d", &V);
    int cost;

    for (int i = 0; i < V; i++) {
        for (int j = 0; j < V; j++) {
            std::scanf("%d", &cost);
            if (cost != -1) G[i].push_back(Edge(j, cost));
        }
    }

    prim();
    return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cassert>
#include <string>
#include <cstring>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <algorithm>

using namespace std;

// ---------------------
// repetition
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)

// debug
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;

// ---------------------
#define INF 922337203685477580
typedef long long ll;

int main() {
  int n;
  cin >> n;

  int G[n][n];
  int visited[n];
  int d[n];
  int p[n];
  REP(y, n) {
    REP(x, n) {
      int cost;
      cin >> cost;

      if (cost == -1) {
        G[y][x] = 1 << 15;
      } else {
        G[y][x] = cost;
      }
    }
    visited[y] = 0;
    d[y] = 1 << 15;
    p[y] = -1;
  }

  d[0] = 0;

  REP(_, n) {
    int mincost = 1 << 15;
    int minv;
    REP(v, n) {
      if (!visited[v] && d[v] < mincost) {
        minv = v;
      }
    }
    visited[minv] = 1;
    REP(to, n) {
      if (G[minv][to] < (1 << 15) && G[minv][to] < d[to]) {
        d[to] = G[minv][to];
        p[to] = minv;
      }
    }
  }

  ll sum = 0;
  REP(i, n) {
    if (p[i] != -1) {
      sum += G[i][p[i]];
    }
  }

  cout << sum << 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,M[MAX][MAX];

int prim(){
    int n,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]!=balck&&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(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;
    return 0;
}