Go: package main

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

//util
var sc = bufio.NewScanner(os.Stdin)

func nextStr() string {
	sc.Scan()
	return sc.Text()
}

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func printList(list []int) {
	n := len(list)
	buf := bufio.NewWriter(os.Stdout)
	for _, v := range list[0 : n-1] {
		buf.Write([]byte(fmt.Sprintf("%d ", v)))
	}
	buf.Write([]byte(fmt.Sprintf("%d\n", list[n-1])))
	buf.Flush()
}

func max(a, b int) int {
	if a > b {
		return a
	} else {
		return b
	}
}

func min(a, b int) int {
	if a > b {
		return b
	} else {
		return a
	}
}

//solve
const (
	MAX   = 100
	INFTY = (1 << 21)
	WHITE = 0
	GRAY  = 1
	BLACK = 2
)

//V: グラフの頂点全体の集合
//T: MSTに属する頂点の集合
var (
	n     int
	M     [MAX][MAX]int //重みを記録する隣接行列
	color [MAX]int
	p     [MAX]int //MSTにおける頂点の親を記録
	d     [MAX]int //Tに属する頂点と、V−Tに属する頂点を繋ぐ辺の中で、重みが最小のものを記録
)

func prim() int {
	var u, minv int
	//初期化
	for i := 0; i < n; i++ {
		d[i] = INFTY
		p[i] = -1
		color[i] = WHITE
	}

	d[0] = 0

	for {
		//頂点uを決めていく
		//つまり、Tに属する頂点と、V−Tに属する頂点を繋ぐ辺の中で、重みが最小のものを決める
		minv = INFTY
		u = -1
		for i := 0; i < n; i++ {
			//未訪問かつ、辺の重みが最小となるuを選ぶ
			if minv > d[i] && color[i] != BLACK {
				u = i
				minv = d[i]
			}
		}

		if u == -1 {
			break
		}

		color[u] = BLACK
		for 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
				}
			}
		}
	}

	sum := 0
	for i := 0; i < n; i++ {
		if p[i] != -1 {
			sum += M[i][p[i]]
		}
	}
	return sum
}

func main() {
	sc.Split(bufio.ScanWords)
	n = nextInt()

	for i := 0; i < n; i++ {

		for j := 0; j < n; j++ {
			tmp := nextInt()
			if tmp == -1 {
				tmp = INFTY
			}
			M[i][j] = tmp
		}

	}

	fmt.Println(prim())

}

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

#include <cstring>
#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


const int kMaxN = 100;
const int kInvalid = INT_MAX;
int g_weightOfVij[kMaxN][kMaxN];
int g_numOfVertices;


struct Edge {
  Edge (int v0, int v1)
    : v0_(v0)
    , v1_(v1)
  { }

  int v0_;
  int v1_;
  int getWeight() { return g_weightOfVij[v0_][v1_]; }
};


struct VisitData {
  VisitData(int num)
    : numOfData_(num)
    , totalWeight_(0)
    {
      memset(visited_, 0, kMaxN);
    }

  bool visited_[kMaxN];
  int numOfData_;
  int totalWeight_;

  bool isAllVisited();

#if DEBUG
  vector<int> path_; // for history of pathes
#endif // DEBUG
};


bool VisitData::isAllVisited()
{
  for (int i = 0; i < numOfData_; i++)
    if (visited_[i] == false)
      return false;

  return true;
}


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

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


void printAllPath()
{
  DPRINTF("Number of vertices %d\n", g_numOfVertices);
  DPRINTF("$src -> $dst ($weight)\n");

  for (int i = 0; i < g_numOfVertices; i++)
  {
    for (int j = 0; j < g_numOfVertices; j++)
    {
      if (g_weightOfVij[i][j] > 0)
        DPRINTF("%d -> %d (%d)\n", i, j, g_weightOfVij[i][j]);
    }
  }
}


void getConnectedVertices(int pos, vector<int> &vertices)
{
  for (int i = 0; i < g_numOfVertices; i++)
    if (g_weightOfVij[pos][i] > 0)
      vertices.push_back(i);

#if 0
  DPRINTF("connected points from %d :", pos);
  for (int i = 0; i < vertices.size(); i++)
    printf(" %d", vertices[i]);

  cout << endl;
#endif
}


/* find unvisited vertices that are connected from the vertices already visited */
void getNextCandidates(const bool visited[kMaxN], vector<Edge> &candidates)
{
#if 0
  DPRINTF("visited:\n");
  for (int i = 0; i < g_numOfVertices; i++)
    DPRINTF("  [%d] = %d\n", i, visited[i]);
#endif

  for (int startV = 0; startV < g_numOfVertices; startV++)
  {
    if (visited[startV] == true)
    {
      // Get all connected vertices
      vector<int> vertices;
      getConnectedVertices(startV, vertices);

      // Remove vertices that are already visited
      for (int nextV : vertices)
      {
        if (visited[nextV] == false)
          candidates.push_back(Edge(startV, nextV));
      }
    }
  }

#if 0
  DPRINTF("Next candidates:\n");
  for (int i = 0; i < candidates.size(); i++)
    printf("  %d -> %d\n", candidates[i].v0_, candidates[i].v1_);
#endif
}


//int findSpanningTree(int vertex, VisitData info /* Not pointer nor reference */)
int findSpanningTree(int vertex, VisitData &info)
{

  assert(info.visited_[vertex] == false);
  info.visited_[vertex] = true;

#if DEBUG
  info.path_.push_back(vertex);
#endif

#if 1 & DEBUG
  DPRINTF("Path:");
  for (int i = 0; i < info.path_.size(); i++)
    printf(" %d", info.path_[i]);
  cout << endl;
#endif

  if (info.isAllVisited())
  {
    DPRINTF("all vertices visited. total weight %d\n", info.totalWeight_);
    return info.totalWeight_;
  }

  vector<Edge> nextPosCandidates;
  getNextCandidates(info.visited_, nextPosCandidates);

  int minWeight = kInvalid;

  for (int i = 0; i < nextPosCandidates.size(); i++)
  {
    const int startV = nextPosCandidates[i].v0_;
    const int nextV = nextPosCandidates[i].v1_;

    VisitData nextInfo = info;
    nextInfo.totalWeight_ += g_weightOfVij[startV][nextV];

    const int weight = findSpanningTree(nextV, nextInfo);

    if (weight != kInvalid && weight < minWeight)
      minWeight = weight;
  }

  DPRINTF("minWeight %d\n", minWeight);
#if 1 & DEBUG
  DPRINTF("  Path:");
  for (int i = 0; i < info.path_.size(); i++)
    printf(" %d", info.path_[i]);
  cout << endl;
#endif

  return minWeight;
}


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

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

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

    cin.ignore();
  }

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

  const int startPos = 0;
  VisitData info(g_numOfVertices);

  cout << findSpanningTree(startPos, info) << endl;

  return 0;
}

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

using namespace std;

int P[10001];

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

int root(int a) {
	if (P[a] == a)
		return a;
	return (P[a] = root(P[a]));
}

bool is_same_set(int a, int b) {
	return root(a) == root(b);
}

void unite(int a, int b) {
	P[root(a)] = root(b);
}

bool span(int n) {
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			if (!is_same_set(i, j))
				return false;
		}
	}
	return true;
}

int main() {
	int n;
	cin >> n;
	init(n);
	vector<vector<int> > map(n, vector<int>(n));
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			cin >> map[i][j];
		}
	}
	vector<pair<int, pair<int, int> > > edges;
	for (int i = 0; i < n; i++) {
		for (int j = i; j < n; j++) {
			if (map[i][j] >= 0)
				edges.push_back(pair<int, pair<int, int> >(map[i][j], pair<int, int>(i, j)));
		}
	}
	sort(edges.begin(), edges.end());
	int result = 0;
	while (!span(n)) {
		pair<int, pair<int, int> > edge = edges.front();
		edges.erase(edges.begin());
		int a = edge.second.first;
		int b = edge.second.second;
		if (!is_same_set(a, b)){
			unite(a, b);
			result += edge.first;
		}
	}
	cout << result << endl;
}