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<algorithm>
using namespace std;
int G[103][103];
int V;
int NG[103][103];//新しくできるほう
int used[103];
int main(){
	int ans=0;
	cin>>V;
	for(int i=0;i<V;i++){
		for(int j=0;j<V;j++){
		cin>>G[i][j];
		}
	}
	for(int i=0;i<103;i++)for(int j=0;j<103;j++)NG[i][j]=-1;
	used[0]=1;
	for(int i=0;i<V;i++){
		int u=-1,a,b;
		for(int j=0;j<V;j++){
		if(used[j]==0)u=i;
		}
		if(u==-1)break;
		int now=99999999;
		for(int j=0;j<V;j++){
			for(int k=0;k<V;k++){
			if(now>G[j][k]&&G[j][k]!=-1&&used[j]==1&&used[k]==0){
			now=G[j][k];
			a=j,b=k;
			}
			}
		}
		ans+=G[a][b];
		used[b]=1;
		NG[a][b]=G[a][b];
	}
	cout<<ans<<endl;
	return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <tuple>

using namespace std;

tuple<int,int,int> a[10010];
int t[101];
int n,w;
int k=0;
int x,y;

int root(int x)
{
     if(t[x]==x)
     {
          return x;
     }
     else
     {
          t[x]=root(t[x]);
          return t[x];
     }
}

void unite(int x,int y)
{
     t[root(x)]=root(y);
}

bool same(int x,int y)
{
     return root(x)==root(y);
}

int main()
{
     cin >> n;
     for(int i=0;i<n;i++)
     {
          t[i]=i;
          for(int j=0;j<n;j++)
          {
               cin >> w;
               if(w==-1)
               {
                    continue;
               }
               a[k]=make_tuple(w,i,j);
               k+=1;
          }
     }
     sort(a,a+k);
     w=0;
     for(int i=0;i<k;i++)
     {
          x=get<1>(a[i]);
          y=get<2>(a[i]);
          if(same(x,y))
          {
               continue;
          }
          unite(x,y);
          w+=get<0>(a[i]);
     }
     cout << w << endl;
     return 0;
}

C++: #include <iostream>
#include <algorithm>

using namespace std;

typedef struct b {
	int dis;
	int node1;
	int node2;
}b;

bool cmp(b n1, b n2) {
	return n1.dis < n2.dis;
}

int main() {
	int n;
	cin >> n; 
	int graph[101][101];
	b bs[10000];
	int bCount = 0;
	int ifs[101];
	for (int i = 0; i < n; ++i) {
		ifs[i] = 0;
		for (int j = 0; j < n; ++j) {
			cin >> graph[i][j];
			if (graph[i][j] == -1) {
				continue;
			}
			else if(i>j){
				bs[bCount].dis= graph[i][j];
				bs[bCount].node1 = i;
				bs[bCount++].node2 = j;
			}
		}
	}

	sort(bs, bs + bCount, cmp);

	long totalDis = 0;
	int k = 1;
	for (int i = 0; i < bCount; ++i) {
		b b1 = bs[i];
		if (ifs[b1.node1]>0 && ifs[b1.node2]==ifs[b1.node1]) {
			continue;
		}

		totalDis += b1.dis;
		if (ifs[b1.node1] == 0 && ifs[b1.node2] == 0) {
			ifs[b1.node1] = k;
			ifs[b1.node2] = k++;
		}
		else if (ifs[b1.node1] == 0) {
			ifs[b1.node1] = ifs[b1.node2];
		}
		else if (ifs[b1.node2] == 0) {
			ifs[b1.node2] = ifs[b1.node1];
		}
		else {
			for (int j = 0; j < n; ++j) {
				if (ifs[j] == ifs[b1.node2] && j != b1.node2) {
					ifs[j] = ifs[b1.node1];
				}
			}
			ifs[b1.node2] = ifs[b1.node1];
		}
	}

	cout << totalDis << endl;
	return 0;

}

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

int n, M[MAX][MAX];

int prim() {
    int d[n], p[n];
    int color[n];
    
    for(int i=0; i<n; i++) {
        color[i] = WHITE;
        d[i] = INFTY;
        p[i] = -1;
    }
    d[0] = 0;
    
    while(1) {
        int u=-1;
        int mincost = INFTY;
        for (int i=0; i<n; i++) {
            if (color[i] != BLACK && d[i] < mincost) {
                mincost = d[i];
                u = i;
            }
        }
        
        if (mincost == INFTY) break;
        
        color[u] = BLACK;
        
        for (int v=0; v<=n; v++) {
            if (color[v] != BLACK && M[u][v] != INFTY) {
                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++) {
        if(p[i] != -1) sum += M[i][p[i]];
    }
    return sum;
}

int main() {
    scanf("%d", &n);
    for (int i=0; i<n; i++) {
        for (int j=0; j<n; j++) {
            int tmp;
            scanf("%d", &tmp);
            M[i][j] = (tmp == -1) ? INFTY : tmp;
        }
    }
    
    int sum = prim();
    printf("%d\n", sum);
    
    return 0;
}

C++: #include <bits/stdc++.h>
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, M[MAX][MAX];

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

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

  d[0] = 0;

  while (1) {
    minv = INFTY;
    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] != INFTY ) {
        if ( d[v] > M[u][v] ) {
          d[v] = M[u][v];
          p[v] = u;
          color[v] = GRAY;
        }
      }
    }
  }

  int sum = 0;
  for (int i = 0; i < n; i++) {
    if (p[i] != -1) sum += M[i][p[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;
      M[i][j] = (e == -1) ? INFTY : e;
    }
  }

  cout << prim() << endl;

  return 0;

}