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>
#include <vector>
#include <stack>
#include <queue>
#include <bitset>
#include <utility>
#include <functional>
#include <numeric>

template <class T>
using vec = std::vector<T>;

constexpr int nil = -1;
constexpr int inf = 1 << 30;

struct Edge
{
    int to;
    int cost;
};

int prim(const vec<vec<Edge>>& edges)
{
    const int n = edges.size();
    vec<bool> reached(n);
    vec<int> parents(n, nil);
    vec<int> distances(n, inf);

    distances[0] = 0;

    while (true)
    {
        int minCost {inf};
        int u {};
        
        for (int i = 0; i < n; ++i)
        {
            if (!reached[i] && distances[i] < minCost)
            {
                u = i;
                minCost = distances[i];
            }
        }
        
        if (minCost == inf)
            break;

        reached[u] = true;

        for (auto e : edges[u])
        {
            if (!reached[e.to])
            {
                if (e.cost < distances[e.to])
                {
                    distances[e.to] = e.cost;
                    parents[e.to] = u;
                }
            }
        }
    }

    int sum = std::accumulate(distances.begin(), distances.end(), 0);

    return sum;
}

void solve()
{
    using namespace std;
    int n;
    cin >> n;

    //隣接リスト形式
    vec<vec<Edge>> edges(n);

    for (int i = 0; i < n; ++i)
    {
        for (int j = 0, cost; j < n; ++j)
        {
            cin >> cost;
            if (cost != -1)
                edges[i].push_back({j, cost});
        }
    }

    cout << prim(edges) << endl;
}

int main()
{
    solve();
    return 0;
}

C++: #include<stdio.h>

#define MAX 1000
#define INFINITY 10000

int n;
int M[MAX][MAX], color[MAX], d[MAX], p[MAX];

enum {
    WHITE,
    GRAY,
    BLACK
};

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

    while (true) {
        int min = INFINITY;
        int target = -1;
        for (int i = 0; i < n; ++i) {
            if (d[i] < min && color[i] != BLACK) {
                min = d[i];
                target = i;
            }
        }
        if (target == -1) break;

        color[target] = BLACK;

        for (int v = 0; v < n; ++v) {
            if (color[v] != BLACK && M[target][v] != -1) {
                if(M[target][v] < d[v]) {
                    d[v] = M[target][v];
                    p[v] = target;
                    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() {
    scanf("%d", &n);
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            scanf("%d", &M[i][j]);
        }
    }
    printf("%d\n", prim());
    return 0;
}

C++: #include <iostream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
static const int Max = 100;
static const int Infinit = 1000000; // ??????
static const int Nil = -1; // 

enum Status {
	Not_Searched = 0,
	Searched = 1,
	Mts = 2 
};

int n;
int M[Max][Max] = {};
Status st[Max] = {}; 
int min_cost[Max] = {}; // i ??????????????????????????§???????°??????????
int parent[Max] = {}; // ??????index

int prim_search() {
	for (int i = 0; i < n; i++) {
		min_cost[i] = Infinit;
		parent[i] = -1;
		st[i] = Not_Searched;
	}
	min_cost[0] = 0;

	while (1) {
		int mincost = Infinit;
		int mincost_idx = -1;

		for (int i = 0; i < n; i++) {
			if (st[i] != Mts && min_cost[i] < mincost) {
				mincost = min_cost[i];
				mincost_idx = i;
			}
		}
		if (mincost_idx == -1) {
			break;
		}
		st[mincost_idx] = Mts;
		for (int i = 0; i < n; i++) {
			if (st[i] != Mts && M[mincost_idx][i] != Infinit) {
				if (min_cost[i] > M[mincost_idx][i]) {
					min_cost[i] = M[mincost_idx][i];
					parent[i] = mincost_idx;
					st[i] = Searched;
				}
			}
		}
	}
	int sum = 0;
	for (int i = 0; i < n; i++) {
		if (parent[i] != Nil) sum += M[i][parent[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) ? Infinit : e;
		}
	}

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

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define WHITE 0
#define GRAY 1
#define BLACK 2
#define INF 2100

int main() {
  int n; cin >> n;
  vector<vector<int>> G(n, vector<int>(n));
  rep(i, n)rep(j, n) cin >> G.at(i).at(j);

  vector<int> color(n); vector<int> mindis(n);
  rep(i, n) mindis.at(i) = INF;
  vector<int> par(n);
  int ans = 0;
  mindis.at(0) = 0;
  color.at(0) = GRAY;
  par.at(0) = -1;

  while (true) {
    int m = INF;
    int now = 0;
    rep(i, n) {
      if (color.at(i) == GRAY && mindis.at(i) < m) {
        m = mindis.at(i);
        now = i;
      }
    }

    if (m == INF) break;

    color.at(now) = BLACK;
    if (par.at(now) != -1) {
      ans += G.at(par.at(now)).at(now);
    }

    rep(i, n) {
      if (G.at(now).at(i) < mindis.at(i) && G.at(now).at(i) != -1 && color.at(i) != BLACK) {
        mindis.at(i) = G.at(now).at(i);
        color.at(i) = GRAY;
        par.at(i) = now;
      }
    }
  }
  cout << ans << endl;
}

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

const int INF = 1000000000;

int main() {
    int n;
    cin >> n;
    vector< vector<int> > cost(n, vector<int>(n, INF));
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            int a;
            cin >> a;
            cost[i][j] = (a == -1) ? INF : a;
        }
    }

    vector<int> mincost(n, INF);
    vector<bool> used(n, false);
    mincost[0] = 0;

    int res = 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[u][v]);
        }
    }

    cout << res << endl;
    return 0;
}