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