Go: package main

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

const BIG_COST = 3000

const (
	ST_NOT_YET = iota
	ST_JOINED
)

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	nNodes := scanInt(sc)
	costs := make([][]int, nNodes)

	for r := 0; r < nNodes; r++ {
		costs[r] = make([]int, nNodes)
		for c := 0; c < nNodes; c++ {
			costs[r][c] = scanInt(sc)
		}
	}

	states := make([]int, nNodes)
	minCosts := make([]int, nNodes)

	for i := 1; i < nNodes; i++ {
		minCosts[i] = BIG_COST
	}

	for {
		var u int
		minCost := BIG_COST

		for i := 0; i < nNodes; i++ {
			if states[i] != ST_JOINED && minCosts[i] < minCost {
				minCost = minCosts[i]
				u = i
			}
		}

		// 全てのノードが MST に JOIN できたので終了。
		if minCost == BIG_COST {
			break
		}

		states[u] = ST_JOINED

		for v := 0; v < nNodes; v++ {
			cost := costs[u][v]

			// MST内の u と MST外の v との辺の重みをそれぞれ minCosts[v] として
			// 保存しておく。このうち辺の重みが最小となる v が次のループで MST に JOIN する。
			if states[v] != ST_JOINED && cost != -1 && cost < minCosts[v] {
				minCosts[v] = cost
			}
		}
	}

	sum := 0
	for _, cost := range minCosts {
		sum += cost
	}
	fmt.Println(sum)
}

func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	v, _ := strconv.Atoi(sc.Text())
	return v
}

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;

}