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 <queue>
#include <algorithm>
#include <vector>


using namespace std;


using ll = long long;


const ll MAX = 100;


ll adj[MAX][MAX];
ll d[MAX] = {};
bool finished[MAX] = {};
ll n;


void msp()
{
    priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> pq;
    pq.push(make_pair(0, 0));

    while (!pq.empty()) {
        auto p = pq.top(); pq.pop();
        if (finished[p.second])
            continue;

        for (ll j = 0; j < n; j++) {
            if (adj[p.second][j] == -1 || finished[j])
                continue;
            pq.push(make_pair(adj[p.second][j], j));
        }
        // cout << p.second << ": " << p.first << endl;
        d[p.second] = p.first;
        finished[p.second] = true;
    }
}


int main()
{
    cin >> n;
    for (ll i = 0; i < n; i++) {
        for (ll j = 0;j < n; j++) {
            cin >> adj[i][j];
        }
    }

    msp();

    ll acc = 0;
    for (ll i = 0; i < n; i++)
        acc += d[i];
    cout << acc << endl;
}

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

const int inf = 100000;
const int NIL = -1;

int G[100][100], d[100];
bool color[100];

int main(){
    int n, minc, tag, ans = 0;
    cin >> n;
    for(int i = 0; i < n; i++){
        d[i] = inf;
        color[i] = false;
        for(int j = 0; j < n; j++) cin >> G[i][j];
    }
    d[0] = 0;
    while(true){
        minc = inf;
        for(int i = 0; i < n; i++){
            if(color[i] == false && d[i] < minc){
                minc = d[i];
                tag = i;
            }
        }
        if(minc == inf) break;
        ans += minc;
        color[tag] = true;
        for(int v = 0; v < n; v++){
            if(G[tag][v] != -1){
                if(color[v] == false && G[tag][v] < d[v]){
                    d[v] = G[tag][v];
                }
            }
        }
    }
    cout << ans << endl;
    return 0;
}

C++: #include <bits/stdc++.h>

#define NIL -2
#define INF 1000000001

using namespace std;

void prim(const vector<vector<int> > &link_weight,vector<int> &color, vector<int> &mincost_array,vector<int> &parents){
    int n = mincost_array.size();
    int mincost;
    int u;
    while(true){
        mincost = INF;
        for(int i = 0;i < n;i++){
            if(color[i] != 1 && mincost_array[i] < mincost){
                mincost = mincost_array[i];
                u = i;      
            }
        }
        if(mincost == INF) break;
        color[u] = 1;

        for(int v = 0;v < n;v++){
            if(color[v] != 1 && link_weight[u][v] < INF){
                if(link_weight[u][v] < mincost_array[v]){
                    mincost_array[v] = link_weight[u][v];
                    parents[v] = u;
                    color[v] = 0;
                }
            }
        }
    }
}

int main(){
    int mat_size;
    int temp;
    int cost = 0;

    scanf("%d",&mat_size);
    vector<vector<int> > link_weight(mat_size,vector<int>(mat_size,INF));
    vector<int> color(mat_size,-1);
    vector<int> mincost_array(mat_size,INF);
    vector<int> parents(mat_size,-1);

    for(int i = 0;i < mat_size;i++){
        for(int j = 0;j < mat_size;j++){
            scanf("%d",&temp);
            if(temp != -1) link_weight[i][j] = temp;
        }
    }
    mincost_array[0] = 0;
    prim(link_weight,color,mincost_array,parents);
    for(int i = 0;i < mat_size;i++) cost += mincost_array[i];
    printf("%d\n",cost);

    return 0;   
}

C++: #include <bits/stdc++.h>
typedef long long ll;
typedef unsigned long long ull;
#define reps(i,s,n) for(int (i)=(s);(i)<(n);++(i))
#define rep(i,n) reps(i,0,n)
#define reptr(i,n) for(int (i)=(n);(i)>=0;--(i))
#define All(x) (x).begin(),(x).end()
#define mp make_pair
const ll MOD=1e9+7;
const int MAX = 100;
const int BLACK = 2;
const int GRAY = 1;
const int WHITE = 0;
using namespace std;

int n;
int M[MAX][MAX];

void prim(){
  int color[MAX];
  //d[i] : 自集合からの点iへの最短距離
  int d[MAX];
  //p[i]=s iの親はs
  int p[MAX];
  rep(i,n){
    color[i] = WHITE;
    d[i] = INT_MAX;
    p[i] = -1;
  }
  d[0] = 0;

  while(1){
    int u = -1,minv = INT_MAX;
    rep(i,n){
      if(color[i] != BLACK && minv > d[i]){
        u = i;
        minv = d[i];
      }
    }
    if(u == -1) break;
    color[u] = BLACK;
    rep(v,n){
      if(M[u][v] != INT_MAX && color[v] != BLACK){
        if(d[v] > M[u][v]){
          d[v] = M[u][v];
          p[v] = u;
          color[v] = GRAY;
        }
      }
    }
  }
  int sum = 0;
  rep(i,n){
    if(p[i] != -1) sum += M[i][p[i]];
  }
  cout << sum << endl;
}

int main(){
  cin >> n;
  rep(i,n){
    rep(j,n){
      int tmp;
      cin >> tmp;
      M[i][j] = (tmp == -1) ? INT_MAX : tmp;
    }
  }
  prim();
}

C++: #include <bits/stdc++.h>

using namespace std;

int n;
const int N = 110;
int A[N][N];
typedef pair<int, int> P;
vector<P> G[N];

int min_cost = 0;

void prim() {
    priority_queue<P, vector<P>, greater<P> > heap;
    heap.push({0, 1});
    vector<bool> vis(n + 1, false);
    vector<int> dis(n + 1, INT_MAX);

    while (!heap.empty()) {
        int u = heap.top().second, cost = heap.top().first;
        heap.pop();
        if (vis[u]) continue;
        vis[u] = true;
        min_cost += cost;
        for (int i = 0; i < G[u].size(); i++) {
            int v = G[u][i].second, len = G[u][i].first;
            if (!vis[v] && len < dis[v]) {
                dis[v] = len;
                heap.push({len, v});
            }
        }
    }    
}

void kruskal() {

}

int main() {
    cin >> n;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            cin >> A[i][j];
            if (A[i][j] != -1) {
                G[i].push_back({A[i][j], j});
            }
        }
    }
    
    prim();
    cout << min_cost << endl;

    return 0;
}