Go: package main

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

const (
	WHITE int = iota
	GRAY
	BLACK
)

type graph struct {
	G [][]int
	C []int
	D []int
	P []int
}

var INFTY = 1 << 32

func constructGraph() *graph {
	sc := bufio.NewScanner(os.Stdin)
	sc.Scan()
	n, _ := strconv.Atoi(sc.Text())

	g := make([][]int, n)
	c := make([]int, n)
	d := make([]int, n)
	p := make([]int, n)

	for i := 0; i < n; i++ {
		sc.Scan()
		fields := strings.Fields(sc.Text())
		g[i] = toIntSlice(fields)
	}

	return &graph{g, c, d, p}
}

func (g *graph) initMst() {
	for i := 0; i < len(g.G); i++ {
		g.C[i] = WHITE
		g.D[i] = INFTY
	}
	g.D[0] = 0
	g.P[0] = -1
}

func (g *graph) primMst() int {
	g.initMst()

	var u int
	for {
		mincost := INFTY
		for i := 0; i < len(g.G); i++ {
			if g.C[i] != BLACK && g.D[i] < mincost {
				mincost = g.D[i]
				u = i
			}
		}

		if mincost == INFTY {
			break
		}

		g.C[u] = BLACK

		for i := 0; i < len(g.G); i++ {
			if g.C[i] != BLACK && g.existEdge(u, i) {
				if g.G[u][i] < g.D[i] {
					g.D[i] = g.G[u][i]
					g.P[i] = u
				}
			}
		}
	}

	totalCost := 0
	for _, cost := range g.D {
		totalCost += cost
	}
	return totalCost
}

func (g *graph) existEdge(u, v int) bool {
	return g.G[u][v] != INFTY && g.G[v][u] != INFTY
}

func toIntSlice(s []string) []int {
	a := make([]int, len(s))
	for i, v := range s {
		num, _ := strconv.Atoi(v)
		if num == -1 {
			num = INFTY
		}
		a[i] = num
	}
	return a
}

func main() {
	g := constructGraph()
	fmt.Println(g.primMst())
}

C++: #include <bits/stdc++.h>
#include <numeric>
#define REP(i, s, n) for (int i = s; i < n; ++i)
#define rep(i, n) REP(i, 0, n)
#define SORT(c) sort((c).begin(), (c).end())
#define SORT_INV(c) sort((c).begin(), (c).end(), greater<int>())
#define IINF INT_MAX
#define LLINF LLONG_MAX
#define DEBUG true
#define LL long long
#define Num 1000000007
// sort(a.begin(), a.end(), std::greater<int>());
using namespace std;
struct edge
{
    int to, cost;
};

typedef pair<int, int> P;
int main()
{
    int n;
    cin >> n;
    vector<edge> g[n];
    rep(i, n)
    {
        rep(j, n)
        {
            int temp = 0;
            edge ed;
            cin >> temp;
            if (temp == -1)
                ed.cost = INT_MAX;
            else
                ed.cost = temp;
            ed.to = j;
            g[i].push_back(ed);
        }
    }
    //solve by  fast prim
    bool used[n];
    int mincost[n];
    rep(i, n)
    {
        used[i] = false;
        mincost[i] = INT_MAX;
    }
    int ans = 0;
    mincost[0] = 0;
    priority_queue<P, vector<P>, greater<P>> que;
    que.push(P(0, 0));
    while (que.empty() == false)
    {
        auto p = que.top();
        que.pop();
        int v = p.second;
        if (mincost[v] < p.first || used[v] == true)
            continue;
        used[v] = true;
        ans += p.first;
        rep(i, g[v].size())
        {
            auto e = g[v][i];
            mincost[e.to] = min(mincost[e.to], e.cost);
            que.push(P(mincost[e.to], e.to));
        }
    }
    cout<<ans<<endl;
    return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
using namespace std;

int root(int i,vector<int> &parent){
    if(parent[i]==-1){
        //親がいない時
        return i;
    }else{
        int result=root(parent[i],parent);
        parent[i]=result; //パス圧縮
        return result;
    }
}

void unite(int x,int y,vector<int> &parent){
    if(root(x,parent)!=root(y,parent)){
        parent[root(y,parent)]=root(x,parent);
    }
}

bool issame(int x,int y,vector<int> &parent){
    return root(x,parent)==root(y,parent);
}

int main(){
    int n;
    cin >> n;
    //first:cost,second:(first:from,second:to)
    vector<pair<int,pair<int,int> > > bridges(n*n);

    //入力
    for(int i=0;i<n;++i){
        for(int j=0;j<n;++j){
            int cost;
            if(cin >> cost,cost!=-1){
                bridges.push_back(pair<int,pair<int,int> >(cost,pair<int,int>(i,j)));
            }
        }
    }

    //sort
    sort(bridges.begin(),bridges.end());

    //島を管理する用
    vector<int> parents(n*n,-1);

    int sum=0;

    for(int i=0;i<bridges.size();++i){
        if(!issame(bridges[i].second.first,bridges[i].second.second,parents)){
            unite(bridges[i].second.first,bridges[i].second.second,parents);
            sum+=bridges[i].first;
        }
    }
    cout << sum;
}

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

using namespace std;

enum status_t {
  WHITE,
  GRAY,
  BLACK,
};

#define GRAPH_SIZE 101

static uint32_t graph[GRAPH_SIZE][GRAPH_SIZE];
static status_t state[GRAPH_SIZE];
static uint32_t shortest_path[GRAPH_SIZE];

size_t minimum_spanning_tree(size_t num)
{
  size_t minimum_cost = 0;

  while (true) {
    uint32_t minimum = UINT32_MAX;
    uint32_t to = 0;
    for (uint32_t j = 0; j < num; j++) {
      if ((shortest_path[j] < minimum) && state[j] != BLACK) {
        minimum = shortest_path[j];
        to = j;
      }
    }

    if (minimum == UINT32_MAX) break;

    state[to] = BLACK;
    minimum_cost += minimum;

    for (uint32_t j = 0; j < num; j++) {
      if (state[j] != BLACK) {
        shortest_path[j] = min(shortest_path[j], graph[to][j]);
      }
    }
  }

  return minimum_cost;
}

#include <iostream>

int32_t main()
{
  uint32_t num;
  cin >> num;

  for (uint32_t i = 0; i < num; i++) {
    for (uint32_t j = 0; j < num; j++) {
      int32_t val;
      cin >> val;
      if (val == -1) {
        graph[i][j] = UINT32_MAX;
      } else {
        graph[i][j] = val;
      }
    }
  }

  cout << minimum_spanning_tree(num) << endl;

  return 0;
}

C++: #include <iostream>
#include <vector>
#include <unordered_set>
#include <utility>
#include <algorithm>
#include <cassert>
using namespace std;

class DisjointSet {
private:
	vector<int> P;		//添え字の番号の頂点の親
	vector< unordered_set<int> > Set;		//添え字の番号の頂点に子として繋がっている頂点の集合
public:
	DisjointSet(int n);
	void unite(int x, int y);
	bool same(int x, int y);
};

DisjointSet::DisjointSet(int n) : P(n), Set(n) {
	for (int i = 0; i<n; ++i) {
		P[i] = i;
		Set[i].insert(i);
	}
}

void DisjointSet::unite(int x, int y) {
	if (P[x] == P[y])
		return;
	int big, small;
	if (Set[P[x]].size() >= Set[P[y]].size()) {
		big = P[x];
		small = P[y];
	}
	else {
		big = P[y];
		small = P[x];
	}
	for (auto i = Set[small].begin(); i != Set[small].end(); ++i) {
		P[*i] = big;
		Set[big].insert(*i);
	}
}

bool DisjointSet::same(int x, int y) {
	return P[x] == P[y];
}

class MSTree : public DisjointSet {
private:
	const int vertex_n;
	vector< pair<int, int> > edge;		//firstは辺の重さ、secondは辺のID(始点・終点)
public:
	MSTree(int n) : DisjointSet(n), vertex_n(n), edge(n*n) {
		for (int i = 0; i < n*n; i++) {
			edge[i].first = -1;
			edge[i].second = i;
		}
	}
	int& GetEdgeWeight(int i, int j) { return edge[vertex_n*i + j].first; }
	int MinEdgeSum();
};

int MSTree::MinEdgeSum() {
	int sum = 0;
	sort(edge.begin(), edge.end());
	for (int i = 0; i < vertex_n*vertex_n; i++) {
		int from = edge[i].second / vertex_n, to = edge[i].second % vertex_n;
		if (edge[i].first == -1 || this->same(from, to))
			continue;
		else {
			this->unite(from, to);
			sum += edge[i].first;
		}
	}
	return sum;
}

int main() {
	int n;
	cin >> n;
	MSTree mst(n);
	for (int i = 0; i < n; i++)
		for (int j = 0; j < n; j++)
			cin >> mst.GetEdgeWeight(i, j);
	cout << mst.MinEdgeSum() << endl;
}