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 H;
int A[101];
int NodeNum[101];
int IndexInA[100];


int parent(int i) {
	return i / 2;
}

int left(int i) {
	return 2 * i;
}

int right(int i) {
	return 2 * i + 1;
}

void minHeapify(int i) {
	int l = left(i);
	int r = right(i);
	int smallest = i;
	if (l <= H && A[l] < A[i]) {
		smallest = l;
	}
	if (r <= H && A[r] < A[smallest]) {
		smallest = r;
	}
	if (smallest != i) {
		int temp = A[i];
		A[i] = A[smallest];
		A[smallest] = temp;
		temp = NodeNum[i];
		NodeNum[i] = NodeNum[smallest];
		NodeNum[smallest] = temp;
		IndexInA[NodeNum[i]] = i;
		IndexInA[NodeNum[smallest]] = smallest;

		minHeapify(smallest);
	}
}

void insert(int d, int nodeNum) {
	int i = ++H;
	A[i] = d;
	NodeNum[i] = nodeNum;
	IndexInA[nodeNum] = i;
	int p = parent(i);
	while (i > 1 && A[p] > A[i]) {
		int temp = A[p];
		A[p] = A[i];
		A[i] = temp;
		temp = NodeNum[p];
		NodeNum[p] = NodeNum[i];
		NodeNum[i] = temp;
		IndexInA[NodeNum[p]] = p;
		IndexInA[NodeNum[i]] = i;
		i = p;
		p = parent(i);
	}
}

int heapExtractMinNodeNum() {
	int minNodeNum = NodeNum[1];
	A[1] = A[H];
	NodeNum[1] = NodeNum[H];
	IndexInA[NodeNum[1]] = 1;
	H--;
	minHeapify(1);

	return minNodeNum;
}

void decrese(int i, int value) {
	A[i] = value;
	int p = parent(i);
	while (i > 1 && A[p] > A[i]) {
		int temp = A[p];
		A[p] = A[i];
		A[i] = temp;
		temp = NodeNum[p];
		NodeNum[p] = NodeNum[i];
		NodeNum[i] = temp;
		IndexInA[NodeNum[p]] = p;
		IndexInA[NodeNum[i]] = i;
		i = p;
		p = parent(i);
	}
}

int main() {
	//int Dist[101];
	int Dist[100];
	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;
	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;



	H = 0;
	u = 0;
	inT[u] = true;
	Dist[u] = 0;
	for (i = 1; i <= N - 1; i++) {
		for (j = 0; j < Degree[u]; j++) {
			v = AdjacenceyList[u][j];
			if (inT[v] == false) {
				W = AdjacenceyWeightList[u][j];
				if (Dist[v] == INFTY) {
					Dist[v] = W;
					insert(W, v);
				}
				else if (W < Dist[v]) {
					Dist[v] = W;
					decrese(IndexInA[v], W);
				}
			}
		}

		int minNodeNum = heapExtractMinNodeNum();
		inT[minNodeNum] = true;
		MinTotalWeight += Dist[minNodeNum];
		u = minNodeNum;

	}

	printf("%d\n", MinTotalWeight);

	return 0;
}

C++: #include <iostream>
using namespace std;
#define WHITE 0
#define GRAY 1
#define BLACK 2
#define INFTY 1<<21
#define N 100

int n, A[N][N];

int ans() {
	int color[N], p[N], d[N];
	int u, minv;

	for (int i = 0; i < n; i++) {
		color[i] = WHITE;
		d[i] = INFTY;
		p[i] = -1;
	}
	d[0] = 0;

	while (1) {
		u = -1;
		minv = INFTY;
		for (int i = 0; i < n; i++) {
			if (color[i] != BLACK && d[i] < minv) {
				minv = d[i];
				u = i;
			}
		}

		if (u == -1) break;
		color[u] = BLACK;

		for (int i = 0; i < n; i++) {
			if (color[i] != BLACK && A[u][i] != INFTY) {
				if (A[u][i] < d[i]) {
					d[i] = A[u][i];
					p[i] = u;
					color[i] = GRAY;
				}
			}
		}
	}

	int sum = 0;
	for (int i = 0; i < n; i++) {
		if (p[i] != -1) sum += A[i][p[i]];
	}
	return sum;
}

int main() {
	cin >> n;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			int e;
			cin >> e;
			A[i][j] = (e != -1) ? e : INFTY;
		}
	}
	cout << ans() << endl;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define INFTY (1<<30)

int n;
vector<int> color(101,0),d(101,INFTY),p(101);
vector<vector<int>> M(101,vector<int>(101,INFTY));

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

    while(1){
        int u;
        int mincost=INFTY;
        for(int i=0;i<n;i++){
            if(color[i]!=2&&d[i]<mincost){
                mincost=d[i];
                u=i;
            }
        }
        if(mincost==INFTY) break;
        color[u]=2;

        for(int v=0;v<n;v++){
            if(color[v]!=2&&M[u][v]<INFTY){
                if(M[u][v]<d[v]){
                    d[v]=M[u][v];
                    p[v]=u;
                    color[v]=1;
                }
            }
        }

    }

}

int main(){
    cin>>n;
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            cin>>M[i][j];
            if(M[i][j]==-1) M[i][j]=INFTY;
        }
    }

    prim();

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

C++: #include <iostream>
#include <queue>
using namespace std;
int main() {
    int n;
    cin >> n;
    int a[100][100];
    for (int i = 0; i < n; ++i)
        for (int j = 0; j < n; ++j)
            cin >> a[i][j];
    struct Edge {
        int from, to, weight;
        Edge(int from, int to, int weight) : from(from), to(to), weight(weight) {}
        bool operator < (const Edge& e) const {
            return weight > e.weight;
        }
    };
    int sm = 0;
    priority_queue<Edge> q;
    q.push(Edge(-1, 0, 0));
    bool visited[100]{};
    while (q.size()) {
        auto e = q.top();
        q.pop();
        if (visited[e.to]) continue;
        visited[e.to] = true;
        sm += e.weight;
        for (int i = 0; i < n; ++i) {
            if (a[e.to][i] >= 0 && !visited[i])
                q.push(Edge(e.to, i, a[e.to][i]));
        }
    }
    cout << sm << endl;
    return 0;
}