Go: package main

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

const (
	WHITE int = iota
	GRAY
	BLACK
)

type graph struct {
	G [][]int
	C []int
	D []int
	P []int
}

var INFTY = 1 << 32

func constructGraph() *graph {
	sc := bufio.NewScanner(os.Stdin)
	sc.Scan()
	n, _ := strconv.Atoi(sc.Text())

	g := make([][]int, n)
	c := make([]int, n)
	d := make([]int, n)
	p := make([]int, n)

	for i := 0; i < n; i++ {
		sc.Scan()
		fields := strings.Fields(sc.Text())
		g[i] = toIntSlice(fields)
	}

	return &graph{g, c, d, p}
}

func (g *graph) initMst() {
	for i := 0; i < len(g.G); i++ {
		g.C[i] = WHITE
		g.D[i] = INFTY
	}
	g.D[0] = 0
	g.P[0] = -1
}

func (g *graph) primMst() int {
	g.initMst()

	var u int
	for {
		mincost := INFTY
		for i := 0; i < len(g.G); i++ {
			if g.C[i] != BLACK && g.D[i] < mincost {
				mincost = g.D[i]
				u = i
			}
		}

		if mincost == INFTY {
			break
		}

		g.C[u] = BLACK

		for i := 0; i < len(g.G); i++ {
			if g.C[i] != BLACK && g.existEdge(u, i) {
				if g.G[u][i] < g.D[i] {
					g.D[i] = g.G[u][i]
					g.P[i] = u
				}
			}
		}
	}

	totalCost := 0
	for _, cost := range g.D {
		totalCost += cost
	}
	return totalCost
}

func (g *graph) existEdge(u, v int) bool {
	return g.G[u][v] != INFTY && g.G[v][u] != INFTY
}

func toIntSlice(s []string) []int {
	a := make([]int, len(s))
	for i, v := range s {
		num, _ := strconv.Atoi(v)
		if num == -1 {
			num = INFTY
		}
		a[i] = num
	}
	return a
}

func main() {
	g := constructGraph()
	fmt.Println(g.primMst())
}

C++: #include <iostream>
#include <climits>
#include <vector>
#include <algorithm>

using namespace std;

class UnionFind {
private:
  std::vector<int> data;
public:
  UnionFind(const int size):data(size,-1){}
  void union_set(int x, int y){
    x = this->get_root(x);
    y = this->get_root(y);
    
    if (x != y) {
      if (this->data[y] < data[x]) {
        swap(x, y);
      }
      this->data[x] += this->data[y];
      this->data[y] = x;
    }  
  }
  bool is_same(const int x, const int y){
    return this->get_root(x) == this->get_root(y);
  }
  int get_root(const int x){
    return this->data[x] < 0 ? x : this->data[x] = this->get_root(this->data[x]);
  }
  int get_size(int x){
    return -this->data[this->get_root(x)];
  }
};

using Edge = struct edge{
  int from, to, cost;
  edge(const int s, const int t, const int c):from(s),to(t),cost(c){}
  bool operator<(const edge& e) const {return cost < e.cost;}
};

struct Graph{
  int v_size;
  vector<Edge> edge_set;
  
  Graph(const int v_size):v_size(v_size){}
  
  void add_edge(const int s, const int t, const int w){
    this->edge_set.emplace_back(Edge{s, t, w});
  }
};

int mst(Graph& g){
  UnionFind uf(g.v_size);
  int ans {0};

  sort(begin(g.edge_set), end(g.edge_set));

  for(auto&& e : g.edge_set){
    if(!uf.is_same(e.from, e.to)){
      ans += e.cost;
      uf.union_set(e.from, e.to);
    }
  }
  
  return ans;
}

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

  int x;
  for(int i = 0; i < n; ++i){
    for(int j = 0; j < n; ++j){
      cin >> x;
      if(x != -1){
        g.add_edge(i, j, x);
      }
    }
  }

  cout << mst(g) << endl;
}

C++: #include <iostream>
#include <sstream>
#include <cstdio>
#include <stdlib.h>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
#define max 2001
#define INF 200000000
int color[max],p[max],n;
int m[max][max],d[max];
void prim(){
  int mincost,u,i;
  p[0]=-1;
  for(i=1;i<n;i++)d[i]=INF;
  while(1){
    mincost = INF;
    for(i=0;i<n;i++){
      if(color[i]!=1&&d[i]<mincost){
	mincost=d[i];
	u=i;
      }
    }
    if(mincost==INF)break;
    color[u]=1;
    for(int j=0;j<n;j++){
      if(color[j]!=1&&m[u][j]!=-1)
	if(m[u][j]<d[j]){
	  d[j]=m[u][j];
	  p[j]=u;
	  color[j]=-1;
	}
    }
  }
}
int main(){
  int sum=0;
  cin>>n;
  for(int i=0;i<n;i++)
    for(int j=0;j<n;j++)
      cin>>m[i][j];
  prim();
  for(int i=0;i<n;i++)
    sum+=d[i];

  cout<<sum<<endl;
  return 0;
}

C++: #define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
//#include "MyMath.h"
//#include "MyDisjointset.h"
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <functional>
#include <stdio.h>
using namespace std;
typedef pair<int, int> P;
const int INF = 2000000000;
int main() {
	int n; cin >> n;
	int m[101][101];
	for (int i = 0; i < n; i++)for (int j = 0; j < n; j++)
		cin >> m[i][j];
	int res = 0;
	int *d = new int[n];
	fill(d, d + n, INF); 
	d[0] = 0;
	bool *visit = new bool [n];
	priority_queue <P, vector<P>, greater<P> > q;
	q.push(P(d[0], 0));
	while (!q.empty()) {
		int f = q.top().first, s = q.top().second;
		visit[s] = true;
		res += f;
		q.pop();
		for (int i = 0; i < n; i++)
			if (!visit[i] && m[s][i] != -1 && m[s][i] > d[i]) {
				m[s][i] = d[i];
				q.push(P(d[i], i));
			}
	}
	cout << res << endl;
	delete[] d, visit;
	return 0;
}

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

using namespace std;
using Graph=vector<vector<int>>;



struct Edge {
	int node1, node2, weight;

	Edge() {}

	Edge(int n1, int n2, int w) :node1(n1), node2(n2), weight(w) {}

	void numset(int n1, int n2, int w) {
		node1 = n1; node2 = n2; weight = w;
	}

};

void eswap(Edge& e1, Edge& e2) {
	Edge temp = e1;   e1 = e2;  e2 = temp;
}

void bubbleSort(vector<Edge>& edge) {
	bool flag;
	for (int i = 0; i < edge.size(); i++) {
		flag = true;
		for (int j = 0; j < edge.size()-i-1; j++) {
			if (edge.at(j).weight > edge.at(j + 1).weight) {
				eswap(edge.at(j), edge.at(j + 1));
				flag = false;
			}
		}
		if (flag) break;
	}
}

int kruskal(Graph &spntree,vector<Edge>& edgeset) {
	int size = spntree.size();
	queue<int> unconnected;
	vector<bool> connected(size,false);
	
	if (edgeset.size() == 0) return 0;
	bubbleSort(edgeset);

	connected.at(edgeset.at(0).node1)=true;
	connected.at(edgeset.at(0).node2) = true;
	spntree.at(edgeset.at(0).node1).push_back(edgeset.at(0).node2);
	spntree.at(edgeset.at(0).node2).push_back(edgeset.at(0).node1);
	int index=0,count = 2,cost=edgeset.at(0).weight;
	Edge ed;
	edgeset.erase(edgeset.begin());

	while (count<size) {
		ed = edgeset.at(index);
		if (connected.at(ed.node1) == true && connected.at(ed.node2) == false) {
			connected.at(ed.node2) = true;
			spntree.at(ed.node1).push_back(ed.node2);
			spntree.at(ed.node2).push_back(ed.node1);
			edgeset.erase(edgeset.begin() + index);
			count++; cost += ed.weight;
			index = 0; continue;
		}
		else if (connected.at(ed.node2) == true && connected.at(ed.node1) == false) {
			connected.at(ed.node1) = true;
			spntree.at(ed.node1).push_back(ed.node2);
			spntree.at(ed.node2).push_back(ed.node1);
			edgeset.erase(edgeset.begin() + index);
			count++; cost += ed.weight;
			index = 0; continue;
		}
		index++;
	}
	return cost;


}

int main(void) {

	int size, temp,cost,edgenum=0;
	Edge etemp;
	vector<Edge> edgeset;
	cin >> size;
	Graph spntree(size);
	for (int i = 0; i < size; i++) {
		for (int j = 0; j < size; j++) {
			cin >> temp;
			if (i < j) {
				if (temp != -1) {
					edgenum++;
					etemp.numset(i, j, temp);
					edgeset.push_back(etemp);
				}
			}
		}
	}

	cost=kruskal(spntree, edgeset);
	cout << cost << endl;
}