Go: package main

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

type color int

const (
	white color = iota // 未訪問
	mst                // MSTとして確定
)

// 頂点
type node struct {
	matrix   []int // 隣接行列
	minToMst int   // MSTへの重み最小値
	parent   int   // 最小値の相手
	color    color
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	n := eGetInt(scanner)

	nodes := make([]node, n)
	for i := 0; i < n; i++ {
		nodes[i].matrix = eAtoi((strings.Fields(eGetline(scanner))))
		nodes[i].parent = -1
		nodes[i].minToMst = math.MaxInt16
		nodes[i].color = white
	}

	// 初回はnode[0]がMSTに入るように,minToMstを設定
	nodes[0].minToMst = 0
	for { // 全部チェックするまで

		// どの頂点を新たにMSTに取り込むか
		min := math.MaxInt16
		u := -1
		for i := 0; i < n; i++ {
			if nodes[i].color == mst {
				continue
			}
			if nodes[i].minToMst < min {
				u = i
				min = nodes[i].minToMst
			}
		}

		if u == -1 {
			break
		}
		// MSTに追加する頂点決定
		nodes[u].color = mst

		// MSTに属していない頂点それぞれについて,MSTとの最小の重みを更新
		for i := 0; i < n; i++ {
			if nodes[i].color == mst {
				continue
			}
			if nodes[i].matrix[u] == -1 {
				// 接してない
				continue
			}
			if nodes[i].matrix[u] < nodes[i].minToMst {
				// node[u]との重みが最小
				nodes[i].minToMst = nodes[i].matrix[u]
				nodes[i].parent = u
			}
		}
	}

	sum := 0
	for _, v := range nodes {
		// node[0]はparentが入っていない
		if v.parent != -1 {
			sum += v.matrix[v.parent]
		}
	}
	fmt.Println(sum)
}

func eGetInt(scanner *bufio.Scanner) int {
	t := eAtoi(strings.Fields(eGetline(scanner)))
	return t[0]
}

func eGetline(p *bufio.Scanner) string {
	if !p.Scan() {
		if err := p.Err(); err != nil {
			log.Fatal(err.Error())
		}
		return ""
	}
	return p.Text()
}

func eAtoi(s []string) []int {
	ints := make([]int, len(s))
	var err error
	for i, e := range s {
		ints[i], err = strconv.Atoi(e)
		if err != nil {
			log.Fatal(err.Error())
		}
	}
	return ints
}

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