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>
using namespace std;

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

int n,A[MAX][MAX];

int prim(){
  int x,minv,d[MAX],p[MAX],c[MAX];

  for(int i=0;i<n;i++){
    d[i]=INF;
    p[i]=-1;
    c[i]=WHITE;
  }

  d[0]=0;
  while(1){
    minv=INF;
    x=-1;
    for(int i=0;i<n;i++){
      if(minv>d[i] && c[i]!=BRACK){
	x=i;
	minv=d[i];
      }
    }
    if(x==-1)break;
    c[x]=BRACK;
    for(int v=0;v<n;v++){
      if(c[v]!=BRACK && A[x][v]!=INF){
	if(d[v]>A[x][v]){
	  d[v]=A[x][v];
	  p[v]=x;
	  c[v]=GRAY;
	}
      }
    }
  }
  int sum=0;
  for(int i=0;i<n;i++){
    if(p[i]!=-1)sum+=A[i][p[i]];
  }

  return sum;
}

int main(){
  cin>>n;
  for(int i=0;i<n;i++){
    for(int j=0;j<n;j++){
      int e;
      cin>>e;
      A[i][j]=(e==-1)?INF:e;
    }
  }

  cout<<prim()<<endl;
  return 0;
}

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

static const int MAX =100;
static const int INFTY=numeric_limits<int>::max();

typedef enum {
    WHITE,
    GRAY,
    BLACK,
}Color;

int n,M[MAX][MAX];

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

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

    d[0]=0;

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

    return sum;
}

int main(){
    cin>>n;

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

    cout<<Prim()<<endl;

    return 0;
}

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

int n;
vector<vector<int>> a(100,vector<int>(100, -1));
vector<int> t, d(100, -1);

void prim(int start) {
    t.push_back(start);
    d[start] = 0;
    while (t.size() < n) {
        int min_v = 2001, min_i;
        rep(i, t.size()) {
            rep(j, n) {
                if (a[t[i]][j] != -1 && d[j] == -1 && a[t[i]][j] < min_v) {
                    min_v = a[t[i]][j];
                    min_i = j;
                }
            }
        }
        t.push_back(min_i);
        d[min_i] = min_v;
    }
    
}

int main() {
    scanf("%d", &n);
    rep(i, n) {
        rep(j, n) {
            scanf("%d", &a[i][j]);
        }
    }
    prim(0);
    int sum = 0;
    rep(i, n) {
        sum += d[i];
    }
    printf("%d\n", sum);
}

C++: #include<iostream>
#define INF 1<<30
using namespace std;
int main() {
  int n; cin >> n;
  int graph[101][101] = {};
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= n; ++j) cin >> graph[i][j];
  int mincost[101], flag[101] = {}, sum = 0;
  for (int i = 2; i <= n; ++i) mincost[i] = INF;
  mincost[1] = 0;
  while (1) {
    int v = -1;
    for (int i = 1; i <= n; ++i) 
      if (!flag[i] && (v == -1 || mincost[v] > mincost[i])) v = i;
    if (v == -1) break;
    flag[v] = 1;
    sum += mincost[v];
    for (int i = 1; i <= n; ++i)
      if (graph[v][i] != -1 && mincost[i] > graph[v][i]) mincost[i] = graph[v][i];
  }
  cout << sum << endl;
  return 0;
}

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

int main() {
    int i, j, n, res = 0;
    cin >> n;
    int a[n][n];

    for (i=0; i<n; i++) {
        for (j=0; j<n; j++) {
            cin >> a[i][j];
        }
    }

    vector<int> v;
    set<int> cons;
    v.push_back(0);
    cons.insert(0);
    while (v.size() != n) {
        int nearest_node = 9999999;
        int nearest_distance = 9999999;
        for (i=0; i<v.size(); i++) {
            for (j=0; j<n; j++) {
                if ((a[v[i]][j] != -1) && (a[v[i]][j] < nearest_distance) && (cons.count(j) == 0)) {
                    nearest_node = j;
                    nearest_distance = a[v[i]][j];
                }
            }
        }
        res += nearest_distance;
        v.push_back(nearest_node);
        cons.insert(nearest_node);
    }
    cout << res << endl;
    return 0;
}

C++: #include <cstdio>
#include <vector>

int main(void){
	int n, min_edge, next_node, count, sum;
	scanf("%d", &n);
	std::vector< std::vector<int> > edges;
	std::vector<int> isUsed;
	edges.resize(n);
	isUsed.resize(n);
	for(int i=0;i<n;i++){
		isUsed[i]=-1;
		edges[i].resize(n);
		for(int j=0;j<n;j++){
			scanf("%d", &edges[i][j]);
		}
	}

	min_edge=3000;
	isUsed[0] = 1;
	count = 0;
	sum=0;
	while(count<4){
		for(int i=0;i<n;i++){
			if(isUsed[i]==1){
				for(int j=0;j<n;j++){
					if (isUsed[j]==-1 && edges[i][j]>-1 && edges[i][j]<min_edge){
						min_edge = edges[i][j];
						next_node = j;
					}
				}
			}
		}
		sum += min_edge;
		isUsed[next_node]=1;
		min_edge = 3000;
		count += 1;
	}
	printf("%d\n", sum);
	return 0;
}