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++: #define _USE_MATH_DEFINES

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>

using namespace std;

typedef pair<long long int, long long int> P;
long long int INF = 1e18;
#define MAX_V 200

long long int cost[MAX_V][MAX_V]; // cost[u][v] は頂点uからvへのコスト
long long int mincost[MAX_V];
bool used[MAX_V];
int V;// Vは頂点数

// 最小全域木であり、O(V^2)

// 最初にすべての頂点に対するcostを計算しておくこと

long long int prim(){
	
	for(int i = 0; i < V; i++){
		mincost[i] = INF;
		used[i] = false;
	}
	
	mincost[0] = 0;
	
	long long int total = 0;
	
	while(true){
		
		int add_v = -1;
		
		for(int u = 0; u < V; u++){
			if(!used[u] && (add_v == -1 || mincost[u] < mincost[add_v])){
				add_v = u;
			}
		} // 追加する頂点を決定
		
		if(add_v == -1){
			break;
		} // 更新する頂点がない場合、終了
		used[add_v] = true;
		total += mincost[add_v];
		
		for(int u = 0; u < V; u++){
			mincost[u] = min(mincost[u], cost[add_v][u]);
		} // 集合の外の頂点への最小コストを更新
	}
	return total;
}

int main(){
	
	long long int ans;
	
	cin >> V;
	
	for(int i = 0; i < V; i++){
		for(int j = 0; j < V; j++){
			cin >> cost[i][j];
			if(cost[i][j] == -1){
				cost[i][j] = INF;
			}
		}
	}
	
	ans = prim();
	cout << ans << endl;
	
	return 0;
}

C++: #include <iostream>
#include <limits.h>
using namespace std;

int main(void)
{
	int n, now = 0, flag[101];
	long ans = 0, an[100][100], min[100];

	cin >> n;

	for(int i = 0; i < n; i++) {
		for(int j = 0; j < n; j++) {
			cin >> an[i][j];
		}
		min[i] = LONG_MAX - 1;
		flag[i] = 0;
	}

	/* ???????則?????????即?????属????????? */
	do {
		flag[now] = 1;
		flag[100] = 0;
/*
		cout << "\n??? " << now << " ????則???????????????????" << endl;
		cout << "?????贈?????? : ";
		for(int i = 0; i < n; i++) {
			if(flag[i] == 1) {
				cout << i << " ";
			}
		}
		cout << endl;
		cout << "?????即???????属?????????則" << endl;
		for(int i = 0; i < n; i++) {
			if(flag[i] == 0) {
				cout << "??? " << i << " : " << min[i] << endl;
			}
		}
*/
		/* min?????卒??属 */
		for(int j = 0; j < n; j++) {
			if(an[now][j] != -1 && an[now][j] < min[j] && flag[j] == 0) {
/*				cout << "??? " << j <<" ???????属?????????卒??属??????????????? : "<< min[j] << " ??? " << an[now][j] << endl; */
				min[j] = an[now][j];
			}
		}
/*
		cout << "?????即???????属?????????則" << endl;
		for(int i = 0; i < n; i++) {
			if(flag[i] == 0) {
				cout << "??? " << i << " : " << min[i] << endl;
			}
		}
*/
		/* ?測??????蔵?卒蔵???min????属???? */
		int key = LONG_MAX - 1, key_j;

		for(int j = 0; j < n; j++) {
			if(flag[j] == 0 && min[j] < key) {
				key = min[j];
				key_j = j;
			}
		}
/*
		cout << "?則???????????賊??????????????????? : ??? " << key_j << endl;
*/
		/* ?則?????????? */
		now = key_j;
		ans += key;

		/* ??????????????贈????????? */
		for(int i = 0; i < n; i++) {
			if(flag[i] == 1) {
				flag[100]++;
			}
		}
	
	}while(flag[100] != n - 1);

	cout << ans << endl;

	return 0;
}

C++: #include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <climits>
#include <map>
#include <list>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <sstream>
#include <fstream>
#include <iostream>
#include <algorithm>
using namespace std;

int main() {
	int n;
	scanf("%d", &n);
	
	bool added[n];
	int a[n][n];
	for(int i = 0; i < n; i++){
		for(int j = 0; j < n; j++){
			int buf;
			scanf("%d", &buf);
			a[i][j] = buf==-1 ? INT_MAX : buf;
		}
		added[i] = false;
	}
	added[0] = true;
	
	long total = 0;
	for(;114514;){
		bool end = true;
		int minCost = INT_MAX, pi, pj;
		for(int i = 0; i < n; i++){
			if(!added[i]){
				end = false;
				continue;
			}
			for(int j = 0; j < n; j++){
				if(minCost > a[i][j]){
					minCost = a[pi = i][pj = j];
				}
			}
		}
		if(end) break;
		a[pj][pi] = a[pi][pj] = INT_MAX;
		if(!added[pi] || !added[pj]){
			added[pi] = added[pj] = true;
			total += minCost;
		}
	}
	
	printf("%ld\n", total);
	return 0;
}

C++: #include<bits/stdc++.h>

using namespace std;
#define rep(i,n) for(int i=0;i<(n);i++)

typedef long long ll;
typedef pair<int, int> P;

#define MAX 100005
#define INF INT_MAX

class Tree
{
    vector<int> rank, id; 
public:
    Tree(int n) : rank(n), id(n) {
        rep(i,n) id[i] = i;
    }
    int getId(int n) {
        if (n != id[n]) id[n] = getId(id[n]);
        return id[n];
    }
    void uniteTree(int u, int v) {
        u = getId(u);
        v = getId(v);
        if (rank[u] > rank[v]) swap(u,v);
        if (rank[u] == rank[v]) rank[v]++;
        id[u] = v;
    }

};

struct Edge
{
    int u, v;
    int cost;
    bool operator<(const Edge& e) const { return this->cost < e.cost; }
};

int main(int, char**)
{
    int n;
    cin >> n;

    vector<Edge> E;
    int tmp;
    rep(i,n) rep(j,n) {
        cin >> tmp;
        if (i >= j) continue;
        if (tmp == -1) continue;
        E.push_back({i,j,tmp});
    }

    sort(E.begin(), E.end());

    Tree tree(n);
    ll ans = 0;
    rep(i,E.size()) {
        int u = E[i].u;
        int v = E[i].v;
        if (tree.getId(u) != tree.getId(v)) {
            tree.uniteTree(u,v);
            ans += E[i].cost;
        }
    }

    cout << ans << endl;

    return 0;
}