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 <bits/stdc++.h>
#define INF 2001
using namespace std;
int M[100][100],d[100],p[100];
bool vist[100]={0};
void prim(int n){
	for(int i=0;i<n;i++) d[i] = INF;
	d[0] = 0;
	p[0] = -1;
	int mincost, u;
	while(1){
		mincost = INF;
		for(int i=0;i<n;i++)
			if(!vist[i] && d[i]<mincost)
				mincost = d[i], u = i;
		if(mincost == INF) break;
		vist[u] = 1;
		for(int i=0; i<n; i++)
			if(!vist[i] && M[u][i]!=-1 && M[u][i]<d[i])
				d[i] = M[u][i],
				p[i] = u;
	}
}
int main()
{	int n; cin>>n;
	for(int i = 0;i < n;i++)
		for(int j = 0;j < n;j++)
			scanf("%d",&M[i][j]);
	prim(n);
	long long ans = 0;
	for(int i=1; i<n; i++)
		ans += M[i][p[i]];
	cout << ans << endl;
	return 0;
}

C++: #include <iostream>
#include<vector>
#include<algorithm>
#include<numeric>
using namespace std;
int inf=(1<<12);


int main(void){
    int n,u;
int total=0;
int m[100][100];
int d[100];

    cin>>n;
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            int k;
            cin>>k;
            if(k==-1){
                m[i][j]=inf;
            }
            else{
                m[i][j]=k;
            }
            d[j]=inf;
        }
    }
    
    d[0]=0;
    while(1){
        u=-1;
        int min=inf;
        for(int i=0;i<n;i++){
            if(min>d[i]&&d[i]!=-1){
                min=d[i];
                u=i;
            }
        }
        if(u==-1)break;
        total+=min;
        d[u]=-1;
        for(int i=0;i<n;i++){
            if(d[i]!=-1&&d[i]>m[u][i]){
                d[i]=m[u][i];
            }
        }
    }
    cout<<total<<endl;
    return 0;
}

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

#define MAX 100
#define NOT -1
#define FND -2
#define CMP -3

int n, route;
int G[MAX+1][MAX+1];
int d[MAX+1][2];
int p[MAX+1];

void make_graph(void){
  scanf("%d", &n);

  for(int i=0; i<n; i++){
    for(int j=0; j<n; j++){
      scanf("%d", &G[i][j]);
      if(G[i][j] == -1) G[i][j]=INT_MAX;
    }
  }
}

void prim(void){
  int u, m, mst=0;

  for(int i=0; i<n; i++){
    d[i][0]=INT_MAX;
    d[i][1]=0;
    p[i]=-1;
  }
  
  d[0][0]=0; d[0][1]=FND;
  
  while(true){
    u=-1;
    m=INT_MAX;
    
    for(int i=0; i<n; i++){
      if(d[i][0] < m && d[i][1] != CMP){
	u=i; m=d[i][0];
      }
    }

    if(u == -1) break;
    d[u][1]=CMP;

    for(int i=0; i<n; i++){
      if(d[i][1] != CMP && G[u][i] != INT_MAX){
	if(d[i][0] > G[u][i]){
	  d[i][0]=G[u][i];
	  d[i][1]=FND;
	  p[i]=u;
	}
      }
    }

  }

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

  printf("%d\n", mst);
}

int main(void){
  make_graph();
  prim();
  
  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>
#include <algorithm>
#include <tuple>

using namespace std;

tuple<int,int,int> a[10010];
int t[101];
int n,w;
int k=0;
int x,y;

int root(int x)
{
     if(t[x]==x)
     {
          return x;
     }
     else
     {
          t[x]=root(t[x]);
          return t[x];
     }
}

void unite(int x,int y)
{
     t[root(x)]=root(y);
}

bool same(int x,int y)
{
     return root(x)==root(y);
}

int main()
{
     cin >> n;
     for(int i=0;i<n;i++)
     {
          t[i]=i;
          for(int j=0;j<n;j++)
          {
               cin >> w;
               if(w==-1)
               {
                    continue;
               }
               a[k]=make_tuple(w,i,j);
               k+=1;
          }
     }
     sort(a,a+k);
     w=0;
     for(int i=0;i<k;i++)
     {
          x=get<1>(a[i]);
          y=get<2>(a[i]);
          if(same(x,y))
          {
               continue;
          }
          unite(x,y);
          w+=get<0>(a[i]);
     }
     cout << w << endl;
     return 0;
}

C++: #include <iostream>
#include <algorithm>

using namespace std;

typedef struct b {
	int dis;
	int node1;
	int node2;
}b;

bool cmp(b n1, b n2) {
	return n1.dis < n2.dis;
}

int main() {
	int n;
	cin >> n; 
	int graph[101][101];
	b bs[10000];
	int bCount = 0;
	int ifs[101];
	for (int i = 0; i < n; ++i) {
		ifs[i] = 0;
		for (int j = 0; j < n; ++j) {
			cin >> graph[i][j];
			if (graph[i][j] == -1) {
				continue;
			}
			else if(i>j){
				bs[bCount].dis= graph[i][j];
				bs[bCount].node1 = i;
				bs[bCount++].node2 = j;
			}
		}
	}

	sort(bs, bs + bCount, cmp);

	long totalDis = 0;
	int k = 1;
	for (int i = 0; i < bCount; ++i) {
		b b1 = bs[i];
		if (ifs[b1.node1]>0 && ifs[b1.node2]==ifs[b1.node1]) {
			continue;
		}

		totalDis += b1.dis;
		if (ifs[b1.node1] == 0 && ifs[b1.node2] == 0) {
			ifs[b1.node1] = k;
			ifs[b1.node2] = k++;
		}
		else if (ifs[b1.node1] == 0) {
			ifs[b1.node1] = ifs[b1.node2];
		}
		else if (ifs[b1.node2] == 0) {
			ifs[b1.node2] = ifs[b1.node1];
		}
		else {
			for (int j = 0; j < n; ++j) {
				if (ifs[j] == ifs[b1.node2] && j != b1.node2) {
					ifs[j] = ifs[b1.node1];
				}
			}
			ifs[b1.node2] = ifs[b1.node1];
		}
	}

	cout << totalDis << endl;
	return 0;

}