Go: package main

import (
	"bufio"
	"fmt"
	"log"
	"math"
	"os"
	"strconv"
	"strings"
)

type color int

const (
	white color = iota // 未訪問
	mst                // MSTとして確定
)

// 頂点
type node struct {
	matrix   []int // 隣接行列
	minToMst int   // MSTへの重み最小値
	parent   int   // 最小値の相手
	color    color
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	n := eGetInt(scanner)

	nodes := make([]node, n)
	for i := 0; i < n; i++ {
		nodes[i].matrix = eAtoi((strings.Fields(eGetline(scanner))))
		nodes[i].parent = -1
		nodes[i].minToMst = math.MaxInt16
		nodes[i].color = white
	}

	// 初回はnode[0]がMSTに入るように,minToMstを設定
	nodes[0].minToMst = 0
	for { // 全部チェックするまで

		// どの頂点を新たにMSTに取り込むか
		min := math.MaxInt16
		u := -1
		for i := 0; i < n; i++ {
			if nodes[i].color == mst {
				continue
			}
			if nodes[i].minToMst < min {
				u = i
				min = nodes[i].minToMst
			}
		}

		if u == -1 {
			break
		}
		// MSTに追加する頂点決定
		nodes[u].color = mst

		// MSTに属していない頂点それぞれについて,MSTとの最小の重みを更新
		for i := 0; i < n; i++ {
			if nodes[i].color == mst {
				continue
			}
			if nodes[i].matrix[u] == -1 {
				// 接してない
				continue
			}
			if nodes[i].matrix[u] < nodes[i].minToMst {
				// node[u]との重みが最小
				nodes[i].minToMst = nodes[i].matrix[u]
				nodes[i].parent = u
			}
		}
	}

	sum := 0
	for _, v := range nodes {
		// node[0]はparentが入っていない
		if v.parent != -1 {
			sum += v.matrix[v.parent]
		}
	}
	fmt.Println(sum)
}

func eGetInt(scanner *bufio.Scanner) int {
	t := eAtoi(strings.Fields(eGetline(scanner)))
	return t[0]
}

func eGetline(p *bufio.Scanner) string {
	if !p.Scan() {
		if err := p.Err(); err != nil {
			log.Fatal(err.Error())
		}
		return ""
	}
	return p.Text()
}

func eAtoi(s []string) []int {
	ints := make([]int, len(s))
	var err error
	for i, e := range s {
		ints[i], err = strconv.Atoi(e)
		if err != nil {
			log.Fatal(err.Error())
		}
	}
	return ints
}

C++: #include <bits/stdc++.h>
using namespace std;
static const int INFTY = 2000000000;
static const int NIL = -1;
static const int MAX = 100;
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 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] = INFTY;   //d[i]??????T??????????????§?????????V????±?????????????i?????????????????????
        p[i] = -1;
        color[i] = WHITE;
    }

    d[0] = 0;

    while(1){
        minv = INFTY;
        u = -1;
        for(int i=0;i<n;i++){
            if(d[i] < minv && color[i] != BLACK){    //?¢????????????§????????????????????????INFTY??§?????????????????¢???
                u = i;
                minv = d[i];
            }
        }
        if(u == -1) break;
        color[u] = BLACK;   //???????????????????????????
        for(int v=0;v<n;v++){
            if(color[v] != BLACK && M[u][v] != INFTY){  //2??????????????¶???????????????????
                if(d[v] > M[u][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(){
    scanf("%d",&n);

    for(int i=0;i<n;i++){
        for(int k=0;k<n;k++){
            int e; scanf("%d",&e);
            M[i][k] = (e==-1) ? INFTY : e;
        }
    }

    printf("%d\n",prim());

    return 0;
}

C++: #include <algorithm>
#include <vector>
#include <utility>
#include <iostream>
using namespace std;
typedef pair<int, pair<int,int> > Edge;

int* uni;
void init(int N)
{
    uni = new int[N];
    for (int i = 0; i < N; i++)
    {
        uni[i] = -1;
    }
}

int root(int a)
{
    if (uni[a] < 0) return a;
    else return uni[a] = root(uni[a]);
}

bool connect(int a, int b)
{
    a = root(a);
    b = root(b);
    if (a == b) return false;
    if (uni[a] > uni[b]) swap( a, b);
    uni[a] += uni[b];
    uni[b] = a;
    return true;
}

int main(){
  int n;
  cin >> n;
  vector<Edge> graph(n);
  for (int i=0;i<n;i++){
    for(int j=0;j<n;j++){
      int tmp;
      cin >> tmp;
      if (tmp != -1){
         graph.emplace_back(tmp, make_pair(i,j));
      }
    }
  }
  sort(graph.begin(), graph.end());
  init(n);
  int sum=0;
  for (auto ite=graph.begin();ite != graph.end();ite++){
    if (connect(ite->second.first, ite->second.second))
        sum+=ite->first;
  }
  cout << sum << endl;
}

C++: #include <stdio.h>
#define MAX 100
static const int INFTY = (1<<21);
int n,A[MAX][MAX];

int prim();

int main(){
  int i,j,s;
  scanf("%d",&n);
  for(i=0;i<n;i++){
    for(j=0;j<n;j++){
      scanf("%d",&s);
      A[i][j]=(s==-1)?INFTY : s;
    }
  }
  printf("%d\n",prim());
  return 0;
}

int prim(){
  int e,min,i,v,sum=0;
  int g[MAX],r[MAX],a[MAX];

  for(i=0;i<n;i++){
    g[i]=INFTY;
    r[i]=-1;
    a[i]=0;
  }
  g[0]=0;

  while(1){
    min=INFTY;
    e=-1;
    for(i=0;i<n;i++){
      if(min>g[i]&&a[i]!=2){
	e=i;
	min=g[i];
      }
    }
    if(e==-1)
      break;
    a[e]=2;
    for(v=0;v<n;v++){
      if(a[v]!=2&&A[e][v]!=INFTY){
	if(g[v]>A[e][v]){
	  g[v]=A[e][v];
	  r[v]=e;
	  a[v]=1;
	}
      }
    }
  }
  for(i=0;i<n;i++){
    if(r[i]!=-1)
      sum+=A[i][r[i]];
  }
  return sum;
}

C++: #include <iostream>
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;
static const int MAX = 2000;

int d[110], p[110];
int color[110];

int main() {
  int n;
  std::cin >> n;
  int G[110][110];

  for(int i=0; i<n; ++i) {
    for(int j=0; j<n; ++j) {
      int v;
      std::cin >> v;
      if(v != -1) {
        G[i][j] = v;
      } else {
        G[i][j] = MAX;
      }
    }
  }

  for(int i=0; i<n; ++i) {
    color[i] = WHITE;
    d[i] = MAX;
  }

  d[0] = 0;
  p[0] = 0;

  while(true) {
    int mini_cost = MAX;
    int u;
    // 最小の重みとなる辺を探している
    for(int i=0; i<n; ++i) {
      if(color[i] != BLACK && d[i] < mini_cost) {
        mini_cost = d[i];
        u = i;
      }
    }

    if(mini_cost == MAX) {
      break;
    }

    color[u] = BLACK;

    for(int v=0; v < n; ++v) {
      if(color[v] != BLACK && G[u][v] != MAX) {
        if(G[u][v] < d[v]) {
          d[v] = G[u][v];
          p[v] = u;
          color[v] = GRAY;
        }
      }
    }
  }

  int d_sum = 0;
  for(int i=0; i<n; ++i) {
    d_sum += d[i];
  } 

  std::cout << d_sum << std::endl;
}

C++: #include <iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
const int maxn=111;
const int inf=0x3f3f3f3f;
int e[maxn][maxn];
int n;
int  p[maxn];
int b[maxn];
int vis[maxn];
void prime()
{
    memset(vis,0,sizeof(vis));
    memset(p,-1,sizeof(p));
    for(int i=0; i<n; i++)
        b[i]=inf;
    b[0]=0;
    while(1)
    {
        int ans=inf;
        int v=-1;
        for(int i=0; i<n; i++)
        {
            if(vis[i]==0&&b[i]<ans)
            {
                v=i;
                ans=b[i];
            }
        }
        if(v==-1)
            break;
        vis[v]=1;
        for(int i=0; i<n; i++)
        {
            if(b[i]>e[v][i]&&vis[i]==0)
            {
                b[i]=e[v][i];
                p[i]=v;
                //printf("%d\n",v);
            }
        }
    }
 
    int sum=0;
    for(int i=0;i<n;i++)
    {
        if(p[i]!=-1)
            sum+=e[p[i]][i];
    }
    printf("%d\n",sum);
}
int main()
{
    scanf("%d",&n);
    for(int i=0; i<n; i++)
        for(int j=0; j<n; j++)
        {
            int x;
            scanf("%d",&x);
            if(x==-1)
                e[i][j]=inf;
            else e[i][j]=x;
        }
    prime();
    return 0;
}