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 <iostream>
using namespace std;

#define MAX 100
#define INFTY 1 << 21
#define WHITE 0
#define GRAY 1
#define BLACK 2

// 隣接行列
int gMatrix[MAX][MAX];

int prim(int n){
    // 頂点
    int u;
    // 最小の重み
    int minV;
    // Tに属する頂点とV - Tに属する頂点をつなぐ辺の中で、重みが最小の辺の重みを記録する
    int weight[MAX];
    // MSTにおける頂点vの親を記録する
    int parent[MAX];
    // 訪問状態を記録する
    int color[MAX];
    
    for(int i = 0;i < n;++i){
        weight[i] = INFTY;
        parent[i] = -1;
        color[i] = WHITE;
    }
    
    weight[0] = 0;
    
    while(1){
        minV = INFTY;
        u = -1;
        
        for(int i = 0;i < n;++i){
            if(minV > weight[i] && color[i] != BLACK){
                // 最小の重みを記録する
                u = i;
                minV = weight[i];
            }
        }
        // 全て訪問した
        if(u == -1)break;
        // 訪問済み
        color[u] = BLACK;
        for(int v = 0;v < n;++v){
            if(color[v] != BLACK && gMatrix[u][v] != INFTY){
                if(weight[v] > gMatrix[u][v]){
                    weight[v] = gMatrix[u][v];
                    parent[v] = u;
                    color[v] = GRAY;
                }
            }
        }
    }
    int sum = 0;
    for(int i = 0;i < n;++i){
        if(parent[i] != -1) sum += gMatrix[i][parent[i]];
    }
    
    return sum;
}

int main(){
    int n;
    cin >> n;
    
    for(int i = 0;i < n;++i){
        for(int j = 0;j < n;++j){
            int edge;
            cin >> edge;
            gMatrix[i][j] = (edge == -1) ? INFTY : edge;
        }
    }
    
    cout << prim(n) << endl;
    return 0;
}

C++: #include <iostream>

using namespace std;

int n;
int a[101][101];
int flg[101];
int t[101];
int sum = 0;

void visit(){
    int ima = 0;
    int mine = 100000000;
    for (int k = 0; k < n-1; k++) {
        mine = 10000000;
        for (int i = 0; i < n; i++) {
            if (flg[i] == 0 && mine > t[i]) {
	            mine = t[i];
	            ima = i;
            }
        }
        sum += mine;
        flg[ima] = 1;
        for (int i = 0; i < n; i++){ 
            if (a[ima][i] != -1){ 
	            if (t[i] > a[ima][i]){
	                t[i] = a[ima][i];
	            }
            }
        }
    }
}

int main() {
    cin >> n;
    for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++){ 
            cin >> a[i][j];
        }
    }
    for(int i = 0; i < n; i++) {
        flg[i] = 0;
        t[i] = 100000000;
    }
    t[0] = 0;
    flg[0] = 1;
    for(int i = 0; i < n;i++){ 
        if(a[0][i] != -1){
            t[i] = a[0][i];
        }
    }
    visit();
    cout << sum << endl;
    return 0; 
}

C++: #include <iostream>
using namespace std;

int n;
int a[101][101];
int flg[101];
int t[101];
int sum = 0;

void visit(){
  int ima = 0;
  int mine = 100000000;
  
  
  for(int k=0;k<n;k++) {
    
    for(int i=0;i<n;i++) {
      if(flg[i] == 0 && mine > t[i]) {
	mine = t[i];
	ima = i;
      }
    }
    sum += mine;
    flg[ima] = 1;
    
    for(int i=0;i<n;i++) 
      if(a[ima][i] != -1) 
	if(t[i] > a[ima][i] ) t[i] = a[ima][i];
    
    
  }
}

int main() {

  cin >> n;

  for(int i=0;i<n;i++) 
    for(int j=0;j<n;j++) 
      cin >> a[i][j];
  
  for(int i=0;i<n;i++) {
    flg[i] = 0;
      t[i] = 100000000;
  }
  
  t[0] = 0;
  flg[0] = 1;
  for(int i=0;i<n;i++) 
    if(a[0][i] != -1) t[i] = a[0][i];

  visit();

  cout << sum << endl;
 
  return 0; 
  
}

C++: #include<iostream>
using namespace std;
static const int MAX = 100;
static const int INFTY = (1<<21);
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,i,v;
    int d[MAX],p[MAX],color[MAX];
    int sum = 0;

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

    d[0] = 0;

    while(1)
    {
        minv = INFTY;
        u = -1;

        for(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(v=0;v<n;v++)
        {
            if(color[v] != BLACK && M[u][v] != INFTY)
            {
                if(d[v] > M[u][v])
                {
                    d[v] = M[u][v];
                    p[v] = u;
                    color[v] = GRAY;
                }
            }
        }
    }

   

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

    return sum;
}

int main()
{
    int i,j,e;
    cin >> n;

    for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++)
        {
            cin >> e;
            M[i][j] = (e == -1) ? INFTY : e;
        }
    }

    cout << prim() << endl;

    return 0;
    
}

C++: #include <bits/stdc++.h>
#define rep(i, a, b) for(int i = a; i < b; i++)
#define Rep(i, a, b) for(int i = a; i <= b; i++)
#define _GLIBCXX_DEBUG
#define Vl vector<ll>
#define Vs vector<string>
#define Vp vector<pair<ll, ll>>
#define ll long long
#define ALL(v) (v).begin(),(v).end()
using namespace std;
const double pi = acos(-1.0);
const ll MOD = 1e9 + 7;
const ll INF = 1LL << 60;
using Graph = vector<vector<ll>>;

ll n;
ll M[110][110]; // kyori
const int white = 0, gray = 1, black = 2;

ll prim(){
  ll u, minv;
  ll d[10000], p[10000], color[10000];

  rep(i, 0, n){
    d[i] = INF;
    p[i] = -1; // parent
    color[i] = white;
  }
  d[0] = 0;
  while(1){
    minv = INF;
    u = -1;
    rep(i, 0, n){
      if(minv > d[i] && color[i] != black){
        u = i;
        minv = d[i];
      }
    }
    if(u == -1) break;
    color[u] = black;
    rep(v, 0, n){
      if(color[v] != black && M[u][v] != INF){
        if(d[v] > M[u][v]){
          d[v] = M[u][v];
          p[v] = u;
          color[v] = gray;
        }
      }
    }
  }
    ll sum = 0;
    rep(i, 0, n){
    if(p[i] != -1) sum += M[i][p[i]];
    }

    return sum;
}

int main() {
  cin >> n;
  rep(i, 0, n){
    rep(j, 0, n){
      ll a; cin >> a;
      M[i][j] = (a == -1) ? INF : a;
    }
  }

  cout << prim() << endl;

  return 0;
}