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 <iostream>
#include <vector>
#include <numeric>

using namespace std;

const int N = 100;

/*
// ????????????
Vnew = {x}???????????§ x ??? V ????????§??????????????????????????????????????????
Enew = {}
while (Vnew ??? V)
    Vnew ????????????????????? u ??¨ ????????????????????? v ????????¶?????????????°??????? (u, v) ??? E ????????????
    v ??? Vnew ????????????
    (u, v) ??? Enew ????????????
*/

enum Color : int {
    WHITE,
    GRAY,
    BLACK,
};

int main() {
    int A[N][N];

    int n;
    cin >> n;
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            int a;
            cin >> a;
            A[i][j] = a == -1 ? a : numeric_limits<int>::max();
        }
    }

    vector<int> color(n, WHITE);
    vector<int> d(n, numeric_limits<int>::max());
    vector<int> p(n, -1);

    d[0] = 0;
    while (1) {
        int minv = numeric_limits<int>::min();
        int u = -1;
        for (int i = 0; i < n; ++i) {
            if (minv > d[i] && color[i] != BLACK) {
                u = i;
                minv = d[i];
            }
        }
        if (u == -1)
            break;
        color[u] = BLACK;
        for (int v = 0; v < n; ++v) {
            if (color[v] != BLACK && A[u][v] != numeric_limits<int>::max()) {
                if (d[v] > A[u][v]) {
                    d[v] = A[u][v];
                    p[v] = u;
                    color[v] = GRAY;
                }
            }
        }
    }
    int sum;
    for (int i =0; i < n; ++i) {
        if (p[i] != -1)
            sum += A[i][p[i]];
    }
    cout << sum << endl;

    return 0;
}

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

static const int MAX_NUM  = 101;
static const int NO_VISIT  = 0;
static const int NOW_VISIT = 1;
static const int END_VISIT = 2;

void MPT(int *pElem, int num)
{
    int i;
    int mincost;
    int target;
    int parent[MAX_NUM];
    int cost[MAX_NUM];
    int state[MAX_NUM] = {0};
    int *tmp;
    int total = 0;

    for (i = 0; i < num; i++) {
        parent[i] = -1;
        cost[i]   = 0x7FFFFFFF;
    }

    cost[0] = 0;

    for (;;) {
        mincost = 0x7FFFFFFF;
        for (i = 0; i < num; i++) {
            if ((state[i] != END_VISIT) && (mincost > cost[i])) {
                target  = i;
                mincost = cost[i];
            }
        }
        if (mincost == 0x7FFFFFFF) {
            break;
        }

        state[target] = END_VISIT;
        total += mincost;

        tmp = pElem + target * MAX_NUM;
        for (i = 0; i < num; i++) {
            if ((state[i] != END_VISIT) && (*(tmp + i) < cost[i])) {
                cost[i]   = *(tmp + i);
                parent[i] = target;
                state[i]  = NOW_VISIT;
            }
        }
    }

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

    return;
}

int main(void)
{
    int i, j;
    int num;
    int tmp;
    int elem[MAX_NUM][MAX_NUM];

    scanf("%d", &num);
    for (i = 0; i < num; i++) {
        for (j = 0; j < num; j++) {
            scanf("%d", &tmp);
            elem[i][j] = (tmp != -1) ? tmp : 0x7FFFFFFF;
        }
    }

    MPT(&elem[0][0], num);

    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

static const int N=100;
static const int INF=100000;

int n;
int M[N][N];

//初期化
int prim(){
  int u,minv;
  int d[N],p[N],color[N];
  for(int i=0;i<n;i++){
    d[i]=INF;
    p[i]=-1;
    color[i]=0;
  }
  
  d[0]=0;
  
  while(1){
    minv=INF;
    u=-1;
    for(int i=0;i<n;i++){
      if(minv>d[i]&&color[i] != 2){
        u = i;
        minv = d[i];
      }
    }
    if(u == -1) break;
    color[u] = 2;
    
    for(int v=0;v<n;v++){
      if(color[v] !=2 && M[u][v] != INF){
        if(d[v]>M[u][v]){
          d[v]=M[u][v];
          p[v]=u;
          color[v]=1;
        }
      }
    }
  }
  int sum=0;
  for(int i=0;i<n;i++){
    if(p[i] !=-1 ) sum += M[i][p[i]];
  }
  return  sum;
}

int main(){
  int e; 
  cin>>n;
  for(int i=0;i<n;i++){
    for(int j=0;j<n;j++){
      cin>>e;
      M[i][j]=(e== -1)? INF : e;
    }
  }
  
  cout<<prim()<<endl;
  return 0;
}

C++: #include <iostream>
using namespace std;
const int maxx=110;
const int infinity=(1<<30);
 
int A[maxx][maxx];
bool flag[maxx];	//用flag来标记这个节点是不是在MST中，true是在T里，false是在V-T里；
 
int main (){
		int n;
		cin>>n;
		for(int i =1;i<=n;i++){
			for(int j=1;j<=n;j++){
				int e;cin>>e;
				A[i][j]=(e==-1)?infinity:e;
			}
		}
 
		for(int i=0;i<maxx;i++) flag[i]=false;
		int sum=0,minv,minj;
		flag[1]=true;
		while(1){
			minv=infinity;
			minj=-1;
			for(int i=1;i<=n;i++){
				if(flag[i]==true){//找到一个在T中的顶点
					for(int j=1;j<=n;j++){
						if(A[i][j]!=-1 && A[i][j]<minv && flag[j]==false) {//找到一个在V-T中的顶点
							minv=A[i][j];//找到权值的最小值minv
							minj=j;//找到权值的最小值对应的顶点，就是下一步要加入到MST中的顶点minj
						} 
					}
				}
			}
//			cout<<minj<<endl;
			if(minv==infinity || minj==-1) break;
			flag[minj]=true;
			sum+=minv;
		}
		
		cout<<sum<<endl;
		return 0;
		
}

C++: #include<iostream>
using namespace std;

const int MAX = 100;


int main() {
	int n;
	int graph[MAX][MAX];
	bool table[MAX] = { false };
	int sum = 0;

	cin >> n;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			int temp;
			cin >> temp;
			if (temp == -1)temp = INT_MAX;
			graph[i][j] = temp;
		}
	}

	table[0] = true;

	for (int i = 0; i < n - 1; i++) {
		int cost = INT_MAX;
		int m;

		for (int j = 0; j < n; j++) {
			if (table[j] == false)continue;

			for (int k = 0; k < n; k++) {
				if (graph[j][k] < cost && table[k] == false) {
					cost = graph[j][k];
					m = k;
				}
			}
		}

		sum += cost;
		table[m] = true;
	}

	cout << sum << endl;

	return 0;
}