Go: package main

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

const BIG_COST = 3000

const (
	ST_NOT_YET = iota
	ST_JOINED
)

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

	nNodes := scanInt(sc)
	costs := make([][]int, nNodes)

	for r := 0; r < nNodes; r++ {
		costs[r] = make([]int, nNodes)
		for c := 0; c < nNodes; c++ {
			costs[r][c] = scanInt(sc)
		}
	}

	states := make([]int, nNodes)
	minCosts := make([]int, nNodes)

	for i := 1; i < nNodes; i++ {
		minCosts[i] = BIG_COST
	}

	for {
		var u int
		minCost := BIG_COST

		for i := 0; i < nNodes; i++ {
			if states[i] != ST_JOINED && minCosts[i] < minCost {
				minCost = minCosts[i]
				u = i
			}
		}

		// 全てのノードが MST に JOIN できたので終了。
		if minCost == BIG_COST {
			break
		}

		states[u] = ST_JOINED

		for v := 0; v < nNodes; v++ {
			cost := costs[u][v]

			// MST内の u と MST外の v との辺の重みをそれぞれ minCosts[v] として
			// 保存しておく。このうち辺の重みが最小となる v が次のループで MST に JOIN する。
			if states[v] != ST_JOINED && cost != -1 && cost < minCosts[v] {
				minCosts[v] = cost
			}
		}
	}

	sum := 0
	for _, cost := range minCosts {
		sum += cost
	}
	fmt.Println(sum)
}

func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	v, _ := strconv.Atoi(sc.Text())
	return v
}

C++: #include <cstdio>
#include <map>
#include <algorithm>

#define rep(i, n) for(int i = 0; i < (n); ++i)

using namespace std;

int n;
int g[100][100];

vector<pair<int, int> > computeSpanningTree(std::vector<pair<int, pair<int, int> > >& edges){
  class UnionFind{
  public:
    UnionFind(int n){
      for(int i = 0; i < n; ++i){
        parent.push_back(i);
      }
      rank.resize(n, 0);
    }

    bool same(int a, int b){
      return find(a) == find(b);
    }

    void unite(int a, int b){
      int p = find(a), q = find(b);

      // assume that p != q

      if(rank[p] > rank[q]){
        parent[q] = p;
      }
      else{
        parent[p] = q;
        
        if(rank[p] == rank[q]){
          ++rank[q];
        }
      }
    }

  private:
    int find(int a){
      return a == parent[a] ? a : parent[a] = find(parent[a]);
    }

  private:
    vector<int> parent, rank;
  };

  class Indexer{
  public:
    int add(int p){
      if(indexer.find(p) == indexer.end()){
        int new_id = indexer.size();
        indexer[p] = new_id;
      }
      return indexer[p];
    }

  private:
    std::map<int, int> indexer;
  };

  sort(edges.begin(), edges.end());

  Indexer id;
  UnionFind uf(edges.size() * 2);

  vector<pair<int, int> > result;
  for(int i = 0; i < edges.size(); ++i){
    int from = edges[i].second.first;
    int to = edges[i].second.second;

    int a = id.add(from);
    int b = id.add(to);

    if(!uf.same(a, b)){
      result.push_back(make_pair(from, to));
      uf.unite(a, b);
    }
  }

  return result;
}

int main(){
	scanf("%d", &n);
	rep(i, n){
		rep(j, n){
			scanf("%d", &g[i][j]);
		}
	}

	std::vector<pair<int, pair<int, int> > > gr;
	rep(i, n){
		for(int j = i; j < n; ++j){
			if(g[i][j] != -1){
				gr.push_back(make_pair(g[i][j], make_pair(i, j)));
			}
		}
	}
	vector<pair<int, int> > edge = computeSpanningTree(gr);
	int s = 0;
	rep(i, edge.size()){
		s += g[edge[i].first][edge[i].second];
	}
	printf("%d\n", s);
	return 0;
}

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;
  }
}


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

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


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 inpPath, string &minPath)
{
  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)
  {
    minPath = to_string(currentV);
    return 0;
  }

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


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

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

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

  }

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

    minPath = to_string(currentV) + minPath;

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

  return minScore;
}


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

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

  string inpPath = "", minPath;

  return minSpanningTreeRecursive(startV, visited, inpPath, minPath);
}


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();
  printAllPath();
#endif // DEBUG

  cout << minSpanningTree() << endl;

  return 0;
}