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>
using namespace std;

struct Data{

  int weight;
  int s,g;

  bool operator < (const Data& p)const {
    if(weight != p.weight)return weight > p.weight;
    if(s != p.s)return s > p.s;
    return g > p.g;
  }
  
};

int par[100],rank[100];
priority_queue<Data> data;

int find(int x){
  
  if( x == par[x]){
    return x;
  }else{
    par[x]=find(par[x]);
  }
  return par[x];
}
bool same(int x,int y){
  if(find(x) == find(y)){
    return true;
  }else{
    return false;
  }
}
void unite(int x,int y){
  
  int x_oya,y_oya;
  
  x_oya=find(x);
  y_oya=find(y);
  
  if(rank[x_oya] > rank[y_oya]){
    par[x_oya]=par[y_oya];
  }else if(rank[x_oya] < rank[y_oya]){
    par[y_oya]=par[x_oya];
  }else{
    par[y_oya]=par[x_oya];
    rank[x_oya]++;
  }
  
}
int kruskal(void){
  
  Data a;
  int cost=0;
  
  while(!data.empty()){
    a=data.top();
    data.pop();
    if(!same(a.s,a.g)){
      cost+=a.weight;
      unite(a.s,a.g);
    }
  }
  return cost;
  
}
int main(){
  
  int n,weight;
  
  for(int i=0 ; i < 100 ; i++){
    par[i]=i;
    rank[i]=0;
  }
  
  cin >>n;

  for(int i=0 ; i < n ; i++){
    for(int j=0 ; j < n ; j++){
      cin >>weight;
      if(weight == -1)continue;
      data.push((Data){weight,i,j});
    }
  }
  
  cout <<kruskal()<<endl;

  return 0;
}

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

const int N=105;
int g[N][N];
int dist[N];
bool vis[N];
int n, ans=0;

void prim(){
    dist[0]=0;
    for(int i=0; i<n; ++i){
        int node=-1;
        for(int j=0; j<n; ++j){
            if(!vis[j] && (node==-1 || dist[j]<dist[node])) node=j;
        }
        if(node==-1) break;
        vis[node]=true;
        ans+=dist[node];
        for(int j=0; j<n; ++j) 
            if(g[node][j]!=-1) dist[j]=min(dist[j], g[node][j]);
    }
}

int main(){
    memset(dist, 0x3f, sizeof dist);
    memset(vis, 0x00, sizeof vis);
    cin>>n;
    for(int i=0; i<n; ++i)
        for(int j=0; j<n; ++j)
            cin>>g[i][j];
            
    prim();
    cout<<ans<<endl;
    return 0;
}

C++: #include <stdio.h>
#include <iostream>
#include <vector>
using namespace std;

#define N_MAX 100
#define WHITE 1
#define GREY 2
#define BLACK 3
#define INF 1000000000
#define NIL -1

int G[N_MAX][N_MAX];
int V[N_MAX];
int V_value[N_MAX];

void init(void){
    for(int i = 0; i < N_MAX; i++){
        V[i] = WHITE;
        V_value[i] = INF;
    }
}

int main(void){
    init();
    int n;
    scanf("%d\n", &n);

    for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++){
            scanf("%d", &G[i][j]);
        }
    }

    V[0] = BLACK;
    V_value[0] = 0;
    int new_node = 0;
    int sum = 0;
    while(1){
        for(int i = 0; i < n; i++){
            if(G[new_node][i] != -1 && V[i] == WHITE){
                V_value[i] = min(V_value[i], G[new_node][i]);
            }
        }

        int min_value = INF;
        int min_index = NIL;
        for(int i = 0; i < n; i++){
            if(V[i] == WHITE){
                if(V_value[i] < min_value){
                    min_index = i;
                    min_value = V_value[i];
                }
            }
        }
        if(min_index == NIL){
            break;
        }else{
            V[min_index] = BLACK;
            new_node = min_index;
            sum += min_value;
        }
    }

    printf("%d\n", 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;

  for (uint32_t i = 0; i < num; i++) {
    shortest_path[i] = UINT32_MAX;
    state[i] = WHITE;
  }

  shortest_path[0] = 0;
  state[0] = GRAY;

  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 <queue>
#include <vector>
using namespace std;

const int MAX_V = 100010;
struct edge{ int cost, to; };


int V;
vector<edge> G[MAX_V];
bool used[MAX_V];

auto compare = [](edge a, edge b){
    return a.cost > b.cost;
};

int res = 0;
int prim_pq(int s){
    for(int i = 0; i < V; i++) used[i] = false;
    used[s] = true;
    
    priority_queue<edge,vector<edge>,decltype(compare)> pq{compare};
    for(int i = 0; i < G[s].size(); i++) pq.push(G[s][i]);
    
    while(!pq.empty()){
        edge e = pq.top(); pq.pop();
        if(used[e.to]) continue;
        used[e.to] = true;
        res += e.cost;
        for(int i = 0; i < G[e.to].size(); i++) pq.push(G[e.to][i]);
    }
    return res;
}

int main(){
    cin >> V;
    for(int i = 0; i < V; i++){
        for(int j = 0; j < V; j++){
            int x; cin >> x;
            if(x == -1) continue;
            edge e;
            e.to = j; e.cost = x;
            G[i].push_back(e);
        }
    }
    cout << prim_pq(0) << endl;
    return 0;
}