Go: package main

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

const (
	white int = iota
	gray
	black
)
const INFTY int = 2001

var G [][]int
var color []int
var d []int
var p []int
var N int

func prim() int {
	u := 0
	d[u] = 0
	for {
		minCost := INFTY
		for i:=0; i<N; i++ {
			if color[i] != black && d[i] < minCost {
				minCost = d[i]
				u = i
			}
		}

		if minCost == INFTY {
			break
		}

		color[u] = black

		for v:=0; v<N; v++ {
			if color[v] != black && G[u][v] != -1 {
				if G[u][v] < d[v] {
					d[v] = G[u][v]
					p[v] = u
					color[v] = gray
				}
			}
		}
	}
	// sum
	sum := 0
	for i:=0; i<N; i++ {
		if p[i] != -1 {
			sum += G[i][p[i]]
		}
	}
	return sum
}




func initVariable(N int) {
	G = make([][]int, N)
	for i:=0; i<N; i++ {
		G[i] = make([]int, N)
	}
	color = make([]int, N)
	d = make([]int, N)
	p = make([]int, N)
	for i:=0; i<N; i++ {
		d[i] = INFTY
		p[i] = -1
	}
}

func nextInt(scanner *bufio.Scanner) int {
	scanner.Scan()
	num, _ := strconv.Atoi(scanner.Text())
	return num
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	N = nextInt(scanner)
	initVariable(N)
	for i:=0; i<N; i++ {
		for j:=0; j<N; j++ {
			G[i][j] = nextInt(scanner)
		}
	}
	fmt.Printf("%d\n", prim())
}

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;
}