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 INFTY 2001
#define WHITE 0
#define GRAY 1
#define BLACK 2
#define N 100

// 最小全域木を構成し、重みの総和を返す関数
int prim(int n, int A[N][N]) {
  int i, vertex, min_cost, sum_weight, cost[N], color[N];
  // 初期化
  for (i = 0; i < n; i++) {
    cost[i] = INFTY;
    color[i] = WHITE;
  }
  cost[0] = 0;
  color[0] = GRAY;
  sum_weight = 0;
  
  while (1) {
    // Tに隣接する頂点の中で最小のコストを持つものを選ぶ
    vertex = -1;
    min_cost = INFTY;
    for (i = 0; i < n; i++) {
      if (color[i] == GRAY && cost[i] < min_cost) {
        min_cost = cost[i];
        vertex = i;
      }      
    }
    // T = V ならば終了
    if (vertex == -1) break;

    color[vertex] = BLACK;  // vertexをTに加える
    sum_weight += cost[vertex];

    // vertexに隣接し、Tに属していない頂点のコストを更新
    for (i = 0; i < n; i++) {
      if (color[i] != BLACK && A[vertex][i] < INFTY) {
        color[i] = GRAY;
        if (cost[i] > A[vertex][i]) cost[i] = A[vertex][i];
      }
    }
  }

  return sum_weight;
}


// 最小全域木の辺の重みの和を計算する関数
int sum_weight(int n, int parent[], int A[N][N]) {
  int sum = 0;
  
  for (int i = 1; i < n; i++) sum += A[i][parent[i]];
  
  return sum;
}

int main() {
  int i, j, n, w, A[N][N];

  cin >> n;
  // 隣接行列が与えられる
  for (i = 0; i < n; i++) {
    for (j = 0; j < n; j++) {
      cin >> w;
      if (w == -1) A[i][j] = INFTY;
      else A[i][j] = w;
    }
  }

  cout << prim(n, A) << endl;

  return 0;
}

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

using namespace std;

#define MAX_V 100
#define MAX_E 5050

int par[MAX_V],rank[MAX_V];
int V,E;

struct edge{ int u,v,cost; };
edge es[MAX_E];

bool comp(const edge &e1, const edge &e2){
  return e1.cost < e2.cost;
}

void init(int n){
  for(int i = 0 ; i < n ; i++){
    par[i] = i;
    rank[i] = 0;
  }
}

int find(int x){
  if(par[x] == x){
    return x;
  }else{
    return par[x] = find(par[x]);
  }
}

void unite(int x,int y){
  x = find(x);
  y = find(y);

  if(x == y){
    return;
  }

  if(rank[x] < rank[y]){
    par[x] = y;
  }else{
    par[y] = x;
    if(rank[x] == rank[y]){
      rank[x]++;
    } 
  }
}

bool same(int x,int y){
  return find(x) == find(y);
}

int kruskal(){
  sort(es, es + E, comp);

  init(V);
  int res = 0;

  for(int i = 0 ; i < E ; i++){
    edge e = es[i];

    if(!same(e.u,e.v)){
      unite(e.u,e.v);
      res += e.cost;
    }
  }
  return res;
}

int main(){
  int c;

  cin >> V;
  for(int i = 0 ; i < V ; i++){
    for(int j = 0 ; j < V ; j++){
      cin >> c;

      if(c == -1){
	continue;
      }

      edge e = {i,j,c};
      es[E++] = e;
    }
  }

  cout << kruskal() << endl; 

  return 0;
}

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

const int INF = (1<<30)-1;

template<typename T>
T gcd(T a, T b)
{
	return b ? gcd(b, a%b) : a;
}

template<typename T>
T lcm(T a, T b)
{
	return a / gcd(a, b) * b;
}

//(a^e)%mod
template<typename T>
T power(T a, T e, T mod)
{
	if(e == 0)
	{
		return 1;
	}
	if(e % 2 != 0)
	{
		//a^e=a^(e-1)*a
		return (power(a, e - 1, mod) * a) % mod;
	}
	T res = power(a, e / 2, mod);
	//a^e=a^(e/2)*a^(e/2)
	return (res * res) % mod;
}

template<typename T>
class Tree
{
private:
public:
	T data;
	Tree* parent = NULL;
	std::vector<Tree*> children;
	void setData(T x)
	{
		data = x;
	}
	void setParent(Tree* t)
	{
		parent = t;
	}
	void addChild(Tree* t)
	{
		children.push_back(t);
	}
	Tree(){}
	~Tree(){}
};

template<typename T>
class Graph
{
public:
	T index;
	vector<T> edge;
	void setEdge(T t)
	{
		edge.push_back(t);
	}
	Graph(){}
	~Graph(){}
};

int prim(int size, int cost[101][101])
{
	const int indexed = 1;
	const int WHITE = 0;
	const int BLACK = 1;
	const int GRAY = 2;
	int colour[size + indexed], d[size + indexed], p[size + indexed];
	int u;
	for (int i = indexed; i < size + indexed; ++i)
	{
		colour[i] = WHITE;
		d[i] = INF;
	}
	d[indexed] = 0;
	p[indexed] = -1;

	priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> PQ;
	PQ.push(make_pair(0, indexed));

	while(!PQ.empty())
	{
		u = PQ.top().second;
		PQ.pop();
		colour[u] = BLACK;
		for (int i = indexed; i < size + indexed; ++i)
		{
			if(colour[i] != BLACK && cost[u][i] != INF)
			{
				if(cost[u][i] < d[i])
				{
					PQ.push(make_pair(cost[u][i], i));
					d[i] = cost[u][i];
					p[i] = u;
					colour[i] = GRAY;
				}
			}
		}
	}
	int sum = 0;
	for (int i = indexed; i < size + indexed; ++i)
	{
		if(p[i] != -1)
		{
			sum += cost[i][p[i]];
		}
		// cout << i << " " << p[i] << endl;
	}
	return sum;
}

int main()
{
	using namespace std;
	Graph<int> G[101];
	int costn[101][101];
	int n;
	cin >> n;
	for (int i = 1; i <= n; ++i)
	{
		for (int j = 1; j <= n; ++j)
		{
			int e;
			cin >> e;
			costn[i][j] = (e == -1 ? INF : e);
		}
	}
	cout << prim(n, costn) << endl;

	return 0;
}