Go: package main

import (
	"bufio"
	"container/heap"
	"fmt"
	"os"
	"strconv"
)

type Node struct {
	id     int
	weight int
}

func init() {
	sc.Split(bufio.ScanWords)
}

type NodeHeap []Node

func (h NodeHeap) Len() int           { return len(h) }
func (h NodeHeap) Less(i, j int) bool { return h[i].weight < h[j].weight }
func (h NodeHeap) Swap(i, j int)      { h[i], h[j] = h[j], h[i] }

func (h *NodeHeap) Push(x interface{}) {
	*h = append(*h, x.(Node))
}

func (h *NodeHeap) Pop() interface{} {
	old := *h
	n := len(old)
	x := old[n-1]
	*h = old[0 : n-1]
	return x
}

var sc = bufio.NewScanner(os.Stdin)

func scanInt() int {
	num, _ := strconv.Atoi(scanString())
	return num
}

func scanString() string {
	sc.Scan()
	return sc.Text()
}

func init() {
	sc.Split(bufio.ScanWords)
}

func main() {
	n := scanInt()

	list := make([][]int, n)
	seen := make([]bool, n)
	for i := range list {
		list[i] = make([]int, n)
		for j := range list[i] {
			list[i][j] = scanInt()
		}
	}

	start := Node{id: 0, weight: 0}
	h := &NodeHeap{start}
	heap.Init(h)

	sum := 0
	for h.Len() > 0 {
		node := heap.Pop(h).(Node)
		if seen[node.id] {
			continue
		}
		seen[node.id] = true
		sum += node.weight

		for i, w := range list[node.id] {
			if w < 0 {
				continue
			}
			heap.Push(h, Node{id: i, weight: w})
		}
	}

	fmt.Println(sum)
}

C++: #include <iostream>
#include <vector>
#include <queue>

using namespace std;

using vi = vector<int>;
using vvi = vector<vi>;
using pii = pair<int, int>;

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define IN(v) int v;cin>>v;
#define OUT(v) cout<<v<<endl

int main() {
  IN(N);

  vvi A(N, vi(N, -1));
  REP(i, N) {
    REP(j, N) {
      IN(a);
      A[i][j] = a;
    }
  }
  priority_queue<pii, vector<pii>, greater<pii>> edge;
  REP(i, N) {
    if (A[0][i] != -1)
      edge.push(make_pair(A[0][i], i));
  }
  vector<bool> visited(N, false);
  visited[0] = true;
  int ans = 0;
  while (!edge.empty()) {
    pii p = edge.top(); edge.pop();
    if (visited[p.second])
      continue;
    visited[p.second] = true;
    ans += p.first;
    REP(i, N) {
      if (A[p.second][i] != -1 && !visited[i])
        edge.push(make_pair(A[p.second][i], i));
    }
  }
  OUT(ans);

  return 0;
}

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

static const int MAX=100;
static const int WHITE=0;
static const int GRAY=1;
static const int BLACK=2;

int n,M[MAX][MAX];

int prime(){
  int u,mincost;
  int d[MAX],p[MAX],color[MAX];

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

  d[0]=0;

  while(1){
    mincost=INT_MAX;
    u=-1;
    for(int i=0;i<n;i++){
      if(color[i]!=BLACK&&d[i]<mincost){
	mincost=d[i];
	u=i;
      }
    }
 
    if(mincost==INT_MAX) break;
    
    color[u]=BLACK;

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

  int sum=0;
  for(int i=0;i<n;i++) sum+=d[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;
      if(e==-1) M[i][j]=INT_MAX;
      else M[i][j]=e;
    }
  }
  cout<<prime()<<endl; 
  return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

/* UnionFind：素集合系管理の構造体(union by rank)
	isSame(x, y): x と y が同じ集合にいるか。 計算量はならし O(α(n))
	unite(x, y): x と y を同じ集合にする。計算量はならし O(α(n))
*/
struct UnionFind {  // The range of node number is u 0 v n-1
	vector<int> rank, parents;
	UnionFind() {}
	UnionFind(int n) {  // make n trees.
		rank.resize(n, 0);
		parents.resize(n, 0);
		for (int i = 0; i < n; i++) {
			makeTree(i);
		}
	}
	void makeTree(int x) {
		parents[x] = x;  // the parent of x is x
		rank[x] = 0;
	}
	bool isSame(int x, int y) { return findRoot(x) == findRoot(y); }
	void unite(int x, int y) {
		x = findRoot(x);
		y = findRoot(y);
		if (rank[x] > rank[y]) {
			parents[y] = x;
		}
		else {
			parents[x] = y;
			if (rank[x] == rank[y]) {
				rank[y]++;
			}
		}
	}
	int findRoot(int x) {
		if (x != parents[x]) parents[x] = findRoot(parents[x]);
		return parents[x];
	}
};

// 辺の定義
struct Edge {
	long long u;
	long long v;
	long long cost;
};

bool comp_e(const Edge &e1, const Edge &e2) { return e1.cost < e2.cost; } // 辺を直接比較するための関数

/* Kruskal :クラスカル法で minimum spanning tree を求める構造体
	入力: 辺のvector, 頂点数V
	最小全域木の重みの総和: sum
	計算量: O(|E|log|V|)
*/

struct Kruskal {
	UnionFind uft;
	long long sum;  // 最小全域木の重みの総和
	vector<Edge> edges;
	int V;
	Kruskal(const vector<Edge> &edges_, int V_) : edges(edges_), V(V_) { init(); }
	void init() {
		sort(edges.begin(), edges.end(), comp_e); // 辺の重みでソート
		uft = UnionFind(V);
		sum = 0;
		for (auto e : edges) {
			if (!uft.isSame(e.u, e.v)) { // 閉路にならなければ加える
				uft.unite(e.u, e.v);
				sum += e.cost;
			}
		}
	}
};

int main() {

	int n;
	cin >> n;
	vector<vector<int>> VE(n, vector<int>(n));
	int counter=0;

	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			cin >> VE[i][j];
			if (VE[i][j] != -1) { counter++; }
		}
	}

	int V, E;
	V = n;
	E = counter;
//	cin >> V >> E;
	vector<Edge> edges(E);
//	for (int i = 0; i < E; i++) {
	counter = 0;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			long long s, t, w;
			//		cin >> s >> t >> w;
			if (VE[i][j] != -1) {
				s = i;
				t = j;
				w = VE[i][j];

				Edge e = { s, t, w };
				edges[counter] = e;
				counter++;
			}
		}
	}
//	}


	Kruskal krs(edges, V);
	cout << krs.sum << endl;
	return 0;
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
#define NIL -1
static const int INFTY=(1<<21);
int color[102];
int d[102][102];
int n,di,sum=0;
vector<int> searched;
void count(int start){
    int min=INFTY,minp,a,b,c;
    color[start]=2;
    searched.push_back(start);
    for(int i=0;i<n;i++){
        a=d[start][i];
        if(a!=NIL&&color[i]==0) color[i]++;
    }
    for(int i=0;i<searched.size();i++){
        b=searched[i];
        for(int j=0;j<n;j++){
            c=d[b][j];
            if(color[j]==1&&c<min&&c!=NIL){
                min=c; 
                minp=j;
                }
        }
    }
    if(min!=INFTY){
            sum+=min;
            count(minp);
    }

}
int main(){
    cin>>n;
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            d[i][j]=NIL;
            cin>>di;
            d[i][j]=di;
        }
        color[i]=0;
    }
    count(0);
    cout<<sum<<endl;
    return 0;
}