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++: #include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>

#define INF 2001
#define MAX 101

using namespace std;

int MAT[MAX][MAX];
int N;

int prim(){
    int sum;
    bool complete[MAX];
    int dist[MAX];
    int parent[MAX];
    
    for (int i = 0; i < N; i++) {
        complete[i] = false;
        dist[i] = INF;
        parent[i] = -1;
    }
    
    dist[0] = 0;
    
    while (1) {
        int u = -1;
        int min = INF;
        
        for (int i = 0; i < N; i++) {
            if (min > dist[i] && !complete[i]) {
                u = i;
                min = dist[i];
            }
        }
        
        if (u == -1) break;
        
        complete[u] = true;
        
        for (int v = 0; v < N; v++) {
            if (!complete[v] && MAT[u][v] != INF) {
                if (dist[v] > MAT[u][v]) {
                    dist[v] = MAT[u][v];
                    parent[v] = u;
                }
            }
        }
    }
    
    sum = 0;
    
    for (int i = 1; i < N; i++) {
        sum += MAT[parent[i]][i];
    }
    
    return sum;
}

int main(){
    cin >> N;
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            scanf("%d",&MAT[i][j]);
            if (MAT[i][j] == -1) MAT[i][j] = INF;
        }
    }
    
    cout << prim() << endl;
    
    return 0;
}

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

#if 0
所用時間:

感想:

#endif

int N, buff, M = 0, SUM = 0, A[101][101], cost[5000], dpt[5000], dst[5000], P[100001]; //A[Y][X]
vector<pair<int, int> > edges;

void init(int N){ //木構造の初期化
  for(int i = 0; i <= N; ++i){
    P[i] = i;
  }
}

int root(int a){ //根の参照
  if(P[a] == a) return a;
  return (P[a] = root(P[a]));
}

bool is_same_set(int a, int b){ //同じ根か?
  return root(a) == root(b);
}

void unite(int a, int b){
  P[root(a)] = root(b); //aのrootをbの子にする
}

void preprocess(){//辺の前処理
  for(int r = 0; r < N; ++r){
    for(int c = r+1; c < N; ++c){
      if (A[r][c] > -1) {
        cost[M] = A[r][c];
        dpt[M] = r;
        dst[M] = c;
        M++;
      }
    }
  }

  //for(int i = 0; i < M; ++i) cout << cost[i] << endl;

  for(int i = 0; i < M; ++i){
    edges.push_back(make_pair(cost[i], i));
  }
  // for(int i = 0; i < M; ++i) cout << edges[i].first << " " << edges[i].second << endl;

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

  // for(int i = 0; i < M; ++i) cout << edges[i].first << " " << edges[i].second << endl;

}

void solve(){
  int ans = 0;
  preprocess();

  init(N);

  for(int i = 0; i < M; ++i){
    int index = edges[i].second;
    if(is_same_set(dpt[index], dst[index])) continue;
    else {
      unite(dpt[index], dst[index]);
      ans += cost[index];
    }
  }
  cout << ans << endl;
}

int main(){

cin >> N;

for(int i = 0; i < N; ++i){
  for(int j = 0; j < N; ++j){
    cin >> buff;
    A[i][j] = buff;
    }
  }

  //行列の表示
  // cout << "Matrix:" << endl;
  // for(int row=0; row < N; ++row){
  //   for(int col = 0; col < N; ++col){
  //     cout << A[row][col];
  //   }
  //   cout << endl;
  // }

  solve();
}

C++: #include "bits/stdc++.h"

using namespace std;

using ll = long long;
using ld = long double;
using P = pair<string, int>;
constexpr ld EPS = 1e-12;
constexpr int INF = numeric_limits<int>::max() / 2;
constexpr int MOD = 1e9 + 7;

template <typename T>
void printv(const vector<T> &v)
{
    int sz = v.size();
    for (int i = 0; i < sz; i++)
    {
        cout << v[i] << " \n"[i == sz - 1];
    }
}

struct UnionFind
{
    vector<int> data;
    UnionFind(int size) : data(size, -1) {}
    bool unite(int x, int y)
    {
        x = root(x);
        y = root(y);
        if (x != y)
        {
            if (data[y] < data[x])
                swap(x, y);
            data[x] += data[y];
            data[y] = x;
        }
        return x != y;
    }
    bool find(int x, int y)
    {
        return root(x) == root(y);
    }
    int root(int x)
    {
        return data[x] < 0 ? x : data[x] = root(data[x]);
    }
    int size(int x)
    {
        return -data[root(x)];
    }
};

struct Edge
{
    int from, to, cost;
    Edge(int from, int to, int cost) : from(from), to(to), cost(cost) {}
};

bool operator<(const Edge &e, const Edge &f)
{
    return e.cost > f.cost;
}

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);

    int n;
    cin >> n;
    priority_queue<Edge> es;
    int res = 0;
    UnionFind uf(n);
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            int a;
            cin >> a;
            if (a == -1)
                continue;
            if (j <= i)
                continue;
            es.push(Edge{i, j, a});
        }
    }
    while (!es.empty())
    {
        Edge e = es.top();
        es.pop();
        if (uf.find(e.from, e.to))
            continue;
        res += e.cost;
        uf.unite(e.from, e.to);
    }
    cout << res << endl;
}