Go: package main

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

// I/O
type Scanner struct {
	sc *bufio.Scanner
}

func NewScanner() *Scanner {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 1024), int(1e+9))
	return &Scanner{sc}
}

func (s *Scanner) nextStr() string {
	s.sc.Scan()
	return s.sc.Text()
}

func (s *Scanner) nextInt() int {
	i, e := strconv.Atoi(s.nextStr())
	if e != nil {
		panic(e)
	}
	return i
}

func (s *Scanner) nextFloat() float64 {
	f, e := strconv.ParseFloat(s.nextStr(), 64)
	if e != nil {
		panic(e)
	}
	return f
}

func (s *Scanner) nextRuneSlice() []rune {
	return []rune(s.nextStr())
}

func (s *Scanner) nextIntSlice(n int) []int {
	res := make([]int, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextInt()
	}
	return res
}

func (s *Scanner) nextFloatSlice(n int) []float64 {
	res := make([]float64, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextFloat()
	}
	return res
}

// Arithmetic
func max(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m < i {
			m = i
		}
	}
	return m
}

func min(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m > i {
			m = i
		}
	}
	return m
}

func abs(x int) int {
	if x > 0 {
		return x
	}
	return -x
}

func pow(x, y int) int {
	res := 1
	for i := 0; i < y; i++ {
		res *= x
	}
	return res
}

func ceil(a, b int) int {
	if a%b == 0 {
		return a / b
	} else {
		return a/b + 1
	}
}

// Sort
type RuneSlice []rune

func (a RuneSlice) Len() int           { return len(a) }
func (a RuneSlice) Less(i, j int) bool { return a[i] < a[j] }
func (a RuneSlice) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

const INF = 1 << 60

// Main
func main() {
	sc := NewScanner()
	wtr := bufio.NewWriter(os.Stdout)
	N := sc.nextInt()
	G := make([][]int, N)
	for i := 0; i < N; i++ {
		G[i] = make([]int, N)
		for j := 0; j < N; j++ {
			G[i][j] = sc.nextInt()
		}
	}
	d := make([]int, N)
	for i := 0; i < N; i++ {
		d[i] = INF
	}
	seen := make([]bool, N)
	d[0] = 0
	ans := 0
	for {
		cur := 0
		minCost := INF
		for i := 0; i < N; i++ {
			if !seen[i] && d[i] < minCost {
				minCost = d[i]
				cur = i
			}
		}
		if minCost == INF {
			break
		}
		seen[cur] = true
		ans += d[cur]
		for i := 0; i < N; i++ {
			cost := G[cur][i]
			if cost != -1 && cost < d[i] {
				d[i] = cost
			}
		}
	}

	fmt.Fprintln(wtr, ans)
	wtr.Flush()
}

C++: #include <bits/stdc++.h>
using namespace std;

struct UnionFind {
  vector<int> data;
  UnionFind() {}
  UnionFind(int n) : data(n, -1) {}
  bool same(int x, int y) { return root(x) == root(y); }
  int root(int x) { return (data[x] < 0) ? x : data[x] = root(data[x]); }
  int rank(int x) { return -data[root(x)]; }
  bool unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x != y) {
      if (data[y] < data[x]) swap(x, y);
      data[x] += data[y];
      data[y] = x;
    }
    return x != y;
  }
};
template<typename T> struct Graph {
  struct Edge { int s, t; T cost; };
  int sz;
  vector<Edge> es;
  Graph(int n = 0) : sz(n) {}

  void addEdge(int u, int v, T w) {
    sz = max(sz, max(u, v) + 1);
    es.push_back({u, v, w});
  }
  T kruskal() {
    UnionFind uf(sz);
    sort(begin(es), end(es), [](Edge e1, Edge e2) {
      return e1.cost < e2.cost;
    });
    T res = 0;
    for (auto&& e : es) {
      if (uf.unite(e.s, e.t)) res += e.cost;
    }
    return res;
  }
};

int main() {
  int n; cin >> n;
  vector<vector<int>> table(n, vector<int>(n));
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      cin >> table[i][j];
    }
  }
  Graph<int> g;
  for (int i = 0; i < n; ++i) {
    for (int j = i + 1; j < n; ++j) {
      if (table[i][j] != -1) g.addEdge(i, j, table[i][j]);
    }
  }
  cout << g.kruskal() << endl;
  return 0;
}

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

int n;
int M[100][100];

int prim(){
  int d[100]; //最小コスト
  int color[100];
  int p[100];
  for(int i=0;i<n;i++){
    d[i] = (1 << 21);
    color[i] = 0;
    p[i] = -1;
  }
  d[0] = 0;
  p[0] = 0;

  int mincost;
  int u;
  int cost = 0;

  while(true){
    mincost = (1 << 21);
    for(int i=0;i<n;i++){
      if(color[i] != 2 && d[i] < mincost){
        mincost = d[i];
        u = i;
      }
    }

    if(mincost == (1 << 21)) break;

    color[u] = 2;
    cost += mincost;

    // uと隣接する頂点についてdの更新
    for(int i=0;i<n;i++){
      if(M[u][i] != -1 && color[i] != 2 && M[u][i] < d[i]){
        d[i] = M[u][i];
        p[i] = u;
        color[i] = 1;
      }
    }
  }
  return cost;
}

int main(){
  cin >> n;
  for(int i=0;i<n;i++){
    for(int j=0;j<n;j++){
      cin >> M[i][j];
    }
  }
  cout << prim() << endl;
  return 0;
}

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

static const int N=100;
static const int INFTY=2001;
static const int WHITE=0;//未探索
static const int GRAY=1;//MSTに隣接
static const int BLACK=2;//MSTに含まれる

int n,v,M[N][N];

int prim(){
    int sum_weight; //MSTの辺の重みの総和
    int cost[N]; //MSTから頂点に伸びている辺の重みの最小値をその頂点に保持する
    int color[N]; //MSTか、MSTに隣接しているか、それ以外か
    //初期化
    for(int i=0;i<n;i++){
        cost[i]=INFTY;
        color[i]=WHITE;
    }
    cost[0]=0;
    color[0]=GRAY;
    sum_weight=0;

    while(true){
        int vertex=-1;//次に含む頂点の候補
        int min_cost=INFTY;//次に選ぶ辺の最小コスト
        for(int i=0;i<n;i++){
            //MSTに隣接した頂点から最小コストのものを探す
            if(color[i]==GRAY&&cost[i]<min_cost){
                min_cost=cost[i];
                vertex=i;
            }
        }
        //T(部分グラフ)＝Vなら終了
        if(vertex==-1) break;
        //該当の頂点がある場合
        color[vertex]=BLACK;
        sum_weight+=cost[vertex];

        //vertexをMSTに含めたために、それによる他の頂点の最小コストの変化と状態を更新
        for(int i=0;i<n;i++){
            if(color[i]!=BLACK&&M[vertex][i]<INFTY){
                color[i]=GRAY;
                if(cost[i]>M[vertex][i]) cost[i]=M[vertex][i];
            }
        }
    }
    return sum_weight;
}

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

    cout<<prim()<<endl;
}

C++: #define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <vector>
#include <functional>
using namespace std;
const int num = 10000;
const int INF = 2000000000;
int c[num];
int pe[num];
int d[num];
typedef pair<int, int> p;
vector <p> v[num];
int main() {
	int n;
	cin >> n;
	fill(pe, pe + n, INF);
	fill(c, c + n, 0);
	fill(d, d + n, INF);
	d[0] = 0;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			int w; cin >> w;
			if (w != -1) v[i].push_back(p(w, j));
		}
	}
	priority_queue <p, vector<p>, greater<p>> q;
	for (int i = 0; i < v[0].size(); i++) {
		q.push(p(v[0][i].first, v[0][i].second));
		d[v[0][i].second] = v[0][i].first;
		pe[v[0][i].second] = 0;
	}
	c[0] = 1;
	while (!q.empty()) {
		int f = q.top().first;
		int s = q.top().second;
		q.pop();
		c[s] = 1;
		if (d[s] < f) continue;
		for (int i = 0; i < v[s].size(); i++) {
			int fi = v[s][i].first;
			int se = v[s][i].second;
			if (c[se]) continue;
			if (pe[se] == INF || d[se] > fi) {
				d[se] = fi;
				pe[se] = s;
				q.push(p(fi, se));
			}
		}
	}
	int sum = 0;
	for (int i = 0; i < n; i++)
		sum += d[i];
	cout << sum << endl;
	return 0;
}