Go: package main

import (
	"fmt"
	"sort"
)

type Edge struct {
	I int
	J int
	A int
}

func main() {
	var n int
	fmt.Scan(&n)

	g := make([][]int, n)
	edges := make([]Edge, 0, n*(n-1)/2)
	parents := make([]int, n)
	for i := 0; i < n; i++ {
		g[i] = make([]int, n)
		for j := 0; j < n; j++ {
			fmt.Scan(&g[i][j])
			if g[i][j] > 0 {
				edges = append(edges, Edge{i, j, g[i][j]})
			}
		}
		parents[i] = i
	}
	sort.SliceStable(edges, func(i, j int) bool { return edges[i].A > edges[j].A })

	sum := 0
	for len(edges) > 0 {
		edge := edges[len(edges)-1]
		edges = edges[:len(edges)-1]

		if same(edge.I, edge.J, &parents) {
			continue
		}
		union(edge.I, edge.J, &parents)
		sum += edge.A
	}
	fmt.Println(sum)
}



func root(id int, parents *[]int) int {
	if id == (*parents)[id] {
		return id
	}
	return root((*parents)[id], parents)
}

func union(i, j int, parents *[]int) {
	(*parents)[root(j, parents)] = (*parents)[root(i, parents)]
}

func same(i, j int, parents *[]int) bool {
	return root(i, parents) == root(j, parents)
}

C++: #include<iostream>
#include<stack>
#include<queue>
using namespace std;
#define max 100
#define infty 1<<21

int n;
int aa[max][max];
int color[max];
int d[max];
int p[max];

int prim() {
	for (int i = 0; i < n; i++) {
		color[i] = 0;
		d[i] = infty;
		p[i] = -1;
	}
	d[0] = 0;
	int ans = 0;

	while (true) {
		int mincost = infty;
		int u = -1;
		for (int i = 0; i < n; i++) {
			if (color[i] != 1 && d[i] < mincost) {
				u = i;
				mincost = d[i];
			}
		}
		if (u == -1) break;
		color[u] = 1;
		for (int i = 0; i < n; i++) {
			if (color[i] != 1 && aa[u][i] != -1) {
				if (aa[u][i] < d[i]) {
					d[i] = aa[u][i];
					p[i] = u;
				}
			}
		}
	}
	for (int i = 0; i < n; i++) {
		if (p[i] != -1) ans += aa[i][p[i]];
	}

	return ans;
}

int main() {
	cin >> n;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			int num1;
			cin >> num1;
			aa[i][j] = num1;
		}
	}

	int kotae = prim();

	cout << kotae << endl;

	return 0;
}

C++: #include<bits/stdc++.h>

using namespace std;


class UnionFind{
    int N;
    vector<int> par;
    vector<int> rank;
    public:
    UnionFind(int N):N(N),par(N),rank(N,0){iota(par.begin(),par.end(),0);}
    int find(int x);
    void unite(int x,int y);
    bool same(int x,int y){return find(x)==find(y);}
};

int UnionFind::find(int x){
    if(par[x]==x) return x;
    return par[x]=find(par[x]);
}
void UnionFind::unite(int x,int y){
    x=find(x),y=find(y);
    if(x==y) return;
    if(rank[x]>=rank[y]){
        if(rank[x]==rank[y]) rank[x]++;
        par[y]=x;
    }else{
        par[x]=y;
    }
}

using Weight=long long;
struct Edge{
    int from,to;
    Weight weight;
};
bool operator<(const Edge& lhs,const Edge& rhs){
    return lhs.weight<rhs.weight;
}

using Edges=vector<Edge>;
using Graph=vector<Edges>;


Edges MinimumSpanningTree(int n,Edges edges){
    sort(edges.begin(),edges.end());
    Edges res;
    UnionFind u(n);
    for(int i=0;i<edges.size();i++){
        if(!u.same(edges[i].from,edges[i].to)){
            res.push_back(edges[i]);
            u.unite(edges[i].from,edges[i].to);
        }
    }
    return res;
}

Edges MinimumSpanningTree(const Graph& g){
    Edges edges;
    for(int i=0;i<g.size();i++){
        copy(g[i].begin(),g[i].end(),back_inserter(edges));
    }
    auto res=MinimumSpanningTree(g.size(),edges);
    return res;
}

int main(){
    int n;
    cin>>n;
    Graph g(n);
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            Weight c;
            cin>>c;
            if(c==-1) continue;
            g[i].push_back({i,j,c});
        }
    }
    auto mst=MinimumSpanningTree(g);
    cout<<accumulate(mst.begin(),mst.end(),Weight(0),[](const Weight& lhs,const Edge& rhs){return lhs+rhs.weight;})<<endl;
    return 0;
}

C++: #include<iostream>
using namespace std;
#define REP(i,n) for(unsigned int i=0;i<(n);i++)
#define MAX 101
#define INF (1<<21)
#define NONE -1
#define WHITE 0
#define GRAY 1
#define BLACK 2


int node[MAX][MAX];
int N;

int prim()
{
	int cost[MAX];
	int color[MAX];
	int parent[MAX];
	REP(i, N)
	{
		cost[i] = INF;
		color[i] = WHITE;
		parent[i] = NONE;
	}

	cost[0] = 0;
	
	while (true)
	{
		int min_index = NONE;
		int min_value = INF;
		REP(i, N)
		{
			if (color[i] !=BLACK)
			{
				if (cost[i] < min_value)
				{
					min_index = i;
					min_value = cost[i];
				}
			}
		}
		if (min_index == NONE)break;
		color[min_index] = BLACK;
		REP(i, N)
		{
			if (color[i] != BLACK)
			{
				if (node[min_index][i] != INF)
				{
					if (cost[i] > node[min_index][i])
					{
						cost[i] = node[min_index][i];
						parent[i] = min_index;
						color[i] = GRAY;
					}
				}
			}
		}
	}
	int ans = 0;
	REP(i, N)
	{
		if (parent[i]!=NONE)
		{
			ans += node[i][parent[i]];
		}
	}
	return ans;
}


int main()
{
	REP(i, MAX)
	{
		REP(j, MAX)
		{
			node[i][j] = INF;
		}
	}
	cin >> N;
	REP(i, N)
	{
		REP(j, N)
		{
			int tmp;
			cin >> tmp;
			if (tmp == -1)
			{
				node[i][j] = INF;
			}
			else node[i][j] = tmp;
		}
	}
	cout << prim() << endl;
	return 0;
}

C++: #include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;

int x[10000][10000];
int MIN[10000];
int MINX = 0;
int MINZ = 10000000;

int main() {
	memset(MIN, 127, sizeof(MIN));
	int n, a;
	cin >> n;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			cin >> a;
			x[i][j] = a;
		}
	}
	for(int h=0;h<n;h++){
		memset(MIN, 127, sizeof(MIN));
		MINX=0;
		MIN[h] = 0;
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				for (int k = 0; k < n; k++) {
					if (x[j][k] != -1) {
						MIN[k] = min(MIN[k], x[j][k]);
					}
				}
			}
		}
		for (int i = 0; i < n; i++) {
			MINX += MIN[i];
		}
		MINZ=min(MINZ,MINX);
	}
	cout << MINZ << endl;
	return 0;
}

C++: #include "taichitary.h"
#define V_MAX 100
#define INFTY 5000
#define WHITE 0
#define GRAY 1
#define BLACK 2

int M[V_MAX][V_MAX];
int d[V_MAX], p[V_MAX];
int n;
int color[V_MAX];

int Prim(int u){
    p[u] = -1; //p??????????????????????¨????

    for(int i=0; i<n; i++){
        d[i] = INFTY;
        p[i] = -1;
        color[i] = WHITE;
    }

    d[u] = 0; //d?????????????????????

    while(1){
        int mincost = INFTY;
        int from = -1;

        //??????????????§???????°?????????¨??????????¨???¶????????????
        for(int i=0; i<n; i++){
            if(color[i] != BLACK && d[i] < mincost){
                mincost = d[i];
                from = i;
            }
        }

        if(from == -1) break;

        color[from] = BLACK;

        for(int v=0; v<n; v++){
            if(color[v] != BLACK && M[from][v] != INFTY){
                if(M[from][v] < d[v]){
                    d[v] = M[from][v];
                    p[v] = from; //???????¨??????????
                    color[v] = GRAY;
                }
            }
        }
    }

    int sum = 0;

    for(int j=0; j<n; j++){
       if(p[j] != -1) sum += M[j][p[j]];
    }

    return sum;
}

int main(){

    cin >> n;

    for(int k=0; k<n; k++) color[k] = WHITE;

    for(int i=0; i<n; i++){
        for(int j=0; j<n; j++){
            int tmp;
            cin >> tmp;
            if(tmp != -1) M[i][j] = tmp;
            else M[i][j] = INFTY;
        }
    }

    int sum = Prim(0);

    cout << sum << endl;
}