Go: package main

import (
	"bufio"
	"fmt"
	"log"
	"math"
	"os"
	"strconv"
	"strings"
)

type color int

const (
	white color = iota // 未訪問
	mst                // MSTとして確定
)

// 頂点
type node struct {
	matrix   []int // 隣接行列
	minToMst int   // MSTへの重み最小値
	parent   int   // 最小値の相手
	color    color
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	n := eGetInt(scanner)

	nodes := make([]node, n)
	for i := 0; i < n; i++ {
		nodes[i].matrix = eAtoi((strings.Fields(eGetline(scanner))))
		nodes[i].parent = -1
		nodes[i].minToMst = math.MaxInt16
		nodes[i].color = white
	}

	// 初回はnode[0]がMSTに入るように,minToMstを設定
	nodes[0].minToMst = 0
	for { // 全部チェックするまで

		// どの頂点を新たにMSTに取り込むか
		min := math.MaxInt16
		u := -1
		for i := 0; i < n; i++ {
			if nodes[i].color == mst {
				continue
			}
			if nodes[i].minToMst < min {
				u = i
				min = nodes[i].minToMst
			}
		}

		if u == -1 {
			break
		}
		// MSTに追加する頂点決定
		nodes[u].color = mst

		// MSTに属していない頂点それぞれについて,MSTとの最小の重みを更新
		for i := 0; i < n; i++ {
			if nodes[i].color == mst {
				continue
			}
			if nodes[i].matrix[u] == -1 {
				// 接してない
				continue
			}
			if nodes[i].matrix[u] < nodes[i].minToMst {
				// node[u]との重みが最小
				nodes[i].minToMst = nodes[i].matrix[u]
				nodes[i].parent = u
			}
		}
	}

	sum := 0
	for _, v := range nodes {
		// node[0]はparentが入っていない
		if v.parent != -1 {
			sum += v.matrix[v.parent]
		}
	}
	fmt.Println(sum)
}

func eGetInt(scanner *bufio.Scanner) int {
	t := eAtoi(strings.Fields(eGetline(scanner)))
	return t[0]
}

func eGetline(p *bufio.Scanner) string {
	if !p.Scan() {
		if err := p.Err(); err != nil {
			log.Fatal(err.Error())
		}
		return ""
	}
	return p.Text()
}

func eAtoi(s []string) []int {
	ints := make([]int, len(s))
	var err error
	for i, e := range s {
		ints[i], err = strconv.Atoi(e)
		if err != nil {
			log.Fatal(err.Error())
		}
	}
	return ints
}

C++: #include <cstdio>
#include <vector>

int main(void){
	int n, min_edge, next_node, count, sum;
	scanf("%d", &n);
	std::vector< std::vector<int> > edges;
	std::vector<int> isUsed;
	edges.resize(n);
	isUsed.resize(n);
	for(int i=0;i<n;i++){
		isUsed[i]=-1;
		edges[i].resize(n);
		for(int j=0;j<n;j++){
			scanf("%d", &edges[i][j]);
		}
	}

	min_edge=3000;
	isUsed[0] = 1;
	count = 0;
	sum=0;
	while(count<n-1){
		for(int i=0;i<n;i++){
			if(isUsed[i]==1){
				for(int j=0;j<n;j++){
					if (isUsed[j]==-1 && edges[i][j]>-1 && edges[i][j]<min_edge){
						min_edge = edges[i][j];
						next_node = j;
					}
				}
			}
		}
		sum += min_edge;
		isUsed[next_node]=1;
		min_edge = 3000;
		count += 1;
	}
	printf("%d\n", sum);
	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

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