Go: package main

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

const (
	MAX   = 100
	INFTY = 1 << 21
	NIL   = -1
)

const (
	WHITE = iota
	GRAY
	BLACK
)

type Graph [MAX][MAX]int

func (g *Graph) prim(n int) int {
	var (
		u, minv     = 0, 0
		d, p, color [MAX]int
	)

	for i := 0; i < n; i++ {
		d[i] = INFTY
		p[i] = -1
		color[i] = WHITE
	}
	d[0] = 0

	for {
		minv = INFTY
		u = -1
		for i := 0; i < n; i++ {
			if minv > d[i] && color[i] != BLACK {
				u = i
				minv = d[i]
			}
		}

		if u == -1 {
			break
		}

		color[u] = BLACK
		for v := 0; v < n; v++ {
			if color[v] != BLACK && g[u][v] != INFTY {
				if d[v] > g[u][v] {
					d[v] = g[u][v]
					p[v] = u
					color[v] = GRAY
				}
			}
		}
	}

	sum := 0
	for i := 0; i < n; i++ {
		if p[i] != -1 {
			sum += g[i][p[i]]
		}
	}

	return sum
}

func answer(reader io.Reader) string {
	sc := bufio.NewScanner(reader)
	sc.Split(bufio.ScanWords)

	sc.Scan()
	n, _ := strconv.Atoi(sc.Text())

	var g Graph
	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			sc.Scan()
			e, _ := strconv.Atoi(sc.Text())
			if e == -1 {
				g[i][j] = INFTY
				continue
			}
			g[i][j] = e
		}
	}

	return fmt.Sprint(g.prim(n))
}

func main() {
	fmt.Println(answer(os.Stdin))
}

C++: #include<iostream>
using namespace std;
#define MAX 100
#define INFTY 1<<21
#define WHITE 0
#define GRAY 1
#define BLACK 2

int n, M[MAX][MAX];

int prim()
{
    int u,minv;
    int d[MAX],p[MAX],color[MAX];

    for(int i=0;i<n;i++)
    {
        d[i]=INFTY;
        p[i]=-1;
        color[i]=WHITE;
    }

    d[0]=0;
    while(1)
    {
        minv=INFTY;
        u=-1;
        for(int i=0;i<n;i++)
        {
            if(minv>d[i] && color[i]!=BLACK)
            {
                u=i;
                minv=d[i];
            }
        }
        if(u==-1)break;

        color[u]=BLACK;
        for(int v=0;v<n;v++)
        {
            if(color[v]!=BLACK && M[u][v]!=INFTY)
            {
                if(d[v]>M[u][v])
                {
                    d[v]=M[u][v];
                    p[v]=u;
                    color[v]=GRAY;
                }
            }
        }
    }
    int sum=0;
    for(int i=0;i<n;i++)
    {
        if(p[i]!=-1)sum+=M[i][p[i]];
    }
    return sum;
}

int main()
{
    cin >> n;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            int e;
            cin >>e;
            if(e==-1)
            {
                M[i][j]=INFTY;
            }
            else
            {
                M[i][j]=e;
            }
        }
    }

    cout << prim() << endl;
    return 0;
}

C++: #include <iostream>
#include <utility>
#include <string>
#include <algorithm>
#include <set>
#include <deque>
#include <vector>
#define MAX 150
#define rep(i,p) for(int i=0;i<p;i++)
#define rep1(i,p) for(int i=1;i<=p;i++)
#define pb push_back
using namespace std;
int n,temp,a,b,size;
int A[MAX];
int B[MAX];
int W[MAX];
int V[MAX];
pair<int,int> edge[MAX];
int main(){
  size = 0;
  cin >> n;
  rep(i,n) 
    rep(j,n) {
      cin >> W[size];
      if (i <= j) {        
        A[size] = i;
        B[size] = j;
        edge[size].first = W[size];
        edge[size].second = size;
        size++;
      }
    }

  // sort by lenght of edge
  sort(edge, edge+size+1);
  // printing
  // rep(i,size+1) {
  //   cout << A[edge[i].second] << " " << B[edge[i].second]
  //        << " " << edge[i].first << endl;;
  // }
  int sum = 0;
  rep(i,n) V[i] = i;
  rep(i,size) {
    if (edge[i].first == -1) continue;
    temp = edge[i].second;
    // a , b : vertex
    a = A[temp]; 
    b = B[temp];
    if (V[a] != V[b]) {
      // ?????????????????????
      sum += edge[i].first;
      if (V[a] != a && V[b] == b){
        V[b] = V[a];
      } else if (V[a] == a && V[b] != b) {
        V[a] = V[b];
      } else if (V[a] == a && V[b] == b) {
        V[b] = V[a];
      }
    } else {

    }
  }
  cout << sum << endl;
  return 0;
}

C++: #include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;

#define MIN(a,b) ((a)<(b)?(a):(b))
#define MAX(a,b) ((a)>(b)?(a):(b))

struct NODE {
    int number;
    NODE* root;
};

struct EDGE {
    int n1;
    int n2;
    int w;
};

int findRoot(NODE& n) {
    NODE temp_node=n;
    while(temp_node.root!=nullptr) {
        temp_node=*(temp_node.root);
    }
    return temp_node.number;
}

int main()
{
    int n;
    int rt1,rt2;
    int i,j;
    int w;
    EDGE temp_e;
    NODE temp_node;
    vector<EDGE> e;
    vector<NODE> node;

    scanf("%d",&n);
    for(i=0; i<n; ++i) {
        temp_node.number=i;
        temp_node.root=nullptr;
        node.push_back(temp_node);
        for(j=0;j<n;++j) {
            scanf("%d",&w);
            if(w!=-1 && i<j) {
                temp_e.n1=i;
                temp_e.n2=j;
                temp_e.w=w;
                e.push_back(temp_e);
            }
        }
    }
    sort(e.begin(),e.end(),[](EDGE& lhs,EDGE& rhs) { return (lhs.w==rhs.w)?(lhs.n1<rhs.n1):(lhs.w<rhs.w); });
    w=0;
    for(auto itr=e.begin(); itr!=e.end(); ++itr) {
        rt1=findRoot(node[itr->n1]);
        rt2=findRoot(node[itr->n2]);
        if(rt1!=rt2) {
            w+=itr->w;
            node[MAX(rt1,rt2)].root = &node[MIN(rt1,rt2)];
        }
    }
    printf("%d\n",w);
    return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <queue>

#define INPUT_FROM_FILE (0) 
#if INPUT_FROM_FILE
#include <fstream>
#endif

class UnionFindTree{
public:
    std::vector<int> parent;
    std::vector<int> rank;
 
    UnionFindTree(int n){
        parent.resize(n, 0);
        rank.resize(n, 0);
 
        for(int i = 0; i < n; i++){
            parent[i] = i;
        }
        return;
    }
    ~UnionFindTree(){
        return;
    }
 
    int find(int x){
        if(parent[x] == x){
            return x;
        }
        else{
            return parent[x] = find(parent[x]);
        }
    }
 
    void unite(int x, int y){
        x = find(x);
        y = find(y);
 
        if(x == y){
            return;
        }
 
        if(rank[x] < rank[y]){
            parent[x] = y;
        }
        else{
            parent[y] = x;
            if(rank[x] == rank[y]){
                rank[x]++;
            }
        }
    }
 
    bool is_same(int x, int y){
        return find(x) == find(y);
    }
};

class Edge{
public:
	int u;
	int v;
	int cost;

	Edge(){
		return;
	}
	
	~Edge(){
		return;
	}
	bool operator < (const Edge &r){
		return this->cost < r.cost;
	}
};


int main(int argc, char **argv){
	int n;
	std::vector<int> A;
	std::vector<Edge> edges;

#if INPUT_FROM_FILE
	std::ifstream ifs("test.txt");
	//FILE *fp = fopen("test.txt", "r");
#endif

#if INPUT_FROM_FILE
	ifs >> n;
#else
	std::cin >> n;
#endif

	A.resize(n * n);
	for(int i = 0; i < n; i++){
		int offset = i * n;
		for(int j = 0; j < n; j++){
#if INPUT_FROM_FILE
			ifs >> A[offset + j];
#else
			std::cin >> A[offset + j];
#endif
		}
	}

	for(int i = 0; i < n; i++){
		int offset = i * n;
		for(int j = i + 1; j < n; j++){
			if(A[offset + j] >= 0){
				Edge e;
				e.u = i;
				e.v = j;
				e.cost = A[offset + j];

				edges.push_back(e);
			}
		}
	}

	std::sort(edges.begin(), edges.end());
   UnionFindTree uft(n);
   int result = 0;
 
    for(int i = 0; i < edges.size(); i++){
        Edge &e = edges[i];
        if(!uft.is_same(e.u, e.v)){
            uft.unite(e.u, e.v);
            result += e.cost;
        }
    }
 
    std::cout << result << std::endl;
	return 0;
}

C++: import heapq

n = int(input())
#A = [[int(i) for i in input().split()] for i in range(n)]
h = []
DP = {}
heapq.heappush(h,(0,0))
c = 0
while(len(DP)<n):
    tmp = heapq.heappop(h)
    if tmp[1] in DP:
        continue
    c += tmp[0]
    DP[tmp[1]] = 1
    for i,j in enumerate(A[tmp[1]]):
        if j > 0 and not i in DP:
            heapq.heappush(h,(j,i))
print(c)