Go: package main

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

const R = 100000000

var sc = bufio.NewScanner(os.Stdin)

var n int
var A [101][101]int

func nextInt() int {
	sc.Scan()
	i, _ := strconv.Atoi(sc.Text())
	return i
}
func prim() int {
	var u, Minimum, sum int
	var d, p, color [101]int

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

	for {
		Minimum = R
		u = -1
		for i := 0; i < n; i++ {
			if Minimum > d[i] && color[i] != 2 {
				u = i
				Minimum = d[i]
			}
		}
		if u == -1 {
			break
		}
		color[u] = 2
		for j := 0; j < n; j++ {
			if color[j] != 2 && A[u][j] != R {
				if d[j] > A[u][j] {
					d[j] = A[u][j]
					p[j] = u
					color[j] = 1
				}
			}
		}
	}

	for i := 0; i < n; i++ {
		if p[i] != -1 {
			sum += A[i][p[i]]
		}
	}
	return sum
}
func main() {
	sc.Split(bufio.ScanWords)
	n = nextInt()

	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			e := nextInt()
			if e == -1 {
				A[i][j] = R
			} else {
				A[i][j] = e
			}
		}
	}
	fmt.Println(prim())
}

C++: #include<iostream>
#include<vector>
#include<utility>
using namespace std;

bool self(vector<vector<int> >& G, int s, int t, vector<pair<int, int> >& stock_idx, vector<int>& searched){
    if (searched[s] == 1) return false;
    searched[s] = 1;
    if (G[s][t] != -1 && stock_idx.size() > 1) {
        stock_idx.push_back(make_pair(s, t));
        return true;
    }
    for (int i = 0; i < G[s].size(); i++) {
        if (G[s][i] != -1) {
            stock_idx.push_back(make_pair(s, i));
            if (self(G, i, t, stock_idx, searched)) return true;
            stock_idx.pop_back();
        }
    }
    return false;
}

bool DFS(vector<vector<int> >& G, int s, int t, vector<pair<int, int> >& stock_idx){
    vector<int> searched(G.size(), 0);
    return self(G, s, t, stock_idx, searched);
}

void cut(vector<vector<int> >& G, vector<pair<int, int> >& stock_idx){
    pair<int, int> max_idx = stock_idx[0];
    for (int i = 0; i < stock_idx.size(); i++) {
        if (G[stock_idx[i].first][stock_idx[i].second] > G[max_idx.first][max_idx.second]) {
            max_idx = stock_idx[i];
        }
    }
    G[max_idx.first ][max_idx.second] = -1;
    G[max_idx.second][max_idx.first ] = -1;
    stock_idx.clear();
}

int main(){
    int n;
    cin >> n;
    vector<vector<int> > G(n, vector<int>(n));
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            cin >> G[i][j];
        }
    }
    for (int i = 0; i < n; i++) {
        vector<pair<int, int> > stock_idx;
        while (DFS(G, i, i, stock_idx)) cut(G, stock_idx);
    }
    int sum = 0;
    for (int i = 0; i < n; i++) {
        for (int j = i + 1; j < n; j++) {
            if (G[i][j] != -1) sum += G[i][j];
        }
    }
    cout << sum << endl;
    // for (int i = 0; i < n; i++) {
    //     for (int j = 0; j < n; j++) {
    //         cout << " " << G[i][j];
    //     }
    //     cout << endl;
    // }

    return 0;
}

C++: #include <bits/stdc++.h>
 
using namespace std;
namespace kruskal_n {
struct UnionFind {
	int sz;
	vector<int> par;
	UnionFind(int a) : sz(a), par(sz, -1) {}
	int root(int x) { return par[x] < 0 ? x : par[x] = root(par[x]);}
	bool same(int x, int y) { return root(x) == root(y);}
	bool unite(int x, int y) {
		x = root(x);
		y = root(y);
		if (x == y) return false;
		if (size(x) < size(y)) swap(x, y);
		par[x] += par[y];
		par[y] = x;
		return true;
	}
	int size(int x) { return -par[root(x)];}
	int size() { return sz;}
};
template<class W>
struct Edge {
	using type = Edge<W>;
	int from;
	int to;
	W w;
	template<class... Args> Edge(int f, int t, Args... args) : from(f), to(t), w(args...) {}
	inline bool operator<(const Edge& rhs) const { return w < rhs.w; }
	inline bool operator>(const type& rhs) const { return rhs < *this; }
	inline bool operator>=(const type& rhs) const { return !(*this < rhs); }
	inline bool operator<=(const type& rhs) const { return !(rhs < *this); }
	inline bool operator==(const type& rhs) const { return !(*this < rhs) && !(rhs < *this); }
	inline bool operator!=(const type& rhs) const { return (*this < rhs) || (rhs < *this); }
};
template<class W>
struct Edges : private vector<Edge<W>> {
	using type = vector<Edge<W>>;
	Edges() : type() {}
	Edges(int n) : type() {assert(0 && "Constructor must be empty");}
	template<class... Args> void add_edge(int u, int v, Args... args) {
		(*this).emplace_back(u, v, args...);
	}
	using type::begin; using type::end; using type::rbegin; using type::rend;
	using type::cbegin; using type::cend; using type::crbegin; using type::crend;
	using type::size; using type::operator[]; using type::at; using type::back; using type::front;
	using type::reserve; using type::resize; using type::assign; using type::shrink_to_fit;
	using type::clear; using type::erase; using type::insert; using type::swap; 
	using type::push_back; using type::pop_back; using type::emplace_back; using type::empty;
};
template<class T>
T Kruskal(Edges<T>& e) {
	sort(e.begin(), e.end());
	UnionFind u(e.size());
	T res = 0;
	for (auto &x : e) {
		if (u.unite(x.from, x.to)) res += x.w;
	}
	return res;
}
}
using kruskal_n::Kruskal;
template<class T> using graph = kruskal_n::Edges<T>;

int main() {
    int n; cin >> n;
    graph<long long> g;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            int c; cin >> c;
            if (c >= 0) g.add_edge(i, j, c);
        }
    }
    cout << Kruskal(g) << endl;
}

C++: #include <climits>
#include <iostream>
#include <vector>

using namespace std;

int prim(vector<vector<int>>& graph) {
    int n = graph.size() - 1;
    vector<bool> isInTree(n + 1);
    vector<int> weight(n + 1);
    for (int i = 1; i <= n; i++)
        weight[i] = INT_MAX;
    int v = 1;
    weight[v] = 0;
    while (true) {
        isInTree[v] = true;
        int minCost = INT_MAX;
        for (int i = 1; i <= n; i++) {
            if (!isInTree[i])
                continue;
            for (int j = 1; j <= n; j++) {
                int cost = graph[i][j];
                if (cost >= 0 && !isInTree[j] && cost <= minCost) {
                    v = j;
                    minCost = cost;
                }
            }
        }
        if (minCost == INT_MAX)
            break;
        weight[v] = minCost;
    }

    int result = 0;
    for (int i = 1; i <= n; i++)
        result += weight[i];
    return result;
}

int main() {
    int n;
    cin >> n;
    vector<vector<int>> graph(n + 1, vector<int>(n + 1));
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++)
            cin >> graph[i][j];

    int totalCost = prim(graph);
    cout << totalCost << endl;

    return 0;
}

C++: a