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 <climits>
#include <iostream>
#include <vector>

using namespace std;

// calculate Minimum Spanning Tree with Prim's algorithm
void mst(vector<vector<int>>& g, vector<int>& weights) {
    int n_vertex = weights.size();
    vector<bool> mst(n_vertex, false);

    weights[0] = 0;
    while (true) {
        int idx;
        int min_weight = INT_MAX;
        for (int i = 0; i < n_vertex; ++i) {
            if (!mst[i] && weights[i] < min_weight) {
                idx = i;
                min_weight = weights[i];
            }
        }

        if (min_weight == INT_MAX)
            break;

        mst[idx] = true;

        for (int i = 0; i < n_vertex; ++i) {
            if (!mst[i] && g[idx][i] != -1)
                weights[i] = min(weights[i], g[idx][i]);
        }
    }
}

int main() {
    int n;
    cin >> n;

    vector<vector<int>> g(n, vector<int>(n, 0));
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j)
            cin >> g[i][j];
    }

    vector<int> weights(n, INT_MAX);
    mst(g, weights);

    int sum_of_weights = 0;
    for (int i = 0; i < n; ++i)
        sum_of_weights += weights[i];
    cout << sum_of_weights << '\n';
}

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

const int N = 100;
int n;
int A[N][N];
bool visit[N] = {0};
priority_queue< pair<int, int> > Q;

int main()
{
	cin >> n;
	for (int i=0; i<n; i++)
	{
		for (int j=0; j<n; j++)
		{
			cin >> A[i][j];
		}
	}

	Q.push(pair<int, int>(0,0));

	int cost = 0;

	while (Q.size())
	{
		pair<int, int> q = Q.top();
		Q.pop();
		int k = q.second;
		if (visit[k]) continue;
		cost += q.first;
		visit[k]=true;
		for(int j=0;j<n;j++)
		{
			int c = A[k][j];
			if (c <= 0) continue;
			if (visit[j]) continue;
			Q.push(pair<int, int>(-c,j));
		}
	}

	cout << -cost << endl;
		
	return 0;
}

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

const int N = 100;
int n;
int A[N][N];
bool visit[N] = {0};
priority_queue< pair<int, int> > Q;

int main()
{
	cin >> n;
	for (int i=0; i<n; i++)
	{
		for (int j=0; j<n; j++)
		{
			cin >> A[i][j];
		}
	}

	Q.push(pair<int, int>(0,0));
	visit[0] = true;

	int cost = 0;

	while (Q.size())
	{
		pair<int, int> q = Q.top();
		Q.pop();
		int k = q.second;
		if (visit[k]) continue;
		cost += q.first;
		visit[k]=true;
		for(int j=0;j<n;j++)
		{
			int c = A[k][j];
			if (c <= 0) continue;
			if (visit[j]) continue;
			Q.push(pair<int, int>(-c,j));
		}
	}

	cout << -cost << endl;
		
	return 0;
}

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

static const int MAX = 100;
static const int INF = 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;
    int d[MAX], p[MAX], color[MAX];

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

    d[0] = 0;

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

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

        if(u == -1) break;

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

    int sum = 0;

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

    return sum;
}

int main(){
    cin >> n;

    for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++){
            int x;
            cin >> x;
            if(x == -1) M[i][j] = INF;
            else        M[i][j] = x;
        }
    }

    cout << prim() << endl;
    
    return 0;
}

C++: #include <iostream>
#include <string>
#define MAX_N 110
using namespace std;
typedef long long ll;
const int INF = 1<<30; 
const int MOD = 1e9 + 7;
int cost[MAX_N][MAX_N], mincost[MAX_N];
bool used[MAX_N];
int prim(int n)
{
    int res = 0;
    mincost[0] = 0;
    while(true)
    {
        int v = -1;
        for(int u = 0; u < n; u++)
        {
            if(!used[u] && (v == -1 || mincost[u] < mincost[v])) v = u;
        }
        if(v == -1) break;
        used[v] = true;
        res += mincost[v];
        for(int u = 0; u < n; u++) mincost[u] = min(mincost[u], cost[v][u]);
    }
    return res;
}
int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n; cin >> n;
    for(int i = 0; i < MAX_N; i++) used[i] = false;
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < n; j++)
        {
            cin >> cost[i][j];
            if(cost[i][j] == -1) cost[i][j] = INF;
        }
        mincost[i] = INF;
    }
    int ans = prim(n);
    cout << ans << 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;
  int d[MAX], p[MAX], color[MAX]; // d:??±??? ,p:??????v?????? ,color:????????????

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

  d[0] = 0;
  
  while(1){

    minv = INFTY;
    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 && M[u][v] != INFTY){
	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(){
  cin >> n;

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

  return 0;  
}