Go: package main

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

const (
	MAX   = 100
	INFTY = 1 << 21
	NIL   = -1
)

const (
	WHITE = iota
	GRAY
	BLACK
)

type Graph [MAX][MAX]int

func (g *Graph) prim(n int) int {
	var (
		u, minv     = 0, 0
		d, p, color [MAX]int
	)

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

	for {
		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 && g[u][v] != INFTY {
				if d[v] > g[u][v] {
					d[v] = g[u][v]
					p[v] = u
					color[v] = GRAY
				}
			}
		}
	}

	sum := 0
	for i := 0; i < n; i++ {
		if p[i] != -1 {
			sum += g[i][p[i]]
		}
	}

	return sum
}

func answer(reader io.Reader) string {
	sc := bufio.NewScanner(reader)
	sc.Split(bufio.ScanWords)

	sc.Scan()
	n, _ := strconv.Atoi(sc.Text())

	var g Graph
	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			sc.Scan()
			e, _ := strconv.Atoi(sc.Text())
			if e == -1 {
				g[i][j] = INFTY
				continue
			}
			g[i][j] = e
		}
	}

	return fmt.Sprint(g.prim(n))
}

func main() {
	fmt.Println(answer(os.Stdin))
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

struct edge {int u, v, cost;};

int n;
vector<edge> G;

bool comp(const edge& e1, const edge& e2) {
    return e1.cost < e2.cost;
}

class UnionFind {
 public:
    int g_num;
    vector<int> rank, par;

    UnionFind() {}
    UnionFind(int size) {
        rank.resize(size, 0);
        par.resize(size, 0);
        for (int i = 0; i < size; i++) {
            par[i] = i;
            rank[i] = 0;
        }
        g_num = size;
    }

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

    void unite(int x, int y) {
        x = find(x);
        y = find(y);
        if (x == y) return;

        g_num--;
        if (rank[x] > rank[y]) {
            par[y] = x;
        } else {
            par[x] = y;
            if (rank[x] == rank[y]) rank[y]++;
        }
    }

    int find(int x) {
        if (x == par[x]) return x;
        return par[x] = find(par[x]);
    }

    int group_num() {
        return g_num;
    }
};

int prim() {
    sort(G.begin(), G.end(), comp);
    UnionFind uf(n);
    int res = 0;
    for (auto e: G) {
        if (!uf.same(e.u, e.v)) {
            uf.unite(e.u, e.v);
            res += e.cost;
        }
    }
    return res;
}

int main(void) {
    cin >> n;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            int cost;
            cin >> cost;
            if (cost == -1) continue;
            G.push_back(edge {i, j, cost});
        }
    }
    int ans = prim();
    cout << ans << endl;
    return 0;
}

C++: //============================================================================
// Name        : MinimumSpanningTree.cpp
// Author      : Ken
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <vector>
#include <set>
using namespace std;

struct Edge;

struct Node{
	const int id;
	vector<int> adjEdgeIds;
};

struct Edge{
	int id;
	int weight;
	int startNodeId;
	int endNodeId;
};

class Graph{
public:
	int MAX_WEIGHT=2000;
	vector<Node> nodeList;
	vector<Edge> edgeList;

	Graph():nodeList(),edgeList(){}

	Graph(int n):nodeList(){
		for(int i=0; i<n; i++){
			vector<int> empty;
			Node node = {i, empty};
			nodeList.push_back(node);
		}
		//cout << "initialized\n";
	}

	void write(){
		cout << "***nodeList***\n";
		for(int i=0; i<nodeList.size(); i++){
			Node& node = nodeList.at(i);
			cout << "node id: " << node.id << " ";
			for(int j=0; j<node.adjEdgeIds.size(); j++){
				Edge& edge = edgeList.at(node.adjEdgeIds.at(j));
				cout << "edge id: " << edge.id << " ";
				cout << edge.startNodeId << "<->" << edge.endNodeId << " ";
			}
			cout << "\n";
		}

		cout << "***edgeList***\n";
		for(int i=0; i<edgeList.size(); i++){
			Edge& edge = edgeList.at(i);
			cout << "edge id: " << edge.id << " ";
			cout << "edge weight: " << edge.weight << " ";
			cout << edge.startNodeId << "<->" << edge.endNodeId << "\n";
		}
	}

	void addLightestEdge(set<int>& edgeIds){
		set<int> curNodeIds;
		if(edgeIds.size()==0){
			curNodeIds.insert(0);
		}else{
			set<int>::iterator p;
			for(p=edgeIds.begin(); p!=edgeIds.end(); p++){
				Edge& edge = edgeList.at(*p);
				curNodeIds.insert(edge.startNodeId);
				curNodeIds.insert(edge.endNodeId);
			}
		}

		while(true){
		set<int>::iterator p;
		int lightestWeight = MAX_WEIGHT;
		int lightestEdgeId = -1;
		for(p=curNodeIds.begin(); p!=curNodeIds.end(); p++){
			Node& node = nodeList.at(*p);

			vector<int>::iterator q;
			for(q=node.adjEdgeIds.begin(); q!=node.adjEdgeIds.end(); q++){

				if(curNodeIds.count(edgeList.at(*q).startNodeId) == 0 or
				   curNodeIds.count(edgeList.at(*q).endNodeId) == 0){

					if(edgeList.at(*q).weight < lightestWeight){
						lightestWeight = edgeList.at(*q).weight;
						lightestEdgeId = edgeList.at(*q).id;
					}
				}
			}
		}

		if(lightestEdgeId!=-1){
		    edgeIds.insert(lightestEdgeId);
		    //cout << "inserted edge id: " << lightestEdgeId << "\n";
		    //cout << "inserted edge weight: " << lightestWeight << "\n";
		    Edge& edge = edgeList.at(lightestEdgeId);
		    curNodeIds.insert(edge.startNodeId);
		    curNodeIds.insert(edge.endNodeId);
		}else{
			return;
		}
		}
	}

	int sumEdgeWeight(set<int> edgeIdSet){
		int sum = 0;
		set<int>::iterator p;
		for(p=edgeIdSet.begin(); p!=edgeIdSet.end(); p++){
			sum += edgeList.at(*p).weight;
		}

		return sum;
	}
};

void writeSet(set<int> intSet){
	set<int>::iterator p;
	for(p=intSet.begin(); p!=intSet.end(); p++){
		cout << *p << " ";
	}
}



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

	//g.write();

	for(int i=0; i<n; i++){
		for(int j=0; j<n; j++){
			int weight;
			cin >> weight;
			if(i<j and weight != -1){
				int edgeId = g.edgeList.size();
				Edge edge = {edgeId, weight, i, j};
				g.edgeList.push_back(edge);
				g.nodeList.at(i).adjEdgeIds.push_back(edgeId);
				g.nodeList.at(j).adjEdgeIds.push_back(edgeId);
			}
		}
	}

	//g.write();

	set<int> edgeIds;
	g.addLightestEdge(edgeIds);
	//writeSet(edgeIds);
	int totalWeight;
	totalWeight = g.sumEdgeWeight(edgeIds);
	cout << totalWeight << "\n";

	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;
      
    
}