Go: package main

import (
	"sort"
	"bufio"
	"os"
	"fmt"
	"strconv"
)

// Define disjoint-set forests

type vertexNo int

type vertex struct {
	number vertexNo
	rank int
	parent *vertex
}

func makeSet(number vertexNo) vertex {
	return vertex {
		number: number,
		rank: 0,
		parent: nil,
	}
}

func union(x, y *vertex) *vertex {
	return link(findSet(x), findSet(y))
}

func link(x, y *vertex) *vertex {
	if x.rank > y.rank {
		y.parent = x
		return x
	} else {
		x.parent = y
		if x.rank == y.rank {
			y.rank += 1
		}
		return y
	}
}

func findSet(v *vertex) *vertex {
	if v.parent == nil {
		return v
	}
	v.parent = findSet(v.parent)
	return v.parent
}

// Function to calculate minimum spanning tree

type matrix [][]int

type edge struct {
	one vertexNo
	two vertexNo
	weight int
}

const(
	MAX_VERTICES = 100
)

func newEdgesFromMatrix(matrix matrix) []edge {
	edges := make([]edge, 0, MAX_VERTICES * MAX_VERTICES)

	for i := 0; i < len(matrix); i++ {
		row := matrix[i]
		for j := i; j < len(row); j++ {
			if row[j] >= 0 {
				edge := edge{one: vertexNo(i+1), two: vertexNo(j+1), weight: row[j]}
				edges = append(edges, edge)
			}
		}
	}

	return edges
}

func solve(matrix matrix) int {
	nVertices := len(matrix)

	// initialize vertices
	vertices := make([]vertex, nVertices)
	for i := 0; i < nVertices; i++ {
		vertices[i] = makeSet(vertexNo(i+1))
	}

	// prepare edges sorted in the ascending order of weight
	edges := newEdgesFromMatrix(matrix)
	sort.Slice(edges, func(i, j int) bool {
		return edges[i].weight < edges[j].weight
	})

	unionCount := 0
	weightSum := 0
	for unionCount < nVertices-1 {
		for i := 0; i < len(edges); i++ {
			edge := edges[i]
			v1 := &vertices[edge.one-1]
			v2 := &vertices[edge.two-1]
			rep1 := findSet(v1)
			rep2 := findSet(v2)
			if rep1.number == rep2.number {
				continue
			}
			union(rep1, rep2)
			edges = append(edges[0:i], edges[i+1:]...)
			weightSum += edge.weight
			unionCount++
			break
		}
	}

	return weightSum
}

// main

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	n, err := nextInt(sc)
	if err != nil {
		panic(err)
	}

	matrix := parseMatrix(n, sc)

	edges := newEdgesFromMatrix(matrix)
	sort.Slice(edges, func(i, j int) bool {
		return edges[i].weight < edges[j].weight
	})

	fmt.Println(solve(matrix))
}

func parseMatrix(n int, sc *bufio.Scanner) [][]int {
	matrix := make([][]int, n)
	for i := 0; i < n; i++ {
		matrix[i] = make([]int, n)
		for j := 0; j < n; j++ {
			x, err := nextInt(sc)
			if err != nil {
				panic(err)
			}
			matrix[i][j] = x
		}
	}
	return matrix
}

func nextText(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

func nextInt(sc *bufio.Scanner) (int, error) {
	return strconv.Atoi(nextText(sc))
}

C++: #include <stdio.h>
#include <stdint.h>
#include <iostream>
#include <fstream>
#include <cassert>
#include <climits>

#include <vector>
#include <string>
using namespace std;

#define DEBUG (0)

#if DEBUG
#define DPRINTF(...) do { \
    printf("D(L%d) %s: ", __LINE__, __func__); \
    printf(__VA_ARGS__); \
  } while (false)
#else
#define DPRINTF(...)
#endif // DEBUG

/*
   recursive + dfs
*/

const uint8_t kMaxN = 100;
int16_t gWeightOfVij[kMaxN][kMaxN];
int gNumOfVertices;


void printAllWeights()
{
  DPRINTF("Number of vertices %d\n", gNumOfVertices);

  for (int i = 0; i < gNumOfVertices; i++)
  {
    for (int j = 0; j < gNumOfVertices; j++)
    {
      if (j == 0)  DPRINTF("");
      printf(" %d", gWeightOfVij[i][j]);
    }
    cout << endl;
  }
}


vector<int16_t> getConnectedVs(int16_t v, const bool visited[])
{
  vector<int16_t> vertices;

  for (int i = 0; i < gNumOfVertices; i++)
  {
    if (gWeightOfVij[v][i] > 0 && !visited[i])
      vertices.push_back(i);
  }

#if 0
  if (vertices.size() > 0)
  {
    for (int i = 0; i < vertices.size(); i++)
    {
      if (i == 0)
        DPRINTF("v from %d to :", v);

      printf(" %d", vertices[i]);
    }

    cout << endl;
  }
#endif

  return vertices;
}


int minSpanningTreeRecursive(int16_t currentV, const bool visited[], string path)
{
  assert(0 <= currentV && currentV < gNumOfVertices);

  bool updatedVisited[gNumOfVertices];

  for (int i = 0; i < gNumOfVertices; i++)
    updatedVisited[i] = visited[i];

  updatedVisited[currentV] = true;

  // This function ensures all of elements are unvisited
  vector<int16_t> nextVs = getConnectedVs(currentV, updatedVisited);

  if (nextVs.size() == 0)
    return 0;

  //string minPath;
  int minV = -1;
  int minScore = INT_MAX;
  string currentPath = to_string(currentV);

  if (path.size())
    currentPath = path + " > " + currentPath;

  for (int i = 0; i < nextVs.size(); i++)
  {
    const int16_t nextV = nextVs[i];
    const int score = gWeightOfVij[currentV][nextV] + minSpanningTreeRecursive(nextV, updatedVisited, currentPath);

    if (minScore > score)
    {
      minScore = score;
      minV = nextV;
    }

  }

  {
    string minPath = "[" + to_string(currentV) + "] > " + to_string(minV);

    if (path.size())
      minPath = path + " > " + minPath;

    DPRINTF("score %d (currentV %d)\n", minScore, currentV);
    DPRINTF("  %s\n", minPath.c_str());
  }

  return minScore;
}


int minSpanningTree()
{
  int16_t startV = 1;
  bool visited[gNumOfVertices];

  for (int i = 0; i < gNumOfVertices; i++)
    visited[i] = false;

  return minSpanningTreeRecursive(startV, visited, "");
}


int main()
{
  cin >> gNumOfVertices; cin.ignore();

  DPRINTF("Number of vertices %d\n", gNumOfVertices);

  for (int i = 0; i < gNumOfVertices; i++)
  {
    for (int j = 0; j < gNumOfVertices; j++)
      cin >> gWeightOfVij[i][j];

    cin.ignore();
  }

#if DEBUG
  DPRINTF("Number of vertices %d\n", gNumOfVertices);
  printAllWeights();
#endif // DEBUG

  cout << minSpanningTree() << endl;

  return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <cstring>
#define MAX 105
#define INF 1000000
using namespace std;

int n;
int M[MAX][MAX];
int d[MAX];
int p[MAX];
int color[MAX];//0 表示未进入 -1 表示在之中 1 表示已出去

int prim() {
	int u,min,i,j;
	d[0]=0;
	p[0]=-1;
	while(1) {
		min=INF;
		u=-1;
		for(i=0; i<n; i++) {
			if(min>d[i] && color[i]!=1) {
				u=i;
				min=d[i];
			}
		}
		if(u==-1) break;
		color[u]=1;
		for(j=0; j<n; j++) {
			if(color[j]!=1 && M[u][j]!=INF) {
				if(d[j]>M[u][j]) {
					d[j]=M[u][j];
					color[j]=-1;
					p[j]=u;
				}
			}
		}
	}
	int sum=0;
	for(i=0; i<n; i++) {
		if(p[i] != -1) sum+=M[i][p[i]];
	}
	return sum;
}

int main() {
	int i,j,a;
	memset(color,0,sizeof(color));
	memset(d,INF,sizeof(d));
	memset(p,-1,sizeof(p));
	scanf("%d",&n);
	for(i=0; i<n; i++) {
		for(j=0; j<n; j++) {
			scanf("%d",&a);
			if(a==-1) a=INF;
			M[i][j]=a;
		}
	}
	cout<<prim()<<endl;
	return 0;
}

C++: #include <cstdio>
#include <iostream>
#include <queue>
#include <utility>

using namespace std;

const int NIL = 2001;
const int MAX = 100;
int G[MAX][MAX];
int color[MAX];
int W[MAX];
int n;

const int WHITE = 0;
const int GRAY = 1;
const int BLACK = 2;

void MST() {
	priority_queue<pair<int, int>> PQ;
	PQ.push(make_pair(0, 0));
	W[0] = 0;
	color[0] = BLACK;
	while (!PQ.empty()) {
		int u = PQ.top().second;
		PQ.pop();
		color[u] = BLACK;
		for (int i = 0; i < n; ++i) {
			if (G[u][i] != -1 && G[u][i] < W[i] && color[i] != BLACK) {
				PQ.push(make_pair(G[u][i] * -1, i));
				W[i] = G[u][i];
//				cout << u << ":" << i << ":" << G[u][i] << endl;
			}
		}
	}
}

int main() {

	scanf("%d", &n);
	for (int i = 0; i < n; ++i) {
		W[i] = NIL;
		color[i] = WHITE;
	}

	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < n; ++j) {
			int a;
			scanf("%d", &a);
			G[i][j] = a;
		}
	}

	MST();

	cout << endl;

	int sum = 0;
	for (int i = 0; i < n; ++i) {
//		cout << i << ":" << W[i] << endl;
		sum += W[i];
	}
	cout << sum << endl;

	return 0;
}