Go: package main

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

var scanner = bufio.NewScanner(os.Stdin)

func nextString() string {
	scanner.Scan()
	return scanner.Text()
}

func nextInt() int {
	n, err := strconv.Atoi(nextString())
	if err != nil {
		fmt.Printf("strconv.Atoi failed: %v\n", err)
	}
	return n
}

type ElementKey int
type Node struct {
	key    ElementKey
	parent *Node
	rank   int
}

func newNode(key ElementKey) *Node {
	node := Node{}
	node.key = key
	node.parent = &node
	node.rank = 0

	return &node
}

func union(x, y *Node) {
	xSet := findSet(x)
	ySet := findSet(y)
	link(xSet, ySet)
}

func link(x, y *Node) {
	if x.rank > y.rank {
		y.parent = x
	} else {
		x.parent = y

		if x.rank == y.rank {
			y.rank++
		}
	}
}

func findSet(x *Node) *Node {
	if x != x.parent {
		x.parent = findSet(x.parent)
	}
	return x.parent
}

func newDisjointSets(nSets int) *DisjointSets {
	sets := DisjointSets{}
	sets.keyToNode = map[ElementKey]*Node{}
	for i := 0; i < nSets; i++ {
		key := ElementKey(i)
		sets.keyToNode[key] = newNode(key)
	}
	return &sets
}

type DisjointSets struct {
	keyToNode map[ElementKey]*Node
}

func (sets *DisjointSets) union(x, y ElementKey) {
	xNode := sets.keyToNode[x]
	yNode := sets.keyToNode[y]
	union(xNode, yNode)
}

func (sets DisjointSets) findSet(x ElementKey) *Node {
	xNode := sets.keyToNode[x]
	return findSet(xNode)
}

type Edge struct {
	i, j   int
	weight int
}

func main() {
	scanner.Split(bufio.ScanWords)

	n := nextInt()
	sets := newDisjointSets(n)
	edges := []Edge{}
	a := make([][]int, n)
	for i := 0; i < n; i++ {
		a[i] = make([]int, n)
		for j := 0; j < n; j++ {
			a[i][j] = nextInt()
			if a[i][j] >= 0 {
				e := Edge{
					i:      i,
					j:      j,
					weight: a[i][j],
				}
				edges = append(edges, e)
			}
		}
	}
	sort.Slice(edges, func(i, j int) bool { return edges[i].weight < edges[j].weight })
	totalWeight := 0
	for _, edge := range edges {
		i := ElementKey(edge.i)
		j := ElementKey(edge.j)
		if sets.findSet(i) != sets.findSet(j) {
			totalWeight += edge.weight
			sets.union(i, j)
		}
	}
	fmt.Println(totalWeight)
}

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;

}