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;
#define INF 1<<21
#define WHITE 0
#define GRAY 1
#define BLACK 2
#define MAX 100

int v_num;
int adjM[MAX+1][MAX+1];

int PrimsAlgorithm() 
{
    int current_vertex;
    int color[v_num+1], min_edge[v_num+1], parent[v_num+1];

    // set init state
    for (int i = 0; i < v_num; ++i)
    {
        color[i] = WHITE;
        min_edge[i] = INF;
        parent[i] = -1;
    }

    min_edge[0] = 0;

    while (true) 
    {
        int min_cost = INF;
        // move to the minimum vertex which can be accessed 
        for (int i = 0; i < v_num; ++i) 
        {
            if (color[i] != BLACK && min_edge[i] < min_cost)
            {
                min_cost = min_edge[i];
                current_vertex = i;
            }
        }

        if (min_cost == INF) break;

        color[current_vertex] = BLACK;

        for (int i = 0; i < v_num; ++i) 
        {
            if (color[i] != BLACK && adjM[current_vertex][i] != -1)
            {
                if (adjM[current_vertex][i] < min_edge[i])
                {
                    min_edge[i] = adjM[current_vertex][i];
                    parent[i] = current_vertex;
                    color[i] = GRAY;
                }
            } 
        }
    }

    int sum = 0;
    for (int i = 0; i < v_num; ++i) 
    {
        if (parent[i] != -1) sum += adjM[i][parent[i]];
    }
    return sum;
}

int main() 
{
    int v;
    cin >> v_num;
    for (int i = 0; i < v_num; ++i) 
    {
        for (int j = 0; j < v_num; ++j)
        {
            cin >> v;
            adjM[i][j] = v != -1 ? v : INF;
        }
    }
    
    cout << PrimsAlgorithm() << endl;
}

C++: #include <iostream>

using namespace std;


class Graph{
private:

  int size;
  int **graph;
  int *state;

public:

  Graph(int size){
    int i;
    int j;
    this->size = size;
    this->state = new int[size];
    this->graph = new int*[size];
    for(i = 0;i < size;i++){
      this->graph[i] = new int[size];
    }
    for(i = 0;i < this->size;i++){
	this->state[i] = 0;
      }
  }

  ~Graph(){
    int i;
    for(i = 0;i < this->size;i++){
      delete[] this->graph[i];
    }
    delete[] this->graph;
  }

  int getMinimum(){
    int i;
    int j;
    int min;
    int res = 0;

    for(i = 0;i < this->size;i++){
      min = 0;

      for(j = 1;j < this->size;j++){
	if(this->graph[i][j] != -1 && this->state[j] == 0){
	  if(this->graph[i][j] <= this->graph[i][min] 
	     ||	this->graph[i][min] == -1){
	    min = j;
	  }
	}
      }

      if(state[min] == 0){
	state[min] = 1;
	res += graph[i][min];
      }
    }
    return res;
  }

  void setValue(int x,int y,int value){
    this->graph[x][y] = value;
  }

};

int main(){

  int i;
  int j;
  int n;
  int a;

  Graph *graph;

  cin >> n;

  graph = new Graph(n);

  for(i = 0;i < n;i++){
    for(j = 0;j < n;j++){
      cin >> a;
      graph->setValue(i,j,a);
    }
  }

  cout << graph->getMinimum() << endl;

  delete graph;

  return 0;
}

C++: #include <iostream>
#define NMAX 100
#define AMAX 2001

using namespace std;

enum state{
  WHITE,
  BLACK
};

int main(){
  int A[NMAX][NMAX];
  int n; cin>>n;
  state s[NMAX];
  
  for(int r=0;r<n;r++){
    for(int c=0;c<n;c++){
      cin>>A[r][c];
      if(A[r][c]==-1) A[r][c]=AMAX;
    }
  }

  for(int i=0;i<n;i++) s[i]=WHITE;
  s[0]=BLACK;
  
  int sum=0;

  while(1){
    bool flag=true;
    for(int i=0;i<n;i++){
      if(s[i]!=BLACK) flag=false;
    }
    if(flag) break;

    int min=AMAX;
    int minr,minc;
    for(int r=0;r<n;r++){
      for(int c=0;c<n;c++){
        if(s[r]==BLACK && s[c]!=BLACK && A[r][c]<min){
          min=A[r][c];
          minr=r;minc=c;
        }
      }
    }
    s[minr]=BLACK;s[minc]=BLACK; 
    sum+=min;
  }
  cout<<sum<<endl;
}

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

int main() {
	int num;
	std::cin >> num;

	std::vector<std::vector<int> > v(num);

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

	std::vector<int> n(v.size(), 0);
	std::vector<int> color(v.size(), 0);
	int count = 0, color_count = 1;
	for (int i = 1; i <= 2000; i++) {
		for (size_t j = 0; j < v.size(); j++) {
			for (size_t k = 0; k < v.size(); k++) {
				if (v[j][k] == i && ((color[k] == 0 || color[j] == 0) || (color[k] != color[j]))) {
					if (color[k] == 0) {
						if (color[j] != 0) {
							color[k] = color[j];
						} else {
							color[k] = color[j] = color_count;
							color_count++;
						}
					} else {
						if (color[j] == 0) {
							color[j] = color[k];
						} else {
							for (int l = 0; l < color.size(); l++) {
								if (color[l] == color[j]) {
									color[l] = color[k];
								}
							}
						}
					}
					count += i;
				}
			}
		}
	}

	std::cout << count << std::endl;

	return 0;
}