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++: #define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <cstdlib>
#include <string>
#include <iostream>
#include <climits>
#include <list>
#include <queue>

using namespace std;

static const int INFTY = 2001;

int main() {
	int Dist[101];
	int AdjacenceyList[100][100];
	int AdjacenceyWeightList[100][100];
	int Degree[100];
	int i, j;
	int N;
	int W;
	int Deg;
	int MinTotalWeight = 0;
	int u, v;
	int PrevSecondSmallestVertex;
	int SecondSmallestVertex;
	int MinVertex;
	bool inT[100];

	cin >> N;

	for (i = 0; i < N; i++) {
		inT[i] = false;
		Dist[i] = INFTY;
		Deg = -1;
		for (j = 0; j < N; j++) {
			cin >> W;
			if(W != -1){
				AdjacenceyList[i][++Deg] = j;
				AdjacenceyWeightList[i][Deg] = W;
			}
		}
		Degree[i] = Deg + 1;
	}
	Dist[100] = INFTY;

	u = 0;
	inT[u] = true;
	Dist[u] = 0;
	PrevSecondSmallestVertex = 100;
	for (i = 1; i <= N - 1; i++) {
		MinVertex = 100;
		for (j = 0; j < Degree[u]; j++) {
			v = AdjacenceyList[u][j];
			if (inT[v] == false) {
				W = AdjacenceyWeightList[u][j];
				if (W < Dist[v]) {
					Dist[v] = W;
				}
				if (Dist[v] < Dist[MinVertex]) {
					SecondSmallestVertex = MinVertex;
					MinVertex = v;
				}
				else if (Dist[v] < Dist[SecondSmallestVertex]) {
					SecondSmallestVertex = v;
				}
			}
		}

		////DEBUG
		//cout << "MinVerTex = " << MinVertex << " SecondSmallestVertex = " << SecondSmallestVertex << endl;

		if (Dist[PrevSecondSmallestVertex] < Dist[MinVertex]) {
			SecondSmallestVertex = MinVertex;
			MinVertex = PrevSecondSmallestVertex;
			PrevSecondSmallestVertex = SecondSmallestVertex;
		}
		else if (Dist[SecondSmallestVertex] < Dist[PrevSecondSmallestVertex]) {
			PrevSecondSmallestVertex = SecondSmallestVertex;
		}
		inT[MinVertex] = true;
		u = MinVertex;
		MinTotalWeight += Dist[MinVertex];

		////DEBUG
		//cout << "MinVertex = " << MinVertex << " PrevSecondSmallestVertex = " << PrevSecondSmallestVertex << endl;
		//cout << "inT: ";
		//for (int k = 0; k < N; k++) {
		//	cout << inT[k] << " ";
		//}
		//cout << endl;
		//printf("MinTotalWeight = %d\n", MinTotalWeight);
	}
	printf("%d\n", MinTotalWeight);

	return 0;
}

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