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<cstring>
#include<algorithm>
using namespace std;
 
int x[10000][10000];
int MIN[10000];
int MINX = 0;
 
int main() {
    memset(MIN, 127, sizeof(MIN));
    int n, a;
    cin >> n;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            cin >> a;
            x[i][j] = a;
        }
    }
    MIN[0] = 0;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            for (int k = 0; k < n; k++) {
                if (x[j][k] != -1) {
                    MIN[k] = min(MIN[k], x[j][k]);
                }
            }
        }
    }
    for (int i = 0; i < n; i++) {
        MINX = max(MINX, MIN[i]);
    }
    cout << MINX << endl;
    return 0;
}

C++: #include <iostream>
#include <queue>

using namespace std;




struct link { //link connecting x and y vertices
  int x;
  int y;
  int weight;
  bool operator<(const link& other) const
  {
    return weight > other.weight; // for min priority queue, trick the operator
  }
};

priority_queue<link> links;
int roots[100];

int root(int x) {
  if (x == roots[x]) {
    return x;
  } else {
    //roots[x] = root(roots[x]); //attach all to root
    return root(roots[x]);
  }
}

bool same(int x, int y) {
  return (root(x) == root(y));
}

void unite(int x, int y) {
  roots[root(x)] = root(y);
}


  
int main() {
  int min_cost = 0;
  int n;
  cin >> n;


  for (int i = 0; i < n; i++) {
    roots[i] = i; // initialize roots
    for (int j = 0; j < n; j++) {
      
      int weight;
      cin >> weight;
      if (j <= i ) continue;
      if (weight != -1) {
	link new_link = {i, j, weight};
	links.push(new_link);
	//cout << "link added has i j weight: " << i << " " << j << " " << weight << endl;
      }
    }
  }

  while (!links.empty()) {
    link cur_link = links.top();
    //cout << "cur link weight: " << cur_link.weight << " x: " << cur_link.x << " y: " << cur_link.y << endl;
    if (!same(cur_link.x, cur_link.y)) {
      min_cost += cur_link.weight;
      unite(cur_link.x, cur_link.y);
    }
    links.pop();
  }

  cout << min_cost << endl;
      
    
}

C++: #include<iostream>
using namespace std;
#define MAX 100
#define White 1
#define Black 2
#define Red 3
#define Infinit 2001
struct Vertex {
	int weight,color,parent;
};
int M[MAX][MAX];
int n;
Vertex v[MAX];
void search() {
	int weight[MAX],pointer,min;
	for (int i = 0; i < n; i++) {
		if (i == 0) {
			v[i].weight = 0;
		}
		else {
			v[i].weight = Infinit;
		}
		v[i].color = White;
		v[i].parent = -1;
	}
	while (true) {
		min = Infinit;
		pointer = -1;
		for (int i = 0; i < n; i++) {
			if (v[i].color != Black && min>v[i].weight) {
					min = v[i].weight;
					pointer = i;
			}
		}
		if (min==Infinit) break;
		v[pointer].color = Black;
		for (int k = 0; k < n; k++) {
			if (M[pointer][k] != -1 && v[k].color!=Black) {
				if (M[pointer][k]<v[k].weight) {
					v[k].weight = M[pointer][k];
					v[k].parent = pointer;
					v[k].color = Red;
				}
			}
		}

	}
	int sum = 0;
	for (int i = 0; i < n; i++) {
		sum += v[i].weight;
	}
	cout << sum << endl;


}
int main() {
	int id;
	cin >> n;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			cin >>id;
			M[i][j]=id;
		}
	}
	search();
}

C++: #include <iostream>
#include <algorithm>
#define N 100
#define INF 2000000000
#define WHITE 0
#define GRAY 1
#define BLACK 2
using namespace std;

int n;
int M[N][N],color[N],d[N],p[N];
   //M??????????????????color????¨??????¶???(0,1,2),d????????????????°???????????¨????
   //p???MST???????°???¨?????¨??????????????????????????????

void Prim() {
	for (int i = 0;i < n;i++) d[i] = INF;
	d[0] = 0;  //?????????????????????????????§???????????§????´???????????????????????????????
	/*p[0] = -1;  //??????????????????????????????????????????*/

	int u;
	while (true) {
		int min = INF;
		for (int i = 0;i < n;i++) {
			if(color[i]!=BLACK&&min>d[i]){
				min = d[i];
				u = i;
			}
		}

	if (min == INF) break;

	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;*/
				/*color[v] = GRAY;*/
			}

	}

	int sum = 0;
	for (int i = 0;i < n;i++) sum += d[i];
	cout << sum << endl;
		
}

int main() {
	cin >> n;
	for (int i = 0;i < n;i++)
		for (int j = 0;j < n;j++) {
			cin >> M[i][j];
			if (M[i][j] == -1) M[i][j] = INF;
		}

	Prim();

}