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 <iostream>
#include <cstdio>
#include <string>
#include <list>
#include <stack>
#include <queue>
#include <algorithm>
#include <utility>
#include <vector>
#include <map>

using namespace std;

int main()
{
	int N;
	int maxcost=0;
	vector<pair<int,int> > cost;
	cin>>N;
	vector<int> parent(N,-1);
	pair<int,int> ddata;
	for(int i=0;i<N*N;i++){
		cin>>ddata.first;
		if(ddata.first!=-1){
			ddata.second=i;
			cost.push_back(ddata);
		}
	}
	sort(cost.begin(),cost.end());
	for(int i=0;i<cost.size();i++){
		while(parent[cost[i].second/N]!=-1&&parent[parent[cost[i].second/N]]!=-1){
			parent[cost[i].second/N]=parent[parent[cost[i].second/N]];
		}
		while(parent[cost[i].second%N]!=-1&&parent[parent[cost[i].second%N]]!=-1){
			parent[cost[i].second%N]=parent[parent[cost[i].second%N]];
		}
		if(parent[cost[i].second/N]!=cost[i].second%N&&parent[cost[i].second%N]!=cost[i].second/N&&(parent[cost[i].second%N]!=parent[cost[i].second/N]||parent[cost[i].second/N]==-1||parent[cost[i].second%N]==-1)){
			//cout<<' '<<cost[i].second/N<<' '<<cost[i].second%N<<endl;
			maxcost+=cost[i].first;
			if(parent[cost[i].second/N]==-1){
				if(parent[cost[i].second%N]!=-1){
					parent[parent[cost[i].second%N]]=cost[i].second/N;
				}
				parent[cost[i].second%N]=cost[i].second/N;
			}else if(parent[cost[i].second%N]==-1){
				parent[parent[cost[i].second/N]]=cost[i].second%N;
				parent[cost[i].second/N]=cost[i].second%N;
			}else{
				parent[parent[cost[i].second%N]]=parent[cost[i].second/N];
				parent[cost[i].second%N]=parent[cost[i].second/N];
			}
		}
		int c=0;
		for(int i=0;i<N;i++){
			if(parent[i]==-1){
				c++;
			}
		}
		if(c<2){
			break;
		}
	}
	cout<<maxcost<<endl;
	/*
	for(int i=0;i<N;i++){
		cout<<parent[i]<<endl;
	}//*/
	return 0;
}

C++: #include<iostream>
#include<vector>
using namespace std;
int inf = 10000000;
int n;
int map[101][101];
int dep[101];
int all = 0;
int total = 0;
void solve() {
	vector<int>v;
	v.push_back(0);
	dep[0] = 1;
	total = 1;
	while (total != n)
	{
		int min = inf;
		int k ;
		for (int i = 0; i < v.size(); i++) {
			for (int j = 0; j < n; j++) {
				if (!dep[j] && map[v[i]][j] < min) {
					min = map[v[i]][j];
					k = j;
				}
			}
		}
		v.push_back(k);
		dep[k] = 1;
		all += min;
		total++;
	}
}
int main(void) {
	cin >> n;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			int k;
			cin >> k;
			map[i][j] = (k == -1) ? inf : k;
		}
	}
	solve();
	cout << all << endl;
}

C++: #include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string.h>
#include<stack>
#include<queue>
#include<vector>
using namespace std;

int n;
int d[10000], m[100][100], p[100];
int c[10000];


void prim(){
  d[0] = 0;
  p[0] = -1;

  while(1){
    int mincost = 10000, u;
    for(int i = 0; i < n; i++){
      if(c[i] != 2 && mincost > d[i]){
        mincost = d[i];
        u = i;
      }
    }

    if(mincost == 10000) break;

    c[u] = 2;

    for(int i = 0; i < n; i++){
      if(c[i] != 2 && m[u][i] >= 0){
        if(m[u][i] < d[i]){
          d[i] = m[u][i];
          p[i] = u;
          c[i] = 1;
        }

      }
    }
  }
}

int main(){

  cin >> n;

  for(int i = 0; i < n; i++){
    c[i] = 0;
    d[i] = 2000;
    for(int j = 0; j < n; j++){
      cin >> m[i][j];
    }
  }

  prim();

  int sum = 0;
  for(int i = 0; i < n; i++) sum += d[i];
  cout << sum << endl;

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


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