Go: package main

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

//util
var sc = bufio.NewScanner(os.Stdin)

func nextStr() string {
	sc.Scan()
	return sc.Text()
}

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func printList(list []int) {
	n := len(list)
	buf := bufio.NewWriter(os.Stdout)
	for _, v := range list[0 : n-1] {
		buf.Write([]byte(fmt.Sprintf("%d ", v)))
	}
	buf.Write([]byte(fmt.Sprintf("%d\n", list[n-1])))
	buf.Flush()
}

func max(a, b int) int {
	if a > b {
		return a
	} else {
		return b
	}
}

func min(a, b int) int {
	if a > b {
		return b
	} else {
		return a
	}
}

//solve
const (
	MAX   = 100
	INFTY = (1 << 21)
	WHITE = 0
	GRAY  = 1
	BLACK = 2
)

//V: グラフの頂点全体の集合
//T: MSTに属する頂点の集合
var (
	n     int
	M     [MAX][MAX]int //重みを記録する隣接行列
	color [MAX]int
	p     [MAX]int //MSTにおける頂点の親を記録
	d     [MAX]int //Tに属する頂点と、V−Tに属する頂点を繋ぐ辺の中で、重みが最小のものを記録
)

func prim() int {
	var u, minv int
	//初期化
	for i := 0; i < n; i++ {
		d[i] = INFTY
		p[i] = -1
		color[i] = WHITE
	}

	d[0] = 0

	for {
		//頂点uを決めていく
		//つまり、Tに属する頂点と、V−Tに属する頂点を繋ぐ辺の中で、重みが最小のものを決める
		minv = INFTY
		u = -1
		for i := 0; i < n; i++ {
			//未訪問かつ、辺の重みが最小となるuを選ぶ
			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
				}
			}
		}
	}

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

func main() {
	sc.Split(bufio.ScanWords)
	n = nextInt()

	for i := 0; i < n; i++ {

		for j := 0; j < n; j++ {
			tmp := nextInt()
			if tmp == -1 {
				tmp = INFTY
			}
			M[i][j] = tmp
		}

	}

	fmt.Println(prim())

}

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