Go: package main

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

const (
	white int = iota
	gray
	black
)
const INFTY int = 2001

var G [][]int
var color []int
var d []int
var p []int
var N int

func prim() int {
	u := 0
	d[u] = 0
	for {
		minCost := INFTY
		for i:=0; i<N; i++ {
			if color[i] != black && d[i] < minCost {
				minCost = d[i]
				u = i
			}
		}

		if minCost == INFTY {
			break
		}

		color[u] = black

		for v:=0; v<N; v++ {
			if color[v] != black && G[u][v] != -1 {
				if G[u][v] < d[v] {
					d[v] = G[u][v]
					p[v] = u
					color[v] = gray
				}
			}
		}
	}
	// sum
	sum := 0
	for i:=0; i<N; i++ {
		if p[i] != -1 {
			sum += G[i][p[i]]
		}
	}
	return sum
}




func initVariable(N int) {
	G = make([][]int, N)
	for i:=0; i<N; i++ {
		G[i] = make([]int, N)
	}
	color = make([]int, N)
	d = make([]int, N)
	p = make([]int, N)
	for i:=0; i<N; i++ {
		d[i] = INFTY
		p[i] = -1
	}
}

func nextInt(scanner *bufio.Scanner) int {
	scanner.Scan()
	num, _ := strconv.Atoi(scanner.Text())
	return num
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	N = nextInt(scanner)
	initVariable(N)
	for i:=0; i<N; i++ {
		for j:=0; j<N; j++ {
			G[i][j] = nextInt(scanner)
		}
	}
	fmt.Printf("%d\n", 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;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <utility>
using namespace std;
int P[10010];

void init(int N)
{
    for(int i = 1; i <= N; ++i)
    {
        P[i] = i;
    }
}
 
int root(int a)
{
    if(P[a] == a)
    {
        return a;
    }
    return (P[a] = root(P[a]));
}
 
bool is_same_set(int a, int b)
{
    return root(a) == root(b);
}
 
void unite(int a, int b)
{
    P[root(a)] = root(b);
}

int main()
{
	int n = 0;
	cin >> n; 
	int cost[5000];
	int src[5000];
	int dst[5000];
	int A[100 + 10][100 + 10];
	int M = 0;
	pair<int,int> p;
	vector<pair<int,int>> edges;

	for(int r = 0; r < n; ++r)
	{
		for(int c = 0; c < n; ++c)
		{
			cin >> A[r][c];
		}
	}
	
	for (int r = 0; r < n; ++r)
	{
		for(int c = r + 1; c < n; ++c)
		{
			if(A[r][c] > -1)
			{
				cost[M] = A[r][c];
				src[M] = r;
				dst[M] = c;
				edges.push_back(make_pair(cost[M],M));
				M += 1;
			}
		}
	}
	sort(edges.begin(),edges.end());
	int sum = 0;
	init(n);
	for(int i = 0; i < M; ++i)
	{
		if(is_same_set(src[i], dst[i]) == 1) continue;
		else
		{
			unite(src[i], dst[i]);
			sum += edges[i].first;
		}
	}
	cout << sum << endl;
}

C++: #include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
const int MAXN = (int)1e2 + 10;

int m,n;
int p[MAXN];
int a[MAXN][MAXN];
int vis[MAXN];
int d[MAXN];
const int INF = (int) 0x3f3f3f3f;

void work(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++) p[i]=i;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            scanf("%d",&a[i][j]);
        }
    }
    int ans=0;
    int cur=1,cnt=1;
    vis[1]=1;
    for(int i=2;i<=n;i++) d[i]=INF;
    for(int i=1;i<=n;i++)if(a[1][i]!=-1) d[i]=a[1][i];
    while(cnt<n){
        int id=0;
        for(int i=1;i<=n;i++)if(!vis[i]){
            if(id==0||d[id]>d[i]){
                id=i;
            }
        }
        vis[id]=1;
        cnt++;
        ans+=d[id];
        for(int i=1;i<=n;i++)if(!vis[i]&&a[id][i]!=-1){
            d[i]=min(d[i],a[id][i]);
        }
    }
    if(cnt==n) printf("%d\n",ans);
    else printf("-1\n");
}

int main(){
    work();
    return 0;
}

/*
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
*/

C++: #include <iostream>
#include <limits>

constexpr int MAXN = 100;
int G[MAXN][MAXN];
int D[MAXN];
int P[MAXN];
int C[MAXN];
int n;
constexpr int MAXINT = 2001;

#define FOR(i,a,b) for(int i = (a); i<(b); ++i)

int main() {

    std::cin >> n;
    FOR(i, 0, n)
    FOR(j, 0, n)
    {
        int v;
        std::cin >> v;
        if(v < 0) G[i][j] = MAXINT;
        else G[i][j] = v;
    }

    FOR(i, 0, n) D[i] = MAXINT;

    int r = 0;
    D[r] = 0;
    P[r] = -1;
    while (true) {
        int mincost = MAXINT;
        int t;
        for (int i = 0; i < n; i++) {
            if ((C[i] == 0) && (D[i] < mincost)) {
                mincost = D[i];
                t = i;
            }
        }
        if (mincost == MAXINT) {
            break;
        }

        C[t] = 1;
        P[t] = r;

        for (int i = 0; i < n; i++) {
            if ((C[i] == 0) && (G[t][i] < D[i])) {
                D[i] = G[t][i];
            }
        }
        r = t;
    }

    int sum = 0;
    FOR(i, 0, n) {
        sum += D[i];
    }
    std::cout << sum << std::endl;

}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <queue>
#include <stack>
#include <map>
#define FOR(i,b,e) for(int i=b;i<e;i++)
#define REP(i,n) FOR(i,0,n)
#define INFTY 1000000
using namespace std;

const int N = 100;
int n;
int D[N], M[N][N];
bool V[N];

int prim() {
  int nn, minD, cost;

  REP(i, n) D[i] = INFTY;
  D[0] = 0;

  while (true) {
    minD = INFTY;
    REP(i, n) {
      if (!V[i] && D[i] < minD) {
        minD = D[i];
        nn = i;
      }
    }
    if (minD == INFTY) break;

    cost += D[nn];
    V[nn] = true;
    REP(i, n) {
      if (!V[i] && M[nn][i] != -1 && M[nn][i] < D[i]) {
        D[i] = M[nn][i];
      }
    }
  }

  return cost;
}

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

  cin >> n;
  REP(i, n) {
    REP(j, n) {
      cin >> M[i][j];
    }
  }

  cout << prim() << endl;
}