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<cstdio>
#include<functional>
#include<algorithm>
#define F first
#define SF second.first
#define SS second.second
using namespace std;
typedef pair<int,int> P;
typedef pair<int,P> P2;
int main(void)
{
	int i,j,n,a,par[100],leng[100],sum,p1,cnt,s,t;
	P2 pp[100*99/2];
	scanf("%d",&n);
	p1=0;
	for(i=0;i<n;i++)	{
		for(j=0;j<n;j++)	{
			scanf("%d",&a);
			if(i<j && a!=-1)	{
				pp[p1].F=a;
				pp[p1].SF=i;
				pp[p1].SS=j;
				p1++;
			}
		}
	}
	sort(pp,pp+p1);
	for(i=0;i<n;i++)	{
		leng[i]=1;	par[i]=i;
	}
	cnt=1;	sum=0;
	for(i=0;i<p1;i++)	{
		s=pp[i].SF;	t=pp[i].SS;
		while(par[s]!=s)	s=par[s];
		while(par[t]!=t)	t=par[t];
		if(s!=t)	{
			if(leng[s]>leng[t])	par[t]=s;
			else if(leng[s]<leng[t])	par[s]=t;
			else {
				par[s]=t;	leng[t]++;

			}
			sum+=pp[i].F;
			cnt++;
			if(cnt==n)	break;
		}
	}
	printf("%d\n",sum);
	return 0;
}

C++: #include<iostream>
using namespace std;
static const int whi=0;
static const int gra=1;
static const int bla=2;
static const int MAXX=(1<<21);
int color[110];
int a[110][110];
int minni[110];
int parent[110];
int n;

int prim(){
  int b=0;
  int minmin=0;
  int result=0;
  for(int i=0;i<n;i++){
    color[i]=whi;
    minni[i]=MAXX;
    parent[i]=-1;
  }
  minni[0]=0;
  while(1){
    minmin=MAXX;
    b=-1;
    for(int i=0;i<n;i++){
      if(color[i]!=bla&&minni[i]<minmin){
	b=i;
	minmin=minni[i];
	
      }
    }
    if(b==-1){
      break;
    }
    color[b]=bla;
    for(int j=0;j<n;j++){
      if(color[j]!=bla&&a[b][j]!=MAXX){
	if(a[b][j]<minni[j]){
	  minni[j]=a[b][j];
	  parent[j]=b;
	  color[j]=gra;
	}
      }
    }
  }
  for(int i=0;i<n;i++){
    if(parent[i]!=-1) result+=a[i][parent[i]];
  }
  return result;
}

int main(){
  cin>>n;

  int s;
  for(int i=0;i<n;i++){
    for(int j=0;j<n;j++){
      cin>>s;
      a[i][j]=(s==-1) ? MAXX : s;
    }
  }
  cout<<prim()<<endl;
}

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

static const int MAX=100;
static const int INFTY=(1<<21);
static const int WHITE=0;
static const int GRAY=1;
static const int BLACK=2;

int n,m[MAX][MAX];

int prim(){
  int u,min,d[MAX],p[MAX],color[MAX];

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

  d[0]=0;

  while(1){
    min=INFTY;
    u=-1;
    for(int i=0;i<n;i++){
      if(min>d[i] && color[i]!=BLACK){
        u=i;
        min=d[i];
      }
    }
    if(u==-1) break;
    color[u]=BLACK;
    for(int j=0;j<n;j++){
      if(color[j]!=BLACK && m[u][j]!=INFTY){
        if(d[j]>m[u][j]){
          d[j]=m[u][j];
          p[j]=u;
          color[j]=GRAY;
        }
      }
    }
  }
  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;
      m[i][j]=(e==-1) ? INFTY : e;
    }
  }
  cout << prim() << endl;

  return 0;
}

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;
    for (int k = 0; k < n; ++k) {
        sum += d[k];
    }
    cout << sum << endl;


    return 0;
}

C++: //最少全域木
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int G[103][103];
int V;
int NG[103][103];//新しくできるほう
int used[103];
int main(){
	int ans=0;
	cin>>V;
	for(int i=0;i<V;i++){
		for(int j=0;j<V;j++){
		cin>>G[i][j];
		}
	}
	for(int i=0;i<103;i++)for(int j=0;j<103;j++)NG[i][j]=-1;
	used[0]=1;
	for(int i=0;i<V;i++){
		int u=-1,a,b;
		for(int j=0;j<V;j++){
		if(used[j]==0)u=i;
		}
		if(u==-1)break;
		int now=99999999;
		for(int j=0;j<V;j++){
			for(int k=0;k<V;k++){
			if(now>G[j][k]&&G[j][k]!=-1&&used[j]==1&&used[k]==0){
			now=G[j][k];
			a=j,b=k;
			}
			}
		}
		ans+=G[a][b];
		used[b]=1;
		NG[a][b]=G[a][b];
	}
	cout<<ans<<endl;
	return 0;
}

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

const int white = 0;
const int gray = 1;
const int black = 2;
const int Max_n = 100;
const int Max_a = 2000;

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

int prim(){
    for(int i=n; i<n; i++){
        color[i] = white;
        d[i] = Max_a + 1;
    }

    d[0] = 0;
    p[0] = -1;

    while(1){
        int mincost = Max_a + 1;
        int u;
        for(int i=0; i<n; i++){
            if(color[i] != black && d[i] < mincost){
                mincost = d[i];
                u = i;
            }
        }
        if(mincost == Max_a + 1){
            break;
        }

        color[u] = black;

        for(int v=0; v<n; v++){
            if(color[v] != black && M[u][v] != -1){
                if(M[u][v] < d[v]){
                    d[v] = M[u][v];
                    p[v] = u;
                    color[v] = gray;
                }
            }
        }
    }

    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++){
            cin >> M[i][j];
        }
    }

    cout << prim() << endl;


    return 0;
}