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 <iostream>
using namespace std;
 
#define WHITE 0
#define GRAY 1
#define BLACK 2
#define INFTY 1000000
 
 
int n,M[100][100],color[100],d[100],p[100];
 
//プリム法
int prim(){
  int i,u;

  //初期化
  for( i = 0; i < 100; i++ ){ 
    color[i] = WHITE;
    d[i] = INFTY;
    p[i] = -1;   
  }
  
  d[0] = 0;
  
  
  while(1){
    u = -1;
    int mincost = INFTY;
     
    for( i = 0; i < n; i++ ){
      if( color[i] != BLACK && d[i] < mincost ){
    mincost = d[i];
    u = i;
      }
    }
    
    if( u == -1 ) break;
 
    color[u] = BLACK;
 
    //最小全域木の更新
    for( int v = 0; v < n; v++ ){
      if( color[v] != BLACK && M[u][v] != INFTY ){
    if( M[u][v] < d[v] ){
      d[v] = M[u][v];
      p[v] = u;
      color[v] = GRAY;
    }
      }
    }
  }
  //最小全域木の辺の重みの総和をもとめる
  int sum = 0;
  for( 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];
      if( M[i][j] == -1 ) M[i][j] = INFTY;
    }
  }
 
  cout << prim() << endl;
 
  return 0;
}

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

vector<vector<int>> v;
vector<int> d, color;

const int WHITE = 0;
const int GRAY = 1;
const int BLACK = 2;

int n, ans;

void prim(){
  priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> q;
  q.push(make_pair(0, 0));
  d[0] = 0;

  while(!q.empty()){
    auto next = q.top(); q.pop();
    if(color[next.second] == BLACK) continue;

    color[next.second] = BLACK;
    ans += d[next.second];

    for(int i=0; i<n; ++i){
      if(i != next.second && v[next.second][i] != -1 && color[i] != BLACK && d[i] > v[next.second][i]){
        q.push(make_pair(v[next.second][i], i));
        d[i] = v[next.second][i];
        color[i] = GRAY;
      }
    }
  }
}

int main(){
  cin >> n;

  v.resize(n);
  d.resize(n);
  color.resize(n);

  for(int i=0; i<n; ++i){
    d[i] = numeric_limits<int>::max();
    v[i].resize(n);
    for(int l=0; l<n; ++l) cin >> v[i][l];
  }

  prim();

  cout << ans << endl;
}

C++: #include <iostream>
#include <vector>
#include <numeric>

using namespace std;

const int N = 100;



enum {
    WHITE,
    GRAY,
    BLACK,
};

int main() {
    int A[N][N];

    int n;
    cin >> n;
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            int a;
            cin >> a;
            A[i][j] = a == -1 ? a : numeric_limits<int>::max();
        }
    }

    // ????????????
    vector<int> color(n, WHITE); // ??????????¨??????¶???
    vector<int> d(n, numeric_limits<int>::max());
    vector<int> p(n, -1);

    d[0] = 0;
    while (1) {
        int minv = numeric_limits<int>::min();
        int u = -1;
        for (int 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 (int v = 0; v < n; ++v) {
            if (color[v] != BLACK && A[u][v] != numeric_limits<int>::max()) {
                if (d[v] > A[u][v]) {
                    d[v] = A[u][v];
                    p[v] = u;
                    color[v] = GRAY;
                }
            }
        }
    }
    int sum;
    for (int i =0; i < n; ++i) {
        if (p[i] != -1)
            sum += A[i][p[i]];
    }
    cout << sum << endl;

    return 0;
}

C++: #include <iostream>
#include <queue>
using namespace std;
#define MAX 105

int n, choice[MAX];

struct MyPower {
	int v, p1, p2;
	void set(int value, int point1, int point2) {
		v = value;
		p1 = point1;
		p2 = point2;
	}
};

bool operator<(MyPower a, MyPower b) {
	return (a.v > b.v);
}

int main(void) {
	int i, j, x;
	priority_queue<MyPower> PQ;
	MyPower MP;
	for (i = 0; i < MAX; i++) {
		choice[i] = 0;
	}
	cin >> n;
	for (i = 0; i < n; i++) {
		for (j = 0; j < n; j++) {
			cin >> x;
			if (x == -1) continue;
			MP.set(x, i, j);
			PQ.push(MP);
		}
	}
	/*
	int siz = PQ.size();
	for (i = 0; i<siz; i++) {
		MP = PQ.top();
		PQ.pop();
		cout << MP.v<< " "<<MP.p1<<" "<<MP.p2<<endl;
	}
	*/
	
	int c = 1, total = 0;
	while (c < n) {
		MP = PQ.top();
		PQ.pop();
		if (choice[MP.p1] == 0 || choice[MP.p2] == 0) {
			total += MP.v;
			choice[MP.p1] = 1;
			choice[MP.p2] = 1;
			c++;
		}
	}
	cout << total << endl;
	
}

C++: #include <cstdio>

const int MAX_N = 100;
const int INFINITY = 2000 + 1;

enum
{
  WHITE,
  GRAY,
  BLACK
};

int color[MAX_N];
int M[MAX_N][MAX_N];
int d[MAX_N];
int p[MAX_N];
int n;

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

  d[0] = 0;

  while (true)
  {
    int mincost = INFINITY;
    int u = -1;

    for (int i = 0; i < n; ++i)
    {
      if (color[i] != BLACK && mincost > d[i])
      {
        mincost = d[i];
        u = i;
      }
    }

    if (u == -1)
      break;

    color[u] = BLACK;

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

int main()
{
  scanf("%d", &n);

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

  prim();

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

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

  return 0;
}