Go: package main

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

const (
	WHITE int = iota
	GRAY
	BLACK
)

type graph struct {
	G [][]int
	C []int
	D []int
	P []int
}

var INFTY = 1 << 32

func constructGraph() *graph {
	sc := bufio.NewScanner(os.Stdin)
	sc.Scan()
	n, _ := strconv.Atoi(sc.Text())

	g := make([][]int, n)
	c := make([]int, n)
	d := make([]int, n)
	p := make([]int, n)

	for i := 0; i < n; i++ {
		sc.Scan()
		fields := strings.Fields(sc.Text())
		g[i] = toIntSlice(fields)
	}

	return &graph{g, c, d, p}
}

func (g *graph) initMst() {
	for i := 0; i < len(g.G); i++ {
		g.C[i] = WHITE
		g.D[i] = INFTY
	}
	g.D[0] = 0
	g.P[0] = -1
}

func (g *graph) primMst() int {
	g.initMst()

	var u int
	for {
		mincost := INFTY
		for i := 0; i < len(g.G); i++ {
			if g.C[i] != BLACK && g.D[i] < mincost {
				mincost = g.D[i]
				u = i
			}
		}

		if mincost == INFTY {
			break
		}

		g.C[u] = BLACK

		for i := 0; i < len(g.G); i++ {
			if g.C[i] != BLACK && g.existEdge(u, i) {
				if g.G[u][i] < g.D[i] {
					g.D[i] = g.G[u][i]
					g.P[i] = u
				}
			}
		}
	}

	totalCost := 0
	for _, cost := range g.D {
		totalCost += cost
	}
	return totalCost
}

func (g *graph) existEdge(u, v int) bool {
	return g.G[u][v] != INFTY && g.G[v][u] != INFTY
}

func toIntSlice(s []string) []int {
	a := make([]int, len(s))
	for i, v := range s {
		num, _ := strconv.Atoi(v)
		if num == -1 {
			num = INFTY
		}
		a[i] = num
	}
	return a
}

func main() {
	g := constructGraph()
	fmt.Println(g.primMst())
}

C++: #include <iostream>
#include <algorithm>
#include <string>
#include <fstream>
#include <vector>
#include <cstdio>
#include <cmath>
#include <queue>
#include <stack>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <list>
#include <bitset>

using namespace std;
typedef long long ll;

// #define DEBUG

class Unionfind
{
    vector<int> p;

public:
    Unionfind(int n);
    int find(int x);
    void unite(int x, int y);
    bool match(int x, int y);
};

Unionfind::Unionfind(int n)
{
    for (int i = 0; i < n; i++)
    {
        p.push_back(i);
    }
}

int Unionfind::find(int x)
{
    while (p[x] != x)
    {
        p[x] = p[p[x]];
        x = p[x];
    }
    return x;
}

bool Unionfind::match(int x, int y)
{
    return (find(x) == find(y)) ? true : false;
}

void Unionfind::unite(int x, int y)
{
    x = Unionfind::find(x);
    y = Unionfind::find(y);
    if (x != y)
    {
        p[x] = y;
    }
}

int main()
{
#ifdef DEBUG
    cout << "DEBUG MODE" << endl;
    ifstream in("input.txt"); //for debug
    cin.rdbuf(in.rdbuf());    //for debug
#endif

    int n, d, x, y, ans;
    cin >> n;
    Unionfind tree(n);
    priority_queue<tuple<int, int, int>> pq;
    tuple<int, int, int> w;

    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            cin >> x;
            if (x != -1)
            {
                pq.push(make_tuple(-x, i, j));
            }
        }
    }

    ans = 0;
    while (!pq.empty())
    {
        w = pq.top();
        d = -get<0>(w), x = get<1>(w), y = get<2>(w);
        pq.pop();
        if (!tree.match(x, y))
        {
            tree.unite(x, y);
            ans += d;
        }
    }
    cout << ans << endl;
    return 0;
}

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);
}