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<cinttypes>
#include<algorithm>
#include<numeric>
#include<iterator>
#include<tuple>
#include<functional>
#include<set>

using si32 = std::int32_t;
using ui32 = std::uint32_t;


int main() {

	si32 n;
	std::cin >> n;

	std::vector<std::vector<si32>> graph( n ), cost( n, std::vector<si32>( n, -1 ) );

	for( si32 i{}; i < n; ++i )
	{
		for( si32 j{}; j < n; ++j )
		{
			si32 t; std::cin >> t;

			if( t < 0 || i == j )
				continue;

			graph[i].emplace_back( j );
			graph[j].emplace_back( i );

			cost[i][j] = cost[j][i] = t;
		}
	}


	//	std::queue<>

		//edges
	std::vector<std::pair<si32, si32>> mst;
	mst.reserve( n - 1 );

	for( const auto&e : graph.front() )
		if( mst.empty() )
			mst.emplace_back( 0, e );
		else
			if( cost[0][e] < cost[ mst[0].first ][ mst[0].second ] )
				mst[0] = std::make_pair( 0, e );

	//std::cout << mst[0].first << " " << mst[0].second << "\n";


	std::set<si32> mst_vertices;
	for( const auto&v : { mst[0].first, mst[0].second } )
		mst_vertices.emplace( v );


	while( mst.size() < static_cast<decltype(mst.size())>(n - 1) )
	{
		
		si32 cost_t = 20000;
		std::pair<si32, si32> edge{ 0,0 };
		
		for( const auto& mv : mst_vertices )
		{
			for( const auto& gv : graph[mv] )
				if( mst_vertices.find( gv ) == mst_vertices.end() )
					if( cost[mv][gv] < cost_t )
					{
						edge = std::make_pair( mv, gv );
						cost_t = cost[mv][gv];
					}
		}

		mst_vertices.emplace( edge.second );
		mst.emplace_back( edge );
		/*
		std::cout << edge.first << " " << edge.second << "\n";
		std::cout<<"---\n"
			<< mst.size() << ":\n";
		for( const auto&e : mst )
			std::cout << e.first << " " << e.second << "\n";
		std::cout << "------\n";
		*/
		
	}
	
	ui32 ans{};
	for( const auto& e : mst )
	{
		ans += cost[e.first][e.second];
//		std::cout << e.first << " " << e.second << " " << cost[e.first][e.second] << " " << ans << "\n";
	}

	std::cout << ans << "\n";

	return 0;
}

C++: #include<stdio.h>
#include<algorithm>
#include<queue>
using namespace std;

const int V_MAX = 10000;
const int INF = 100000000;

struct Edge {
	bool operator< (const Edge& e)const {
		return e.w > w;
	}
	int s;
	int t;
	int w;
};

int parent(int uft[], int child) {
	if (uft[child] == -1)return child;
	return uft[child] = parent(uft, uft[child]);
}

int main() {
	int v, e;
	vector<Edge> edge;
	int uft[V_MAX];
	int ans = 0;
	
	scanf("%d%d", &v, &e);
	for (int i = 0; i < v; i++) {
		uft[i] = -1;
	}
	for (int i = 0; i < e; i++) {
		Edge e;
		scanf("%d%d%d", &e.s, &e.t, &e.w);
		
		edge.push_back(e);
	}

	sort(edge.begin(), edge.end());

	for (int i = 0; i < e; i++) {
		Edge e = edge[i];
		
		int p1 = parent(uft, e.s);
		int p2 = parent(uft, e.t);
		if (p1 != p2) {
			uft[p1] = p2;
			ans += e.w;
		}
	}

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

	return 0;
}

C++: //pair???????????¨???????????????????????£????????§31-179573????????????????????????????????????????????????????????????
#include<iostream>
#include<utility>
#include<vector>
#include<algorithm>
using namespace std;

int P[10010];
void init(int N){
  for (int i=0;i<=N;i++) P[i]=i;
}

int root(int a){
  if(P[a]==a) return a;
  return(P[a] = root(P[a]));
}

bool is_same_set(int a,int b){
  return root(a) == root(b);
}
void unite(int a,int b){
  P[root(a)] = root(b);
}

int main(){
  int n;
  cin>>n;
  int A[n][n];
  for(int i=0;i<n;i++){
    for(int j=0;j<n;j++){
      cin >> A[i][j];
    }
  }
  vector<int> cost,src,dst;
  int M=0;
  for(int r=0;r<n;r++){
    for(int c=r+1;c<n;c++){
      if(A[r][c]>-1){
	cost[M]=A[r][c];
	src[M]=r;
	dst[M]=c;
	M+=1;
      }
    }
  }
  pair<int,int> edges[M];
  for(int i=0;i<M;i++){
    edges[i] =make_pair(cost[i],i);
  }

  sort(edges,edges+M);
  int s=0;
  for(int i=0;i<M;i++){
    int k= edges[i].second;
    int p=src[k];
    int q=dst[k];
    if(is_same_set(p,q)) continue;
    unite(p,q);
    s+=edges[i].first;
  }
  cout << s<<endl;
}

C++: #include<bits/stdc++.h>

using namespace std;

struct Edge{
    int from,to,w;
    bool friend operator < (const Edge& a,const Edge& b){
        return a.w<b.w;
    }
};
const int maxn=105;
vector<Edge> G;
int fa[maxn];
int V,E;
void init(){
    for(int i=1;i<=V;i++)
        fa[i]=i;
}

int find(int x){
    int y=fa[x];
    for(;fa[y]!=y;y=fa[y]);
    while(x!=y){
        int temp=fa[x];
        fa[x]=y;
        x=temp;
    }
    return y;
}

void merge(int x,int y){
    int nx=find(x);
    int ny=find(y);
    if(nx!=ny){
        fa[nx]=ny;
    }
}
int ans=0;
void krusal(int v){
    sort(G.begin(),G.end());
    E=G.size();
    V=v;
    init();
    for(int i=0;i<E;i++){
        Edge temp=G[i];
        int fa1=find(temp.from);
        int fa2=find(temp.to);
        if(fa1!=fa2){
            ans+=temp.w;
            fa[fa1]=fa2;
        }
    }
}

int main(){
    int V;
    cin>>V;
    E=0;
    for(int i=1;i<=V;i++){
        for(int j=1;j<=V;j++){
            int key;
            cin>>key;
            if(key!=-1){
                G.push_back(Edge{i,j,key});
            }
        }
    }
    krusal(V);
    cout<<ans<<endl;
}

C++: #include <iostream>
#include <vector>
#include <queue>
#define to first
#define cost second
const int inf=1e9+7;
using namespace std;
int main(){

  int n,tmp,r=0,now=0,flag[100]={};
  cin>>n;
  vector <pair<int,int> > gl[n];
  priority_queue < pair<int,int>, vector< pair<int,int> >, greater< pair<int,int> > >q;

  for(int i=0;i<n;i++) for(int j=0;j<n&&cin>>tmp;j++) if(tmp!=-1) gl[i].push_back(make_pair(j,tmp));
  for(int j=0;j<n-1;j++){
    for(int i=0;i<gl[now].size();i++) if(!flag[gl[now][i].to]) q.push(make_pair(gl[now][i].cost,gl[now][i].to));
    flag[now]=1;
    for(;flag[q.top().second];) q.pop();
    r+=q.top().first;
    now=q.top().second;
    q.pop();
  }
  cout<<r<<endl;
  return 0;

}