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 <bits/stdc++.h>
using namespace std;
#define INF (1<<30)
#define MAX_N 100
typedef pair<int, int> P;

struct edge{
    int v[2], cost;
    edge(){}
    edge(int v0, int u0, int cost0){
        v[0] = v0; v[1] = u0; cost = cost0;
    }
    bool operator < (const edge &e) const {
        return cost > e.cost;
    }
};

int ans = 0, N;
vector<edge> E[MAX_N];

void prim(){
    bool seen[N];
    fill(seen, seen + N, false);
    priority_queue<edge> todo;
    seen[0] = true;
    for(int i = 0; i < E[0].size(); i++)
        todo.push(E[0][i]);
    while(!todo.empty()){
        edge e = todo.top(); todo.pop();
        if(seen[e.v[1]]) continue;
        ans += e.cost;
        seen[e.v[1]] = true;
        for(int i = 0; i < E[e.v[1]].size(); i++)
            if(!seen[E[e.v[1]][i].v[1]]) todo.push(E[e.v[1]][i]);
    }
}

int main(){
    cin >> N;
    for(int i = 0; i < N; i++)
        for(int j = 0; j < N; j++){
            int cost;
            cin >> cost;
            if(i < j && cost != -1){
                E[i].push_back(edge(i, j, cost));
                E[j].push_back(edge(j, i, cost));
            }
        }
    prim();
    cout << ans << endl;
    return 0;
}

C++: #include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <utility>
using namespace std;
//_____
#define pb push_back
#define fi first
#define se second
#define mkp make_pair
#define rep(i,n) for(int i=0; i<(n); ++i)
#define reps(i,f,n) for(int i=(f); i<=(n); ++i)
#define err(...) fprintf(stderr, __VA_ARGS__)
#define show(x) cerr << #x << ':' << x << endl
#define outl(x) cout << (x) << '\n'
#define FAST() cin.tie(0), ios::sync_with_stdio(false)
typedef pair<int,int> pii;
#define INF 0x3f3f3f3f
//_______
vector<pii> G[102];
bool used[102];

int prim()
{
   priority_queue < pii, vector<pii>, greater<pii> > pq;
   pq.push( mkp(0,0) );
   int sum = 0;

   while( !pq.empty() ){
      const pii  tmp = pq.top(); pq.pop();
      if( used[ tmp.se ]++ )continue;
      sum += tmp.fi;

      rep(j, G[tmp.se].size() )
         pq.push( mkp( G[tmp.se][j].fi, G[tmp.se][j].se ) );
   }
   return (sum);
}

signed main()
{
   FAST();
   int N; cin >> N;

   rep(i, N)rep(j, N){
      int v; cin >> v;
      if( v != -1 ) G[i].pb( mkp(v,j) );
   }
   outl( prim() );
}

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

std::vector<int> color;
std::vector<std::vector<int>> M;
std::vector<int> d;
std::vector<int> p;
int INF = 1001001001;
int sum = 0;

int prim(int n){
    for(int i = 0; i < n; i++){
        color[i] = 0;
        d[i] = INF;
    }

    d[0] = 0;
    p[0] = -1;

    while(true){
        int u;
        int mincost = INF;
        for(int i = 0; i < n; i++){
            if(color[i] != 1 && d[i] < mincost){
                mincost = d[i];
                u = i;
            }
        }

        if(mincost == INF){
            break;
        }

        color[u] = 1;

        for(int i = 0; i < n; i++){
            if(color[i] != 1 && M[u][i] != -1){
                if(M[u][i] < d[i]){
                    d[i] = M[u][i];
                }
            }
        }

    }
    
    for(int i = 0; i < d.size(); i++){
        sum += d[i];
    }

    return sum;
}

int main(void){
    int n;
    std::cin >> n;

    color.resize(n);
    M.resize(n, std::vector<int>(n));
    d.resize(n);
    p.resize(n);

    for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++){
            std:: cin >> M[i][j];
        }
    }
    
    
    

    std::cout << prim(n) << std::endl;

    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

int n;
int a[101][101];

set<int>s,t;

int main(){
	s = set<int>();
	t = set<int>();
	scanf("%d",&n);
	for(int i = 0; i < n; i++){
		for(int j = 0; j < n; j++){
			scanf("%d",&a[i][j]);
		}
		if(i == 0){
			t.insert(i);
		}
		else{
			s.insert(i);
		}
	}
	int ans = 0;
	while(!s.empty()){
		int min_s=-1,min_t=-1,min_w=10000000;
		for(set<int>::iterator sit = s.begin(); sit != s.end(); sit++){
			for(set<int>::iterator tit = t.begin(); tit != t.end(); tit++){
				if(a[*sit][*tit] != -1 && a[*sit][*tit] < min_w){
					min_s = *sit;
					min_t = *tit;
					min_w = a[*sit][*tit];
				}
			}
		}
		s.erase(min_s);
		t.insert(min_s);
		ans += min_w;
	}
	printf("%d\n",ans);
	
	return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef float fl;
typedef long long ll;

static const int MAX = 100;
static const int INF = (1<<21);
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;

int n, M[MAX][MAX];

int prim() {
    int u, minv;
    int d[MAX], p[MAX], color[MAX];

    rep(i,n) {
        d[i] = INF;
        p[i] = -1;
        color[i] = WHITE;
    }
    d[0] = 0;

    while(1){
        minv = INF;
        u = -1;
        rep(i,n) {
            if( minv > d[i] && color[i] != BLACK) {
                u = i;
                minv = d[i];
            }
        }
        if (u == -1) break;
        color[u] = BLACK;
        rep(v,n) {
            if (color[v] != BLACK && M[u][v] != INF) {
                if ( d[v] > M[u][v]){
                    d[v] = M[u][v];
                    p[v] = u;
                    color[v] = GRAY;
                }
            }
        }
    }
    int sum = 0;
    rep(i,n) {
        if (p[i] != -1) sum += M[i][p[i]];
    }
    return sum;
}

int main(){
    cin >> n;
    rep(i,n){
        rep(j,n){
            int e; cin >> e;
            M[i][j] = (e == -1)? INF : e;
        }
    } 
    cout << prim() << endl;
    return 0;
}