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 <climits>
using namespace std;

int n;
int g[101][101];

int d[101];
char c[101];
int p[101];

void prim() {
    for (int i = 0; i < n; ++i) {
        d[i] = INT_MAX;
        c[i] = 'w';
    }
    c[0] = 'b';
    d[0] = 0;
    while (true) {
        int u, v;
        int mine = INT_MAX;
        for (int j = 0; j < n; ++j) {
            if (c[j] == 'b') {
                u = j;
                for (int k = 0; k < n; ++k) {
                    if(g[u][k] > -1 && g[u][k] <= d[k] && c[k] != 'b' && g[u][k] <= mine) {
                        d[k] = g[u][k];
                        mine = d[k];
                        v = k;
                    }
                }
            }
        }
        p[v] = u;
        c[v] = 'b';
        if(mine == INT_MAX) break;
    }
}

int main() {
    cin >> n;
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            int w;
            cin >> w;
            g[i][j] = w;
        }
    }
    prim();
    int sum = 0;
    for (int k = 0; k < n; ++k) {
        sum += d[k];
    }
    cout << sum << endl;


    return 0;
}

C++: #include"iostream"
#include"vector"
using namespace std;

int main()
{
  int n;
  vector<int>list;
  int w=0;
  cin>>n;
  vector<vector<int> > A(n,vector<int>(n));
  for(int i=0;i<n;i++)
    {
      for(int j=0;j<n;j++)
        {
          cin>>A[i][j];
        }
    }
  list.push_back(0);
  int mini=2001;
  while(list.size()<n)
    {
      mini=2001;
      int k,l;
      for(int i=0;i<list.size();i++)
        {
          for(int j=0;j<n;j++)
            {
              if(A[list[i]][j]>=0)
                {
                  if(A[list[i]][j]<mini)
                    {
                      int f=1;
                      for(int s=0;s<list.size();s++)
                        {
                          if(j==list[s])
                            {
                              f=0;
                              break;
                            }
                        }
                      if(f)
                        {
                          mini=A[list[i]][j];
                          k=j;
                          l=list[i];
                        }
                    }
                }
            }
        }
      list.push_back(k);
      w+=mini;
    }
  cout<<w<<endl;
  return 0;
}

C++: #include<stdio.h>
using namespace std;
const int sss=1<<21;
int n;
int a[1000][1000];
int d[1000],p[10000],color[1000];
int prim(){
	int u,minv;
	for(int i=0;i<n;i++){
		d[i]=sss;
		p[i]=-1;
	}
	d[0]=0;
	while(1){
		minv=sss;
		u=-1;
		for(int i=0;i<n;i++){
			if(minv>d[i]&&color[i]!=1){
				u=i;
				minv=d[i];
			}
		}
		if(u==-1) break;
		color[u]=1;
		for(int v=0;v<n;v++){
			if(color[v]!=1&&a[u][v]!=sss){
				if(d[v]>a[u][v]){
					d[v]=a[u][v];
					p[v]=u;
				}
			}
		}
	}
	int sum=0;
	for(int i=0;i<n;i++){
		if(p[i]!=-1) {
			sum+=a[i][p[i]];
		}
	}
}
int main(){
	scanf("%d",&n);
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			int e;scanf("%d",&e);
			a[i][j]=e==-1?sss:e;
		}
	}
	printf("%d\n",prim());
	return 0;
}

C++: #include<iostream>
#include<algorithm>
#define INF 99999
using namespace std;

int G[110][110] = {};

int prim(int n)
{
    int mini[110];
    int used[110];

    //init
    for(int i = 0;i < n;i++){
        mini[i] = INF;
        used[i] = 0;
    }
    mini[0] = 0;
    int ans = 0;

    while(true)
    {
        int v = -1;
        for(int i = 0;i < n;i++){
            if(!used[i] && (v == -1 || mini[i] < mini[v])) v = i;
        }
        if(v == -1) break;
        //ans = -1;

        used[v] = 1;
        ans += mini[v];
        //cout << ans << endl;

        for(int i = 0;i < n;i++){
            mini[i] = min(mini[i], G[v][i]);
        }
    }
    return ans;
}

int main()
{
    int n;
    cin >> n;

    for(int i = 0;i < n;i++){
        for(int j = 0;j < n;j++){
            int cost;
            cin >> cost;
            if(cost == -1) G[i][j] = INF;
            else G[i][j] = cost;
        }
    }

    cout << prim(n) << endl;
    return 0;
}

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


int P[10010]; // 0 から 10000 までの頂点を取り扱い可能
void init(int N) { // 初期化 はじめは全ての頂点はバラバラ
  for (int i=0; i<N; i++) P[i] = i;
}
int root (int a) { //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;
  cin >> n;
  tuple<int,int,int> cost[10010]; //Array for cost
  int count = 0;
  for (int i = 0; i < n; i ++){
    for (int j = 0; j < n; j++){
      int t;
      cin >> t;
      if (t >= 0){
      get<1>(cost[count]) = i;
      get<2>(cost[count]) = j;
      get<0>(cost[count]) = t;
      count++;
    }
    }
  }
  sort(cost,cost + n*n);//sortする

  init(n + 10);

  int ans = 0;
  int anscount = 0;//このcountがnになった時点で終了する
  int k = 0;
  while (anscount != n-1){
    if (is_same_set(get<1>(cost[k]), get<2>(cost[k]))) {
      k ++;
    }
    else{
      unite(get<1>(cost[k]), get<2>(cost[k]));
      ans += get<0>(cost[k]);
      k++;
      anscount++;
    }
  }


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

C++: #include<bits/stdc++.h>
using namespace std;
  static const int MAX=100;
  static const int INF=(1<<21);
  static const int WHI=0;
  static const int GRA=1;
  static const int BLA=2;
  int n,M[MAX][MAX];
  int prim(){
    int u,minv;
    int d[MAX],p[MAX],color[MAX];
    for(int i=0;i<n;i++){
      d[i]=INF;
      p[i]=-1;
      color[i]=WHI;
    }
    
    d[0]=0;
    while(1){
      minv=INF;
      u=-1;
      for(int i=0;i<n;i++){
	if(minv>d[i]&&color[i]!=BLA){
	  u=i;
	  minv=d[i];
	}
      }

      if(u==-1){
	break;
      }
      color[u]=BLA;

      for(int v=0;v<n;v++){
	if(color[v]!=BLA&&M[u][v]!=INF){
	  if(M[u][v]<d[v]){
	    d[v]=M[u][v];
	    p[v]=u;
	    color[v]=GRA;
	  }

	}
      }
    }

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

  int main(){
    cin>>n;
    for(int i=0;i<n;i++){
      for(int j=0;j<n;j++){
	int e;
	cin>>e;
	if(e==-1){
	  M[i][j]=INF;
	}else{
	  M[i][j]=e;
	}
      }
    }
    
    cout<<prim()<<endl;
    return 0;
  }