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 <string>
#include <vector>
using namespace std;

int root(int i,vector<int> &parent){
    if(parent[i]==-1){
        //親がいない時
        return i;
    }else{
        int result=root(parent[i],parent);
        parent[i]=result; //パス圧縮
        return result;
    }
}

void unite(int x,int y,vector<int> &parent){
    if(root(x,parent)!=root(y,parent)){
        parent[root(y,parent)]=root(x,parent);
    }
}

bool issame(int x,int y,vector<int> &parent){
    return root(x,parent)==root(y,parent);
}

int main(){
    int n;
    cin >> n;
    //first:cost,second:(first:from,second:to)
    vector<pair<int,pair<int,int> > > bridges(n*n);

    //入力
    for(int i=0;i<n;++i){
        for(int j=0;j<n;++j){
            int cost;
            if(cin >> cost,cost!=-1){
                bridges.push_back(pair<int,pair<int,int> >(cost,pair<int,int>(i,j)));
            }
        }
    }

    //sort
    sort(bridges.begin(),bridges.end());

    //島を管理する用
    vector<int> parents(n*n,-1);

    int sum=0;

    for(int i=0;i<bridges.size();++i){
        if(!issame(bridges[i].second.first,bridges[i].second.second,parents)){
            unite(bridges[i].second.first,bridges[i].second.second,parents);
            sum+=bridges[i].first;
        }
    }
    cout << sum;
}

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

const int MAX=105;
int state[MAX],par[MAX],N,S[MAX][MAX];
priority_queue<pair<int,int>> Q;

int prim(){
    int sum=0,cnt=1;
    state[0]=1;
    for(int i=0;i<N;i++){
        if(S[0][i]!=10000) Q.push(make_pair(-S[0][i],i));
    }
    while(1){
        int a=Q.top().second;
        sum+=-Q.top().first;
        state[a]=1;
        Q.pop();
        cnt++;
        for(int i=0;i<N;i++){
            if(S[a][i]!=10000&&state[i]==0) Q.push(make_pair(-S[a][i],i));
        }
        if(cnt==N) break;
    }
    return sum;
}


int main(){

    cin>>N;
    for(int i=0;i<N;i++){
        for(int j=0;j<N;j++){
            int a;cin>>a;
            if(a==-1) S[i][j]=10000;
            else S[i][j]=a;
        }
    }
    cout<<prim()<<endl;
}

C++: #include <cstdio>
#include <algorithm>
#include <unordered_map>
#include <unordered_set>

#define N 100
#define INF 0x7FFFFFFF

int prim(std::unordered_map<int, int> a[N], const int n){
    std::unordered_set<int> checked;
    int total = 0;
    checked.insert(0);
    while(checked.size() != n){
        int newId = INF;
        int newWeight = INF;
        for(auto &x: checked){
            for(auto &j: a[x]){
                if(checked.find(j.first) == checked.end() && newWeight > j.second){
                    newId = j.first;
                    newWeight = j.second;
                }
            }
        }
        checked.insert(newId);
        total += newWeight;
    }
    return total;
}

int main(){
    int n;
    std::scanf("%d", &n);
    std::unordered_map<int, int> a[N];
    for(int i = 0; i < n; ++i){
        for(int j = 0; j < n; ++j){
            int v;
            std::scanf("%d", &v);
            if(v != -1){
                a[i].insert(std::make_pair(j, v));
            }
        }
    }
    int val = prim(a, n);
    std::printf("%d\n", val);
    return 0;
}

C++: #include<iostream>
using namespace std;
static const int MAX = 100;
static const int INFTY = (1 << 21);
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;

int n, A[MAX][MAX];

int prim(){
    int u, minv;
    int d[MAX], p[MAX], color[MAX];

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

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

        if(minv == INFTY) break;

        color[u] = BLACK;

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

    int sum = 0;
    for(int i = 0; i < n; i++){
        sum += d[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) ? INFTY : e;
        }
    }

    cout << prim() << endl;

    return 0;
}

C++: #include<bits/stdc++.h>
//cout<<setprecision(1)<<fixed<<endl;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int MAX_N=100000;
#define eps 1e-7
//#define Kongxiangzhouye

static const int MAX=100;
static const int INF=(1<<21);
static const int WHITE=0;
static const int GRAY=1;
static const int BLACK=2;

int begin[10000];
int to[10000];
int next[10000];
int weight[10000];
int e,n;
void add(int x,int y,int z){
	to[++e]=y;
	next[e]=begin[x];
	begin[x]=e;
	weight[e]=z;
}

struct p{
	int edge;
	int weight;
	bool operator < (const p& u)const{
		return weight>u.weight;
	}
};
int createtree(){
	priority_queue<p> q;
	int flag[100];
	memset(flag,0,sizeof(flag));
	int te=begin[1];
	while(to[te]!=-1){
		if(flag[to[te]]==0)
			q.push((p){te,weight[te]});
		te=next[te];
	}
	flag[1]=1;
	int ans=0;
	while(1){
		int flag2=1;
		for(int i=1;i<=n;i++){
			if(flag[i]==0){
				flag2=0;
				break;
			}
		}
		if(flag2==1)
			break;
		while(flag[to[q.top().edge]]==1)
			q.pop();
		p now=q.top();
		q.pop();
		ans=ans+now.weight;
		flag[to[now.edge]]=1;
		int te=begin[to[now.edge]];
		while(te!=-1){
			if(flag[to[te]]==0)
				q.push((p){te,weight[te]});
			te=next[te];
		}
	}
	return ans;
}
int main(){
	ios::sync_with_stdio(false);
	#ifdef Kongxiangzhouye
		freopen("in.txt","r",stdin);
		freopen("out.txt","w",stdout);
	#endif
	while(cin>>n){
		memset(begin,-1,sizeof(begin));
		memset(next,-1,sizeof(next));
		memset(to,-1,sizeof(to));
		e=0;
		for(int i=1;i<=n;i++){
			for(int j=1;j<=n;j++){
				int te;
				cin>>te;
				if(te!=-1)
					add(i,j,te);
			}
		}
		cout<<createtree()<<endl;
	}

	return 0;
}