Go: package main

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

const (
	WHITE int = iota
	GRAY
	BLACK
)

type graph struct {
	G [][]int
	C []int
	D []int
	P []int
}

var INFTY = 1 << 32

func constructGraph() *graph {
	sc := bufio.NewScanner(os.Stdin)
	sc.Scan()
	n, _ := strconv.Atoi(sc.Text())

	g := make([][]int, n)
	c := make([]int, n)
	d := make([]int, n)
	p := make([]int, n)

	for i := 0; i < n; i++ {
		sc.Scan()
		fields := strings.Fields(sc.Text())
		g[i] = toIntSlice(fields)
	}

	return &graph{g, c, d, p}
}

func (g *graph) initMst() {
	for i := 0; i < len(g.G); i++ {
		g.C[i] = WHITE
		g.D[i] = INFTY
	}
	g.D[0] = 0
	g.P[0] = -1
}

func (g *graph) primMst() int {
	g.initMst()

	var u int
	for {
		mincost := INFTY
		for i := 0; i < len(g.G); i++ {
			if g.C[i] != BLACK && g.D[i] < mincost {
				mincost = g.D[i]
				u = i
			}
		}

		if mincost == INFTY {
			break
		}

		g.C[u] = BLACK

		for i := 0; i < len(g.G); i++ {
			if g.C[i] != BLACK && g.existEdge(u, i) {
				if g.G[u][i] < g.D[i] {
					g.D[i] = g.G[u][i]
					g.P[i] = u
				}
			}
		}
	}

	totalCost := 0
	for _, cost := range g.D {
		totalCost += cost
	}
	return totalCost
}

func (g *graph) existEdge(u, v int) bool {
	return g.G[u][v] != INFTY && g.G[v][u] != INFTY
}

func toIntSlice(s []string) []int {
	a := make([]int, len(s))
	for i, v := range s {
		num, _ := strconv.Atoi(v)
		if num == -1 {
			num = INFTY
		}
		a[i] = num
	}
	return a
}

func main() {
	g := constructGraph()
	fmt.Println(g.primMst())
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i,n) for(ll i=0; i<(n); i++)
#define REP2(i,x,n) for(ll i=x; i<(n); i++)
#define REP3(i,x,n) for(ll i=x; i>(n); i--)
#define ALL(n) begin(n),end(n)

#define SENTINEL 2000000000
#define NIL -1
struct cww{cww(){ios::sync_with_stdio(false);cin.tie(0);}}star;
const long long INF = numeric_limits<long long>::max();
static const int MAX = 100;
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;
static const int INFTY = (1<<21);

int n,M[MAX][MAX];

//primのアルゴリズム
int prim(){
    int color[MAX],d[MAX],p[MAX];
    int minv;
    for (int u = 0; u < n; u++)
    {
        color[u] = WHITE;
        d[u] = INFTY;
        p[u] = -1;
    }
    d[0]=0; 
    while(1){
        minv = INFTY;
        int u= -1;
        for (int i = 0; i < n; i++)
        {
            if(color[i]!=BLACK&&d[i]<minv){
                minv = 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 (int  i = 0; i < n; i++)
    {
        if(p[i]!=-1){
            sum += M[i][p[i]];
        }
    }
    return sum;
}

int main(){
    cin >> n;
    REP(i,n){REP(j,n){
        int e;
        cin >> e;
        M[i][j] = (e ==-1)?INFTY:e;    
        }
    }
    cout << prim() << endl;

    return 0;
}

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