Go: package main

import (
	"bufio"
	"container/heap"
	"fmt"
	"os"
	"strconv"
)

type Node struct {
	id     int
	weight int
}

func init() {
	sc.Split(bufio.ScanWords)
}

type NodeHeap []Node

func (h NodeHeap) Len() int           { return len(h) }
func (h NodeHeap) Less(i, j int) bool { return h[i].weight < h[j].weight }
func (h NodeHeap) Swap(i, j int)      { h[i], h[j] = h[j], h[i] }

func (h *NodeHeap) Push(x interface{}) {
	*h = append(*h, x.(Node))
}

func (h *NodeHeap) Pop() interface{} {
	old := *h
	n := len(old)
	x := old[n-1]
	*h = old[0 : n-1]
	return x
}

var sc = bufio.NewScanner(os.Stdin)

func scanInt() int {
	num, _ := strconv.Atoi(scanString())
	return num
}

func scanString() string {
	sc.Scan()
	return sc.Text()
}

func init() {
	sc.Split(bufio.ScanWords)
}

func main() {
	n := scanInt()

	list := make([][]int, n)
	seen := make([]bool, n)
	for i := range list {
		list[i] = make([]int, n)
		for j := range list[i] {
			list[i][j] = scanInt()
		}
	}

	start := Node{id: 0, weight: 0}
	h := &NodeHeap{start}
	heap.Init(h)

	sum := 0
	for h.Len() > 0 {
		node := heap.Pop(h).(Node)
		if seen[node.id] {
			continue
		}
		seen[node.id] = true
		sum += node.weight

		for i, w := range list[node.id] {
			if w < 0 {
				continue
			}
			heap.Push(h, Node{id: i, weight: w})
		}
	}

	fmt.Println(sum)
}

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

int n;
int g[101][101];

int d[101];
char c[101];
int p[101];

void prim() {
    for (int i = 0; i < n; ++i) {
        d[i] = INT_MAX;
        c[i] = 'w';
    }
    c[0] = 'b';
    d[0] = 0;
    while (true) {
        int u, v;
        int mine = INT_MAX;
        for (int j = 0; j < n; ++j) {
            if (c[j] == 'b') {
                u = j;
                for (int k = 0; k < n; ++k) {
                    if(g[u][k] > -1 && g[u][k] <= d[k] && c[k] != 'b' && g[u][k] <= mine) {
                        d[k] = g[u][k];
                        mine = d[k];
                        v = k;
                    }
                }
            }
        }
        p[v] = u;
        c[v] = 'b';
        if(mine == INT_MAX) break;
    }
}

int main() {
    cin >> n;
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            int w;
            cin >> w;
            g[i][j] = w;
        }
    }
    prim();
    int sum;
    for (int k = 0; k < n; ++k) {
        sum += d[k];
    }
    cout << sum << endl;


    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
constexpr int di[] = { 0, 1, 0, -1 }, dj[] = { 1, 0, -1, 0 };
#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, n) for (int i = n; i >= 0; i--)
#define SORT(v) sort((v).begin(), (v).end())
#define SORTR(v) sort((v).rbegin(), (v).rend())
#define all(v) (v).begin(), (v).end()
#define MAX 100
#define vmax 10000
#define sentinel 2000000000
#define INF (1<<30)
constexpr long long int inf = 1000000000000;
constexpr long long int rinf = -(INT64_MAX);
static const int NIL=-1;

//cout<<fixed<<setprecision(20);cin.tie(0);ios::sync_with_stdio(false);

struct edge
{
    int to,cost;
};


int V;                //Vは頂点数
bool used[MAX+1];         //頂点iがxに含まれているか
vector<edge> g[MAX+1];


int prim(int s){
    for(int i=1;i<=V;i++){
        used[i]=false;
    }
    int res=0;
    priority_queue<P,vector<P>,greater<P>> que;
    que.push(P(0,s));
    while(!que.empty()){
        P p=que.top();que.pop();
        int v=p.second;
        if(used[v]) continue;
        res+=p.first;
        used[v]=true;
        for(auto e:g[v]){
            que.push(P(e.cost,e.to));
        }
    }
    return res;
}

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

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

#define rep(i, n) REP(i, 0, n)
#define REP(i, k, n) for(auto i = k; i != n; i++)
#define rrep(i, n) RREP(i, n, 0)
#define RREP(i, n, k) for(auto i = n; i != k; i--)
#define all(x, r) (x), (x)+(r)
#define ALL(x) (x).begin(), (x).end()
#define debug(x) cerr << #x << " " << x << endl
#define exst(x, data) (data).find(x) != (data).end()
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define eb emplace_back
#define np next_permutation
#define b32 bitset<32>

using ll = long long;
using Pii = pair<int,int>;
using Tiii = tuple<int, int, int>;
template<class T>using V = vector<T>;

// class Kruskal {
// private:
//
//
// public:
//     int node_n;
//     vector<vector<int>> cost;
//
//
//     Kruskal(int n) {
//         node_n = n;
//     }
//
// };

const int MAX_V = 101;
int cost[MAX_V][MAX_V];
int mincost[MAX_V];
bool used[MAX_V];
int node_n;

int prim() {
    for(int i=0; i<node_n; i++) {
        mincost[i] = INT_MAX;
        used[i] = false;
    }
    mincost[0] = 0;
    int res = 0;

    while(true) {
        int v = -1;

        for(int u=0; u<node_n; u++) {
            if (!used[u] && (v == -1 || mincost[u] < mincost[v]))
                v = u;
        }
        if (v == -1) break;
        used[v] = true;
        res += mincost[v];

        for(int u=0; u<node_n; u++) {
            mincost[u] = min(mincost[u], cost[v][u]);
        }
    }
    return res;
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);

    int n; cin >> n; node_n = n;
    rep(i,n) rep(j,n) cost[i][j] = INT_MAX;
    rep(i,n) rep(j,n) {
        int c;
        cin >> c;
        if(c != -1) cost[i][j] = c;
    }

    cout << prim() << endl;

    // rep(i,n) {rep(j,n) cout << cost[i][j] << " "; cout << endl;}
    return 0;
}

C++: //Kruskal's algorithm
#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

struct E
{
    int weight;
    int begin;
    int end;
};

struct Unionfind
{
    vector<int> parents;

    Unionfind(int n) : parents(n)
    {
        for (int i = 0; i < n; i++)
        {
            parents[i] = i;
        }
    }

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

    void unite(int x, int y)
    {
        int par_x = root(x);
        int par_y = root(y);
        if (par_x == par_y)
        {
            return;
        }
        parents[par_x] = par_y;
    }

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

int main()
{
    int n;
    cin >> n;
    struct Unionfind tree(n);

    vector<E> e(n * (n - 1) / 2);
    int index = 0;
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            int temp;
            cin >> temp;
            if (i < j)
            {
                e[index].weight = temp;
                e[index].begin = i;
                e[index].end = j;
                index++;
            }
        }
    }

    sort(e.begin(), e.end(),
         [](const E &l, const E &r) {
             return l.weight < r.weight;
         });

    int ans = 0;
    for (int i = 0; i < e.size(); i++)
    {
        if (e[i].weight == -1)
        {
            continue;
        }

        if (tree.same(e[i].begin, e[i].end))
        {
            continue;
        }
        else
        {
            tree.unite(e[i].begin, e[i].end);
            ans += e[i].weight;
        }
    }

    cout << ans << endl;
}

/*

<INPUT>
5
 -1 2 3 1 -1
 2 -1 -1 4 -1
 3 -1 -1 1 1
 1 4 1 -1 3
 -1 -1 1 3 -1

<OUTPUT>
5

*/