Go: package main

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

const (
	MAX_N      = 200
	MAX_WEIGHT = 2000
)

type Edge struct {
	start  int
	end    int
	weight int
}

type ShortestInfo struct {
	edges    []Edge
	shortest int
}

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

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

	shortestList := make([]ShortestInfo, n)
	for i := 1; i < n; i++ {
		shortestList[i] = ShortestInfo{edges: []Edge{}, shortest: MAX_N*MAX_WEIGHT + 1}
	}

	adjacentMatrix := make([][]int, n)
	for i := 0; i < n; i++ {
		adjacentMatrix[i] = make([]int, n)
		for j := 0; j < n; j++ {
			v, err := nextInt(sc)
			if err != nil {
				panic(err)
			}
			adjacentMatrix[i][j] = v
		}
	}

	mst([]int{1}, shortestList, adjacentMatrix)
	fmt.Println(calculateMinWeight(shortestList))
}

func calculateMinWeight(shortestList []ShortestInfo) int {
	visitedList := make([]bool, len(shortestList))
	visitedList[0] = true
	sumWeight := 0
	for !visitedAll(visitedList) {
		shortestInfo := shortestList[0]
		shortestList = shortestList[1:]
		edges := shortestInfo.edges
		for _, edge := range edges {
			if !visitedList[edge.end-1] {
				visitedList[edge.end-1] = true
				sumWeight += edge.weight
			}
		}
	}
	return sumWeight
}

func visitedAll(visitedList []bool) bool {
	for _, v := range visitedList {
		if !v {
			return false
		}
	}
	return true
}

func mst(nodeStack []int, shortestList []ShortestInfo, adjacentMatrix [][]int) {
	for len(nodeStack) > 0 {
		currentNode := nodeStack[0]
		nodeStack = nodeStack[1:]
		currentShortest := getShortest(currentNode, shortestList)
		adjacents := findAdjacents(currentNode, adjacentMatrix)
		for i := 0; i < len(adjacents); i++ {
			adj := adjacents[i]
			s := getShortest(adj.end, shortestList)
			if currentShortest.shortest+adj.weight < s.shortest || (currentShortest.shortest+adj.weight == s.shortest && len(currentShortest.edges)+1 > len(s.edges)) {
				setShortest(adj.end, adj, currentShortest, shortestList)
				nodeStack = append(nodeStack, adj.end)
			}
		}
	}
}

func getShortest(node int, shortestList []ShortestInfo) ShortestInfo {
	return shortestList[node-1]
}

func setShortest(node int, edge Edge, currentShortest ShortestInfo, shortestList []ShortestInfo) {
	newEdges := make([]Edge, len(currentShortest.edges)+1)
	for i := 0; i < len(newEdges)-1; i++ {
		newEdges[i] = currentShortest.edges[i]
	}
	newEdges[len(newEdges)-1] = edge
	shortestInfo := ShortestInfo{edges: newEdges, shortest: currentShortest.shortest + edge.weight}
	shortestList[node-1] = shortestInfo
}

func findAdjacents(currentNode int, adjacentMatrix [][]int) []Edge {
	adjacents := adjacentMatrix[currentNode-1]
	edges := []Edge{}
	for i, v := range adjacents {
		if v > 0 {
			edges = append(edges, Edge{start: currentNode, end: i + 1, weight: v})
		}
	}
	return edges
}

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

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

enum e_state {
    UNVISITED = 0,
    CANDITATE,
    VISITED,
};

const int kInvalid = -1;
const uint8_t kMaxN = 100;
int g_numOfVertices;
int g_weightOfEdge[kMaxN][kMaxN];
int g_minWeight[kMaxN];
e_state g_visited[kMaxN];


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_weightOfEdge[i][j]);
        }
        cout << endl;
    }
}


void printAllPath()
{
    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 (g_weightOfEdge[i][j] > 0)
                printf("%d -> %d (%d)\n", i, j, g_weightOfEdge[i][j]);
        }
    }
}


bool isConnected(int a, int b)
{
    if (g_weightOfEdge[a][b] == kInvalid)
        return false;
    else
        return true;
}


void updateStatus(int v)
{
    g_visited[v] = VISITED;

    // mark neighthoods to CANDITATE
    for (int i = 0; i < g_numOfVertices; i++)
    {
        if (isConnected(v, i) && g_visited[i] == UNVISITED)
            g_visited[i] = CANDITATE;
    }

#if (1) & DEBUG
    for (int i = 0; i < g_numOfVertices; i++)
    {
        DPRINTF("[%d] ", i);
        switch (g_visited[i]) {
            case UNVISITED: cout << "UNVISITED" << endl; break;
            case CANDITATE: cout << "CANDITATE" << endl; break;
            case VISITED: cout << "VISITED" << endl; break;
            default: assert(false); break;
        }
    }
#endif
}


void updateMinWeight()
{
    for (int i = 0; i < g_numOfVertices; i++)
    {
        if (g_visited[i] == VISITED)
        {
            for (int j = 0; j < g_numOfVertices; j++)
            {
                if (isConnected(i, j) && g_visited[j] == CANDITATE)
                {
                    if (g_minWeight[j] > g_weightOfEdge[i][j])
                    {
                        g_minWeight[j] = g_weightOfEdge[i][j];
                    }
                }
            }
        }
    }

#if (1) & DEBUG
    for (int i = 0; i < g_numOfVertices; i++)
    {
        if (g_minWeight[i] < INT_MAX)
            DPRINTF("[%d] %d\n", i, g_minWeight[i]);
    }
#endif
}


int takeUnvisitedVertex()
{
    for (int i = 0; i < g_numOfVertices; i++)
        if (g_visited[i] == CANDITATE)
            return i;

    // make sure g_visited[i] has one CANDITATE element at least
    assert(false);
    return -1;
}


bool haveUnvisitedVertex()
{
    for (int i = 0; i < g_numOfVertices; i++)
        if (g_visited[i] != VISITED)
            return true;

    return false;
}


int minSpanningTree()
{
    int sumOfWeight = 0;
    const int entryPoint = 0;

    //g_visited[entryPoint] = VISITED;
    updateStatus(entryPoint);

    while (haveUnvisitedVertex())
    {
        // update weight on edges that is neighborhood
        updateMinWeight();

        // take minimum edge, mark it as visited
        int minVertex, minWeight = INT_MAX;

        for (int i = 0; i < g_numOfVertices; i++)
        {
            if (g_visited[i] == CANDITATE && g_minWeight[i] < minWeight)
            {
                minVertex = i;
                minWeight = g_minWeight[i];
            }
        }

        assert(minWeight != INT_MAX);

        sumOfWeight += minWeight;
        updateStatus(minVertex);
    }

    return sumOfWeight;
}


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_weightOfEdge[i][j];

        cin.ignore();
    }

    // init
    for (int i = 0; i < g_numOfVertices; i++)
    {
        g_minWeight[i] = INT_MAX;
        g_visited[i] = UNVISITED;
    }

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


    cout << minSpanningTree() << endl;

    return 0;
}