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<cstdio>
#include<cstring>
#define white 0
#define black -1
#define grey 1
#define maxn 100
#define infly 1<<21
using namespace std;

int n,m[maxn+5][maxn+5],d[maxn+5],p[maxn+5],color[maxn+5];

int prim()
{
    int u,minv;

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

    d[0]=0;

    while(1)
    {
        minv=infly;
        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 && m[u][v]!=infly)
            {
                if(d[v]>m[u][v])
                {
                    d[v]=m[u][v];
                    p[v]=u;
                    color[v]=grey;
                }
            }
        }
    }

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

    return sum;
}

int main()
{
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            int x;
            scanf("%d",&x);
            if(x==-1) m[i][j]=infly;
            else m[i][j]=x;
        }
    }
    printf("%d\n",prim());
    return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <string>
#include <stack>
#include <cstdlib>
#include <queue>
#include <vector>
#include <set>
#include <stdio.h>
#include <cstdio>
using namespace std;
int p[100001],s[100001],t[100001],cost[100001];
int root(int a) {
	if (p[a] == a) return a;
	return (p[a] = root(p[a]));
}
void unite(int a,int b){
	p[root(a)] = root(b);
}
bool same(int a ,int b){
	if (root(a) == root(b)) return 1;
	else return 0;
}
bool rcheck(int a){
	if (root(a) == a) return 1;
	else return 0;
}
pair <int,int> path[100001];
int main() {
	int n,coun = 0;
	cin >> n;
	for (int i = 0; i < n; i++){
		p[i] = i;
		for (int j = 0;j<n;j++) {
			s[n*i+j] = i; t[n*i+j]=j; 
			cin >> cost[n*i+j];
		}
	}
	for (int i = 0;i < n*n;i++) {
		path[i].first = cost[i];
		path[i].second = i;
	}
	sort(path,path+n*n);
	for (int i = 0; i < n*n;i++) {
		if (path[i].first >= 0) {
			if(same(s[path[i].second],t[path[i].second]) == 0) {
				unite(s[path[i].second],t[path[i].second]);
				coun += path[i].first;
			}
		}
	}
	cout << coun << endl;
}

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

int n;
int M[N][N],color[N],d[N],p[N];
   //M??????????????????color????¨??????¶???(0,1,2),d????????????????°???????????¨????
   //p???MST???????°???¨?????¨??????????????????????????????

void Prim() {
	for (int i = 0;i < n;i++) d[i] = INF;
	d[0] = 0;  //?????????????????????????????§???????????§????´???????????????????????????????
	p[0] = -1;  //??????????????????????????????????????????

	int u;
	while (true) {
		int min = INF;
		for (int i = 0;i < n;i++) {
			if(color[i]!=BLACK&&min>d[i]){
				min = d[i];
				u = i;
			}
		}

	if (min == INF) break;

	color[u] = BLACK;

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

	}

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

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] = INF;
		}

	Prim();

}

C++: #include <iostream>

using namespace std;

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

struct Vertex {
    bool is_visit;
    int min_w;
    int parent;
};

class MST {
private:
    int matrix[N][N];
    Vertex vertex[N];
    int n;

public:
    MST()
    {
        cin >> n;
        for(int i = 0; i < n; i++)
        {
            for(int j = 0; j < n; j++)
            {
                cin >> matrix[i][j];
                if(matrix[i][j] == -1)
                    matrix[i][j] = INF;
            }
        }
    }

    void prim()
    {
        for(int i = 0; i < n; i++)
        {
            vertex[i].is_visit = false;
            vertex[i].min_w = INF;
        }
        vertex[0].min_w = 0;
        vertex[0].parent = -1;

        while(true)
        {
            int u = -1;
            int mincost = INF;
            for(int i = 0; i < n; i++)
            {
                if(!vertex[i].is_visit && vertex[i].min_w < mincost)
                {
                    mincost = vertex[i].min_w;
                    u = i;
                }
            }

            if(mincost == INF)
                break;

            vertex[u].is_visit = true;

            for(int v = 0; v < n; v++)
            {
                if(!vertex[v].is_visit && matrix[u][v] != INF)
                {
                    if(matrix[u][v] < vertex[v].min_w)
                    {
                        vertex[v].min_w = matrix[u][v];
                        vertex[v].parent = u;
                    }
                }
            }
        }
    }

    void print()
    {
        int sum = 0;
        for(int i = 0; i < n; i++)
            if(vertex[i].parent != -1)
                sum += matrix[i][vertex[i].parent];
        cout << sum << endl;
    }
};

int main()
{
    MST mst;
    mst.prim();
    mst.print();
}