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 <map>
#include <unordered_map>
#include <queue>
#include <set>
#include <algorithm>
#include <string>
#include <math.h>
#include <limits.h>
#include <stack>
#include <complex>
#include <stdlib.h>
#include <stdio.h>
#include <functional>
#include <cfloat>
#include <math.h>
#include <numeric>
#include <string.h>
#include <sys/time.h>
#include <random>


#define fs first
#define sc second

using namespace std;

typedef long long ll;
typedef unsigned int uint;
typedef pair<ll, ll> P;


class Graph{
    struct edge{
        int vertice;
        ll weight;
    };

    vector<vector<edge>> g;
    unsigned int n;

public:
    Graph(unsigned int n): n{n}{
        g.resize(n);
    }

    void add_edge(int u, int v){
        g[u].emplace_back(edge{v, 1});
    };

    void add_edge(int u, int v, ll w){
        g[u].emplace_back(edge{v, w});
    }

    vector<edge>& get_edge(int v){
        return g[v];
    }

    unsigned int get_num_vertices() const{
        return n;
    }

    unsigned int size() const{
        return n;
    }

    vector<edge>& operator[](int v){
        return get_edge(v);
    }
};


/**
 * prim法 (O(|E|log(|V|))のアルゴリズム)
 * @param g 連結グラフ
 * @return 連結グラフの最小域木をグラフとして返す
 */
Graph prim2_g(Graph g){
    // Ptmp.fsが今の部分木からのコスト、Ptmp.ｓｃ.fsが次の頂点、Ptmp.sc.scが次の頂点に辺を伸ばす部分木の頂点
    typedef pair<ll, pair<int, int>> Ptmp;

    unsigned int n = g.size();

    Graph mst(n);   // minimum spanning tree
    vector<ll> used(n, false);
    priority_queue<Ptmp, vector<Ptmp>, greater<Ptmp> > que;

    used[0] = true;
    for(auto e: g[0]){
        que.push(Ptmp(e.weight, pair<int, int>(e.vertice, 0)));
    }

    while(!que.empty()){
        Ptmp p = que.top(); que.pop();
        int to = p.second.first, from = p.second.second;
        if(used[to]) continue;

        used[to] = true;
        mst.add_edge(from, to, p.first);
        mst.add_edge(to, from, p.first);

        for(auto e: g[to]){
            if(!used[e.vertice]){
                que.push(Ptmp(e.weight, pair<int, int>(e.vertice, to)));
            }
        }
    }

    return mst;
}


ll prim2(Graph g){
    unsigned int n = g.size();
    Graph mst = prim2_g(g);

    ll res = 0;
    for(int i = 0; i < n; i++){
        for(auto e: mst[i]){
            res += e.weight;
        }
    }

    return res / 2;
}



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.add_edge(i, j, x);
            }
        }
    }

    cout << prim2(g) << endl;

    return 0;
}

C++: #include <iostream>

using namespace std;
static const int MAX=100;
static const int INF=1<<21;

int p[MAX]; //????°???????????????????????????¶??????
int d[MAX]; //??????????¨??????????????????????????????????????????????
int M[MAX][MAX]; //u???v????????????
bool visited[MAX]; //??????????¢??????\

int prim(int n)
{
    //init
    for(int i=0;i<n;i++)
    {
        p[i]=-1;
        visited[i]=false;
        d[i]=INF; //????§???????????????????d?????????????????§
    }

    d[0]=0; //?¬¬0?????????d?????´??°
    while(true)
    {
        //??????d???????°????????????\??????
        int min=INF;
        int min_point=-1;
        for(int i=0;i<n;i++)
        {
            if(d[i]<min&&visited[i]==false)
            {
                min_point=i;
                min=d[i];
            }
        }
        if(min==INF) break;
        visited[min_point]=true;
        //??´??°???????????°?????\????????????????????????d???
        for(int i=0;i<n;i++)
        {
            if(M[min_point][i]!=INF&&visited[i]==false)
                if(d[i]>M[min_point][i])
                {
                    d[i] = M[min_point][i]; //??????d????????§????°±????????´??°
                    p[i]=min_point; //??????
                }
        }
    }
    int sum=0;
    for(int i=0;i<n;i++)
        if(p[i]!=-1) //??????????°?????????????????????¶
           sum+=M[i][p[i]];
    return sum;
}

int main()
{
    int n;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            cin>>M[i][j];
            M[i][j]=(M[i][j]==-1?INF:M[i][j]);
        }
    }
    cout<<prim(n)<<endl;
}

C++: #include <iostream>

using namespace std;
int n;
int dat[100][100];
int cost[100];
int done[100];

void prim(){
	for(int i=0;i<n;i++){
		cost[i]=1<<21;
		done[i]=0;
	}
	int s=0;	
	int next, ncost=1<<21;
	done[s]=1;
	cost[s]=0;
	while(1){
		ncost=1<<21;
		for(int j=0;j<n;j++){
			if(done[j]){
				for(int i=0;i<n;i++){
					if(dat[j][i]!=-1){
						if(cost[i]>=dat[j][i]&&!done[i]){
							cost[i]=dat[j][i];
						}
						if(ncost>cost[i]&&!done[i]){
							next=i;
							ncost=cost[i];
						}
					}
				}
			}
		}
//		cout<<"next:"<<next<<" cost:"<<cost[next]<<endl;
		done[next]=1;
		int flag=true;
		for(int i=0;i<n;i++){
			if(!done[i]) flag = false;
		}
		if(flag)
			return;
	}
}


int main(){
	cin>>n;
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			cin>>dat[i][j];
		}
	}
	prim();
	int sum=0;
	for(int i=0;i<n;i++){
		sum+=cost[i];
	}
	cout<<sum<<endl;
	return 0;
}