Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

//util
var sc = bufio.NewScanner(os.Stdin)

func nextStr() string {
	sc.Scan()
	return sc.Text()
}

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func printList(list []int) {
	n := len(list)
	buf := bufio.NewWriter(os.Stdout)
	for _, v := range list[0 : n-1] {
		buf.Write([]byte(fmt.Sprintf("%d ", v)))
	}
	buf.Write([]byte(fmt.Sprintf("%d\n", list[n-1])))
	buf.Flush()
}

func max(a, b int) int {
	if a > b {
		return a
	} else {
		return b
	}
}

func min(a, b int) int {
	if a > b {
		return b
	} else {
		return a
	}
}

//solve
const (
	MAX   = 100
	INFTY = (1 << 21)
	WHITE = 0
	GRAY  = 1
	BLACK = 2
)

//V: グラフの頂点全体の集合
//T: MSTに属する頂点の集合
var (
	n     int
	M     [MAX][MAX]int //重みを記録する隣接行列
	color [MAX]int
	p     [MAX]int //MSTにおける頂点の親を記録
	d     [MAX]int //Tに属する頂点と、V−Tに属する頂点を繋ぐ辺の中で、重みが最小のものを記録
)

func prim() int {
	var u, minv int
	//初期化
	for i := 0; i < n; i++ {
		d[i] = INFTY
		p[i] = -1
		color[i] = WHITE
	}

	d[0] = 0

	for {
		//頂点uを決めていく
		//つまり、Tに属する頂点と、V−Tに属する頂点を繋ぐ辺の中で、重みが最小のものを決める
		minv = INFTY
		u = -1
		for i := 0; i < n; i++ {
			//未訪問かつ、辺の重みが最小となるuを選ぶ
			if minv > d[i] && color[i] != BLACK {
				u = i
				minv = d[i]
			}
		}

		if u == -1 {
			break
		}

		color[u] = BLACK
		for v := 0; v < n; v++ {
			//未訪問かつ、辺が存在するとき
			if color[v] != BLACK && M[u][v] != INFTY {
				if d[v] > M[u][v] {
					d[v] = M[u][v]
					p[v] = u
					color[v] = GRAY
				}
			}
		}
	}

	sum := 0
	for i := 0; i < n; i++ {
		if p[i] != -1 {
			sum += M[i][p[i]]
		}
	}
	return sum
}

func main() {
	sc.Split(bufio.ScanWords)
	n = nextInt()

	for i := 0; i < n; i++ {

		for j := 0; j < n; j++ {
			tmp := nextInt()
			if tmp == -1 {
				tmp = INFTY
			}
			M[i][j] = tmp
		}

	}

	fmt.Println(prim())

}

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;

}