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 <string>
#include <algorithm>
#include <functional>
#include <vector>
#include <utility>
#include <cstring>
#include <iomanip>
#include <numeric>
#include <cmath>
#include <queue>
#define MAX_N 110
using namespace std;
typedef long long ll;
const int INF = 1<<30; 
const int MOD = 1e9 + 7;
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};
int cost[MAX_N][MAX_N];
int mincost[MAX_N];
bool used[MAX_N];
int n;
int prim()
{
    int res = 0;
    mincost[0] = 0;
    while(true)
    {
        int v = -1;
        for(int u = 0; u < n; u++)
        {
            if(!used[u] && (v == -1 || mincost[u] < mincost[v])) v = u;
        }
        if(v == -1) break;
        used[v] = true;
        res += mincost[v];
        for(int u = 0; u < n; u++) mincost[u] = min(mincost[u], cost[v][u]);
    }
    return res;
}
int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    cin >> n;
    memset(used, false, sizeof(used));
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < n; j++)
        {
            cin >> cost[i][j];
            if(cost[i][j] == -1) cost[i][j] = INF;
        }
        mincost[i] = INF;
    }
    int ans = prim();
    cout << ans << endl;
    return 0;
}

C++: #include <iostream>
#include <queue>
#include <algorithm>
#include <vector>


using namespace std;


using ll = long long;


const ll MAX = 100;


ll adj[MAX][MAX];
ll d[MAX] = {};
bool finished[MAX] = {};
ll n;


void msp()
{
    priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> pq;
    pq.push(make_pair(0, 0));

    while (!pq.empty()) {
        auto p = pq.top(); pq.pop();
        if (finished[p.second])
            continue;

        for (ll j = 0; j < n; j++) {
            if (adj[p.second][j] == -1 || finished[j])
                continue;
            pq.push(make_pair(adj[p.second][j], j));
        }
        // cout << p.second << ": " << p.first << endl;
        d[p.second] = p.first;
        finished[p.second] = true;
    }
}


int main()
{
    cin >> n;
    for (ll i = 0; i < n; i++) {
        for (ll j = 0;j < n; j++) {
            cin >> adj[i][j];
        }
    }

    msp();

    ll acc = 0;
    for (ll i = 0; i < n; i++)
        acc += d[i];
    cout << acc << endl;
}

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

const int inf = 100000;
const int NIL = -1;

int G[100][100], d[100];
bool color[100];

int main(){
    int n, minc, tag, ans = 0;
    cin >> n;
    for(int i = 0; i < n; i++){
        d[i] = inf;
        color[i] = false;
        for(int j = 0; j < n; j++) cin >> G[i][j];
    }
    d[0] = 0;
    while(true){
        minc = inf;
        for(int i = 0; i < n; i++){
            if(color[i] == false && d[i] < minc){
                minc = d[i];
                tag = i;
            }
        }
        if(minc == inf) break;
        ans += minc;
        color[tag] = true;
        for(int v = 0; v < n; v++){
            if(G[tag][v] != -1){
                if(color[v] == false && G[tag][v] < d[v]){
                    d[v] = G[tag][v];
                }
            }
        }
    }
    cout << ans << endl;
    return 0;
}

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

#define NIL -2
#define INF 1000000001

using namespace std;

void prim(const vector<vector<int> > &link_weight,vector<int> &color, vector<int> &mincost_array,vector<int> &parents){
    int n = mincost_array.size();
    int mincost;
    int u;
    while(true){
        mincost = INF;
        for(int i = 0;i < n;i++){
            if(color[i] != 1 && mincost_array[i] < mincost){
                mincost = mincost_array[i];
                u = i;      
            }
        }
        if(mincost == INF) break;
        color[u] = 1;

        for(int v = 0;v < n;v++){
            if(color[v] != 1 && link_weight[u][v] < INF){
                if(link_weight[u][v] < mincost_array[v]){
                    mincost_array[v] = link_weight[u][v];
                    parents[v] = u;
                    color[v] = 0;
                }
            }
        }
    }
}

int main(){
    int mat_size;
    int temp;
    int cost = 0;

    scanf("%d",&mat_size);
    vector<vector<int> > link_weight(mat_size,vector<int>(mat_size,INF));
    vector<int> color(mat_size,-1);
    vector<int> mincost_array(mat_size,INF);
    vector<int> parents(mat_size,-1);

    for(int i = 0;i < mat_size;i++){
        for(int j = 0;j < mat_size;j++){
            scanf("%d",&temp);
            if(temp != -1) link_weight[i][j] = temp;
        }
    }
    mincost_array[0] = 0;
    prim(link_weight,color,mincost_array,parents);
    for(int i = 0;i < mat_size;i++) cost += mincost_array[i];
    printf("%d\n",cost);

    return 0;   
}