Go: package main

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

const (
	MAX = 100
	INF = 1 << 21
	WHITE = 0
	GRAY = 1
	BLACK = 2
)

var (
	n int
	M [MAX][MAX]int
)

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

	for i := 0; i < n; i++ {
		d[i] = INF
		p[i] = -1
		color[i] = WHITE
	}
	d[0] = 0
	for {
		minv = INF
		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 && M[u][v] != INF {
				if d[v] > M[u][v] {
					d[v] = M[u][v]
					p[v] = u
					color[v] = GRAY
				}
			}
		}
	}
	sum := 0
	for i := 0; i < n; i++ {
		if p[i] != -1 {
			sum += M[i][p[i]]
		}
	}
	return sum
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Scan()
	n, _ = strconv.Atoi(scanner.Text())
	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			scanner.Scan()
			e, _ := strconv.Atoi(scanner.Text())
			if e == -1 {
				M[i][j] = INF
			} else {
				M[i][j] = e
			}
		}
	}
	fmt.Println(prim())
}

C++: #include <stdio.h>
#include <vector>
#include <list>
#include <map>
#include <array>
#include <memory>
#include <string>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <cmath>
#include <unordered_map>
#include <stdexcept>
#include <numeric>
#include <queue>

using std::string;
using std::vector;


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// util
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

std::vector<std::string> split(const std::string& input, char delimiter)
{
    std::istringstream stream(input);

    std::string field;
    std::vector<std::string> result;
    while (std::getline(stream, field, delimiter)) {
        result.push_back(field);
    }
    return result;
}



//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// user func
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void printList(const vector<int>& list) {
    // print
    for(int i = 0; i < list.size(); i++){
        printf("%d", list[i]);
        if ( i + 1 != list.size() ) {
            printf(" ");
        }
    }
    printf("\n");
}



//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// logic
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

// 要素数固定で汎用性低し
// index = key, value = parent key
class UnionFindTree{
private:
    vector<int> list;
public:
    UnionFindTree(int num)
        :list(num+1)
    {
        for(int i = 0; i < this->list.size(); i++) {
            list[i] = i;
        }
    }

    int Find(int key) {
        if (list[key] == key) {
            return key;
        } else {
            return Find(list[key]);
        }
    }
    bool AreSame(int lhs, int rhs){
        return Find(lhs) == Find(rhs);
    }
    void Union(int lhs, int rhs){
        this->list[Find(lhs)] = this->list[Find(rhs)];
    }

    void Print() {
        for(int i = 1; i < this->list.size(); i++) {
            printf("%d -> %d\n", i, this->list[i]);
        }
    }
};

class Node{
public:
    Node(int id, int numEdge)
     : id(id)
     , edges(vector<int>(numEdge + 1))
    {
        // constructer
    }
    void Edge(int id, int weight){
        this->edges[id] = weight;
    }
    vector<int> Edges() {
        return this->edges;
    }

    int id;
    bool done = false;
    vector<int> edges;
};

typedef struct _Relation{
    _Relation(int weight, int from, int to)
        : weight(weight)
        , from(from)
        , to(to)
    {}
    int weight;
    int from;
    int to;
} Relation;

class List{
public:
    // constructer
    List(int numNode)
    {
        this->nodes.push_back(Node(0, 0));
        for (int i = 1; i <= numNode; i++) {
            this->nodes.push_back(Node(i, numNode));
        }
    }

    void SetWeight(int from, int to, int weight)
    {
        this->nodes[from].Edge(to, weight);
    }
    
    int CalcWeight() {
        UnionFindTree tree(this->nodes.size() - 1); // init by num elements
        std::list<Relation> costs;
        for (int i = 1; i < this->nodes.size(); i++) {
            for (int j = 1; j < this->nodes[i].Edges().size(); j++) {
                int cost = this->nodes[i].Edges()[j];
                if(cost == -1) continue;
                costs.push_back(Relation(this->nodes[i].Edges()[j], i, j));
            }
        }
        costs.sort([](const Relation& lhs, const Relation& rhs){ return lhs.weight < rhs.weight; });
        vector<Relation> result;
        while(!costs.empty()) {
            auto min = costs.front();
            costs.pop_front();
            if (!tree.AreSame(min.from, min.to)) {
                tree.Union(min.from, min.to);
                result.push_back(min);
            }
        }
        int sum = 0;
        for (auto each : result) {
            sum += each.weight;
        }
        return sum;
    }
    
    

    void Print() {
        for (int i = 1; i < this->nodes.size(); i++) {
            printf("%d:", i);
            for (int j = 1; j < this->nodes[i].Edges().size(); j++) {
                printf(" %d", this->nodes[i].Edges()[j]);
            }
            printf("\n");
        }
    }
private:
    vector<Node> nodes;
};

void solve() {

    int n;
    scanf("%d", &n);
    List list(n);

    for (int i = 1; i <= n; i++){
        for (int j = 1; j <= n; j++) {
            int w;
            scanf("%d", &w);
            list.SetWeight(i, j, w);
        }
    }
    // list.Print();
    printf("%d\n", list.CalcWeight());
}

void test(){
    UnionFindTree t(5);
    t.Union(1, 2);
    t.Union(2, 3);
    t.Union(4, 5);
    t.Print();
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// main
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int main() {
    solve();
    // test();
    return 0;
}

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

int main(){
  int n,a[100][100],b[100]={},c[100]={};
  c[0]=1;
  cin>>n;
  for(int i=0;i<n;i++)
   for(int j=0;j<n;j++)cin>>a[i][j];

  int k=1,min1,ans=0;
  while(1){
   int min=10000;
   for(int i=0;i<k;i++)
    for(int j=0;j<n;j++)
     if(a[b[i]][j]<min && a[b[i]][j]!=-1 &&c[j]!=1){
      min=a[b[i]][j];
      min1=j;
    }
   if(k==n)break;
   b[k]=min1;
   c[min1]=1;
   ans+=min;
   k++;
  }

 cout<<ans<<endl;
 return 0;
}