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++: // Program Name: Minimum Spanning Tree
// Written by: by_sknight
// Date: 2019/4/27
#include <bits/stdc++.h>
using namespace std;
#define MAX 105
#define INFTY INT_MAX
#define WHITE 0
#define GRAY 1
#define BLACK 2

int n, G[MAX][MAX];

int prim() {
    int d[MAX], p[MAX], color[MAX];
    int u, minv;
    for (int i = 0; i < n; i++) {
        d[i] = INFTY;
        p[i] = -1;
        color[i] = WHITE;
    }

    d[0] = 0;

    while (1) {
        u = -1;
        minv = INFTY;
        for (int i = 0; i < n; i++) {
            if (color[i] != BLACK && d[i] < minv) {
                minv = d[i];
                u = i;
            }
        }
        if (u == -1) 
            break;
        color[u] = BLACK;
        for (int v = 0; v < n; v++) {
            if (color[v] != BLACK && G[u][v] < d[v]) {
                d[v] = G[u][v];
                p[v] = u;
                color[v] = GRAY;
            }
        }
    }
    int sum = 0;
    for (int i = 0; i < n; i++) {
        if (p[i] != -1) {
            sum += d[i];
        }
    }
    return sum;
}

int main(void) {
    cin >> n;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            int k;
            cin >> k;
            G[i][j] = (k == -1 ? INFTY : k);
        }
    }

    int sum = prim();

    cout << sum << endl;
}

C++: #include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#include<functional>
#include<map>
#include<cstring>
#define p pair<int,int>
#define P pair<int,p>
using namespace std;

vector<P>s;
int Par[100], Rank[100];
int find(int a) {
	if (Par[a] == a)return a;
	return Par[a] = find(Par[a]);
}
void unite(int a, int b) {
	a = find(a); b = find(b);
	if (Rank[a] > Rank[b]) {
		Par[b] = a;
	}
	else if (Rank[a] < Rank[b]) {
		Par[a] = b;
	}
	else {
		Rank[a]++;
		Par[b] = a;
	}
}
signed main() {
	int a; cin >> a;
	for (int b = 0; b < a; b++)Par[b] = b;
	for (int b = 0; b < a; b++) {
		for (int c = 0; c < a; c++) {
			int d; scanf("%d", &d);
			if (d != -1 && b <= c) {
				s.push_back(P(d,p(b,c)));
			}
		}
	}
	sort(s.begin(), s.end());
	int cost = 0;
	for (P i : s) {
		if (find(i.second.first) != find(i.second.second)) {
			cost += i.first;
			unite(i.second.first, i.second.second);
		}
	}
	cout << cost << endl;
}

C++: #include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <utility>
#include <queue>
#include <climits>
using namespace std;

int prim(vector<vector<int> >& A)
{
    int sum = 0;
    bool on[A.size()];
    for (int i = 0; i < A.size(); i++) {
        on[i] = false;
    }

    // pair<cost, node>
    priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > > PQ;
    PQ.push(make_pair(0, 0));
    while (!PQ.empty()) {
        pair<int, int> edge = PQ.top();
        PQ.pop();
        if (on[edge.second]) continue;
        on[edge.second] = true;
        sum += edge.first;
        for (int i = 0; i < A.size(); i++) {
            if (on[i] || A[edge.second][i] < 0) continue;
            PQ.push(make_pair(A[edge.second][i], i));
        }
    }
    return sum;
}

int main()
{
    int n;
    cin >> n;
    vector<vector<int> > A(n, vector<int>(n));
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            cin >> A[i][j];
        }
    }
    cout << prim(A) << endl;
    return 0;
}

C++: #include<iostream>
#include<vector>
#define INF (1<<21)
#define WHITE 1
#define GRAY 2
#define BLACK 3
#define neg -1
using namespace std;

int n;
int m[100][100];
vector<int> color;
vector<int> g;
vector<int> p;

int prim(){
  int mincost, u;
  
  for(int i = 0; i < n; i++){
    g[i] = INF;
    color[i] = WHITE;
    p[i] = neg;
  }

  g[0] = 0;

  while(1){
    mincost = INF;
    u = -1;

    for(int i = 0; i < n; i++){
      if(mincost > g[i] && color[i] != BLACK){
	u = i;
	mincost = g[i];
      }
    }

    if(u == -1) break;

    color[u] = BLACK;
    
    for(int i = 0; i < n; i++){
      if(color[i] != BLACK && m[u][i] != INF){
	if(g[i] > m[u][i]){
	  g[i] = m[u][i];
	  p[i] = u;
	  color[i] = GRAY;
	}
      }
    }
  }

  int sum = 0;

  for(int i = 0; i < n; i++){
    if(p[i] != neg) sum += m[i][p[i]];
  }

  return sum;
  
}

int main()
{
  cin >> n;
  color.resize(n);
  g.resize(n);
  p.resize(n);

  for(int i = 0; i < n; i++){
    for(int j = 0; j < n; j++){
    int x;
    cin >> x;

    m[i][j] = (x == -1) ? INF:x;
    
    }
  }

  cout << prim() << endl;

  return 0;

}

C++: #include<bits/stdc++.h>
#define rep(i, n) for(int i = 0; i < (n); ++i)

using namespace std;
using ll = long long;
using v1 = vector<int>;
using vl = vector<long long>;
using v2 = vector<vector<int>>;
using v3 = vector<vector<char>>;

int n;
int m[101][101];
int d[101];
int color[101];
int p[101];

int prim(){
	rep(i, n){
		d[i] = 1e5;
		color[i] = 0;
	}
	d[0] = 0;
	p[0] = -1;
	while(1){
		int mincost = 1e5;
		int u = -1;
		rep(i, n){
			if(d[i] < mincost && color[i] != -1){
				mincost = d[i];
				u = i;
			}
		}
		color[u] = -1;
		if(u == -1) break;
		rep(v, n){
			if(color[v] != -1 && m[u][v] != -1){
				if(m[u][v] < d[v]){
					d[v] = m[u][v];
					p[v] = u;
					color[v] = 1;
				}
			}
		}
	}
	int sum = 0;
	rep(i, n){
		sum += d[i];
	}
	return sum;
}

int main(){
	cin >> n;
	rep(i, n){
		rep(j, n){
			cin >> m[i][j];
		}
	}
	cout << prim() << endl;
}