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>
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;
}

C++: #include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;

#define WHITE 0
#define GRAY 1
#define BLACK 2
#define INF 2000000000

int x[10000][10000];
int color[10000];
int MIN[10000];
int sum;
int sum_min = INF;

int main() {
	int n, a, b, c;
	cin >> n;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			cin >> x[i][j];
		}
	}
	for (int i = 0; i < n; i++) {
		memset(MIN, 0, sizeof(MIN));
		b = INF;
		a = 1;
		color[i] = GRAY;
		MIN[i] = 0;
		while (a >= 1) {
			for (int j = 0; j < n; j++) {
				if (color[j] = GRAY) {
					if (b > MIN[j]) {
						b = MIN[j];
						c = j;
					}
				}
			}
			color[c] = BLACK;
			a -= 1;
			for (int j = 0; j < n; j++) {
				if (color[j] != BLACK && x[c][j] != -1) {
					if (color[i] == WHITE) {
						a += 1;
					}
					color[j] = GRAY;
					MIN[j] = min(x[c][j], MIN[j]);
				}
			}
		}
		sum = 0;
		for (int j = 0; j < n; j++) {
			sum += MIN[j];
		}
		sum_min = min(sum_min, sum);
	}
	cout << sum_min << endl;
	return 0;
}

C++: #define _CRT_SECURE_NO_WARNINGS

#include<iostream>
#include<functional>
#include<algorithm>
#include<stack>
#include<queue>
#include<iomanip>
#include<set>
#include<map>
#include<utility>
#include<string>
#include<vector>

using namespace std;

#define LL long long
const int dx[4] = { 1,-1,0,0 };
const int dy[4] = { 0,0,1,-1 };
bool OOA(int x, int y, int H, int W) { return (0 <= x && x<H) && (0 <= y && y<W); }

struct UnionFind {
	int V;
	vector<int>ne;
	void init() { for (int i = 0;i<V;i++)ne.push_back(i); };

	int root(int x) {
		if (ne[x] == x)return x;
		return ne[x] = root(ne[x]);
	}
	bool same(int x, int y) { return root(x) == root(y); }
	void unite(int x, int y) {
		if (same(x, y))return;
		if (x>y)swap(x, y);
		ne[ne[y]] = ne[x];
		return;
	}
};

struct Edge{
	int fr,to,val;
	bool operator < (Edge x){
		return (val<x.val);
	}
};

vector<Edge>e;
int n;

int MST() {//最小全域木の重みの総和
	int ret = 0;
	UnionFind uf;
	uf.V = n;
	uf.init();
	sort(e.begin(), e.end());
	for (int i = 0;i<e.size();i++) {
		if (uf.same(e[i].fr, e[i].to))continue;
		ret += e[i].val;
		uf.unite(e[i].fr, e[i].to);
	}
	return ret;
}


int main() {
	cin >> n;
	e = vector<Edge>(n);
	for (int i = 0;i<n;i++) {
		for (int j = 0;j<n;j++) {
			int a;
			cin >> a;
			if (a != -1) e.push_back({ i,j,a });
		}
	}

	cout << MST() << endl;
	return 0;
}

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

using Weight = int;
using Capacity = int;
struct Edge {
    int src, dst;
    Weight weight;
    Edge(int src_, int dst_, int weight_) :
        src(src_), dst(dst_), weight(weight_) { }
};
using Edges = vector<Edge>;
using Graph = vector<Edges>;
using Array = vector<Weight>;
using Matrix = vector<Array>;

bool operator < (const Edge &e, const Edge &f){ return e.weight > f.weight; }
pair<Weight, Edges> prim(const Graph &g, int r = 0) {
    Edges T; Weight total = 0; vector<int> vis(g.size());
    priority_queue<Edge> q;
    q.emplace(-1, r, 0);
    while (!q.empty()) {
        Edge e = q.top(); q.pop();
        if (vis[e.dst]) continue;
        vis[e.dst] = true;
        total += e.weight; if(e.src != -1) T.push_back(e);
        for(auto& f : g[e.dst]) if (!vis[f.dst]) q.push(f);
    }
    return make_pair(total, T);
}

int main(){
    int n;
    cin >> n;
    Graph g(n);
    for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++){
            int x;
            cin >> x;
            if(x != -1) g[i].emplace_back(i,j,x);
        }
    }
    cout << prim(g).first << endl;
}

C++: // ALDS1_12_A.cpp
// Graph II - Minimum Spanning Tree
#include <iostream>
using namespace std;

const int maxN = 100;
const int INF = 2001;
int w[maxN][maxN];
int color[maxN];
int n;
static const int WHITE = 0;
static const int BLACK = 1;

int prim(int x)
{
    int res = 0;
    int cnt = 1;
    color[x] = BLACK;
    while (cnt++ != n) {
        int minCost = INF;
        int pos = 0; // ??°?????°?????????
        for (int i = 0; i < n; i++) {
            if (color[i] != BLACK) continue;
            for (int j = 0; j < n; j++) {
                if (color[j] != WHITE) continue;
                if (w[i][j] < minCost) {
                    pos = j;
                    minCost = w[i][j];
                }
            }
        }
        res += minCost;
        color[pos] = BLACK;
    }
    return res;
}

int main()
{
    cin >> n;
    int num;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            cin >> num;
            if (num == -1) w[i][j] = INF;
            else w[i][j] = num;
        }
    }
    cout << prim(0) << endl;
    return 0;
}