Go: package main

import (
	"bufio"
	"fmt"
	"log"
	"math"
	"os"
	"strconv"
	"strings"
)

type color int

const (
	white color = iota // 未訪問
	mst                // MSTとして確定
)

// 頂点
type node struct {
	matrix   []int // 隣接行列
	minToMst int   // MSTへの重み最小値
	parent   int   // 最小値の相手
	color    color
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	n := eGetInt(scanner)

	nodes := make([]node, n)
	for i := 0; i < n; i++ {
		nodes[i].matrix = eAtoi((strings.Fields(eGetline(scanner))))
		nodes[i].parent = -1
		nodes[i].minToMst = math.MaxInt16
		nodes[i].color = white
	}

	// 初回はnode[0]がMSTに入るように,minToMstを設定
	nodes[0].minToMst = 0
	for { // 全部チェックするまで

		// どの頂点を新たにMSTに取り込むか
		min := math.MaxInt16
		u := -1
		for i := 0; i < n; i++ {
			if nodes[i].color == mst {
				continue
			}
			if nodes[i].minToMst < min {
				u = i
				min = nodes[i].minToMst
			}
		}

		if u == -1 {
			break
		}
		// MSTに追加する頂点決定
		nodes[u].color = mst

		// MSTに属していない頂点それぞれについて,MSTとの最小の重みを更新
		for i := 0; i < n; i++ {
			if nodes[i].color == mst {
				continue
			}
			if nodes[i].matrix[u] == -1 {
				// 接してない
				continue
			}
			if nodes[i].matrix[u] < nodes[i].minToMst {
				// node[u]との重みが最小
				nodes[i].minToMst = nodes[i].matrix[u]
				nodes[i].parent = u
			}
		}
	}

	sum := 0
	for _, v := range nodes {
		// node[0]はparentが入っていない
		if v.parent != -1 {
			sum += v.matrix[v.parent]
		}
	}
	fmt.Println(sum)
}

func eGetInt(scanner *bufio.Scanner) int {
	t := eAtoi(strings.Fields(eGetline(scanner)))
	return t[0]
}

func eGetline(p *bufio.Scanner) string {
	if !p.Scan() {
		if err := p.Err(); err != nil {
			log.Fatal(err.Error())
		}
		return ""
	}
	return p.Text()
}

func eAtoi(s []string) []int {
	ints := make([]int, len(s))
	var err error
	for i, e := range s {
		ints[i], err = strconv.Atoi(e)
		if err != nil {
			log.Fatal(err.Error())
		}
	}
	return ints
}

C++: #include<iostream>
#include<cstdio>
#include<cstring>
#include <cstdlib>  
#include <cmath>   
#include<cctype>
#include<string>
#include<set>
#include <map>
#include<algorithm>
#include <functional>
#include<vector>
#include<climits>
#include<stack>
#include<queue>
#include <deque>
#include <typeinfo>
#include <utility> 

#define rep(i,m,n) for(int i = m;i < n;++i)
using namespace std;
using ll = long long;
using R = double;
const ll inf = 1LL << 50;
const ll MOD = 1e9 + 7;

int infty = (1 << 21);
int cost[100][100];
int mincost[100];
bool used[100];
int V;

int prim() {
	for (int i = 0; i < V; ++i) {
		mincost[i] = infty;
		used[i] = false;
	}
	mincost[0] = 0;
	int res = 0;

	while (1) {
		int v = -1;
		for (int u = 0; u < V; 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 < V; u++) {
			mincost[u] = min(mincost[u], cost[v][u]);
		}
	}
	return res;
}


int main() {
	cin >> V;
	rep(i, 0, V)rep(j, 0, V) {
		int e; cin >> e;
		cost[i][j] = (e == -1) ? infty : e;
	}

	cout << prim() << endl;
}

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

struct Edge {
    int from, to, cost;
    Edge(int f, int t, int c) : from(f), to(t), cost(c) { }
    bool operator <(const Edge& e) const {
        return cost < e.cost;
    }
};

struct UnionFind {
    vector<int> data;
    UnionFind(int n) : data(n, -1) { }
    int root(int x) {
        return data[x] < 0 ? x : data[x] = root(data[x]);
    }

    bool same(int x, int y) {
        return root(x) == root(y);
    }

    void merge(int x, int y) {
        x = root(x), y = root(y);
        if(x != y) {
            data[x] += data[y]; data[y] = x;
        }
    }
};

int main() {
    int N; cin >> N;
    vector<Edge> es;
    for(int i = 0; i < N; i++) {
        for(int j = 0; j < N; j++) {
            int c; cin >> c;
            if(c != -1) {
                es.push_back(Edge(i, j, c));
            }
        }
    }
    sort(es.begin(), es.end());
    UnionFind uf(N);
    int ans = 0;
    for(int i = 0; i < (int)es.size(); i++) {
        int from = es[i].from;
        int to = es[i].to;
        int cost = es[i].cost;
        if(uf.same(from, to)) continue;
        uf.merge(from, to);
        ans += cost;
    }
    cout << ans << endl;
}

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

typedef pair< int , int > P;
struct Edge {
  int to,cost;
  Edge(){}
  Edge(int to,int cost):to(to),cost(cost){};
};
vector< vector< Edge > > Graph;
int V,E;
int prim(){
  int total = 0;
  vector< bool > used(Graph.size(),false);
  priority_queue< P,vector< P >,greater< P > > que;
  que.push(P(0,0));
  while(!que.empty()){
    int cost = que.top().first;
    int now = que.top().second;
    que.pop();
    if(used[now]) continue;
    used[now] = true;
    total += cost;
    for(int i=0;i<Graph[now].size();i++){
      que.push(P(Graph[now][i].cost,Graph[now][i].to));
    }
  }
  return total;
}

int main(){
  cin >> V;
  Graph.resize(V+1);
  /*
  for(int i=0;i<E;i++){
    int s,t,d;
    cin >> s >> t >> d;
    Graph[s].push_back(Edge(t,d));
    Graph[t].push_back(Edge(s,d));
    }*/
  for(int i=0;i<V;i++){
    for(int j=0;j<V;j++){
      int d; cin >> d;
      if(d != -1){
	Graph[i].push_back(Edge(j,d));
	Graph[j].push_back(Edge(i,d));
      }
    }
  }
  cout << prim() << endl;
}

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

int main(){
  vector<vector<int> > graph;
  vector<int> master_node;
  vector<int> distance;
  vector<int> to_neighbor;
  int n;

  cin >> n;
  n++;
  graph.resize(n);
  master_node.resize(n);
  distance.resize(n);
  to_neighbor.resize(n);
  for(int i=1;i<n;i++){
    graph[i].resize(n);
    master_node[i]=0;
    distance[i]=INT_MAX;
    to_neighbor[i]=INT_MAX;
  }

  for(int i=1;i<n;i++){
    for(int j=1;j<n;j++){
      int input;

      cin >> input;
      graph[i][j]=(input!=-1?input:INT_MAX);
    }
  }


  int from=1;

  master_node[1]=1;
  distance[1]=0;
  for(int i=1;i<n;i++){
    to_neighbor[i]=(graph[1][i]<to_neighbor[i]?graph[1][i]:to_neighbor[i]);
  }
  while(true){
    int min_distance=INT_MAX,to;

    for(int i=1;i<n;i++){
      if((to_neighbor[i]<min_distance)&&(master_node[i]==0)){
        min_distance=to_neighbor[i];
	to=i;
      }
    }
    if(min_distance==INT_MAX){
      break;
    }else{
      master_node[to]=from;
      distance[to]=min_distance;
      from=to;
      to_neighbor[from]=INT_MAX;
      for(int i=1;i<n;i++){
        to_neighbor[i]=(graph[from][i]<to_neighbor[i]?graph[from][i]:to_neighbor[i]);
      }
    }
  }


  int sum=0;
  
  for(int i=1;i<n;i++){
    sum+=distance[i];
  }
  cout << sum << endl;
}

C++: #include<iostream>
#include<iomanip>
#include<algorithm>
#include<bitset>
#include<cctype>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<functional>
#include<limits>
#include<list>
#include<map>
#include<set>
#include<stack>
#include<string>
#include<sstream>
#include<queue>
#include<vector>
using namespace std;

#define MOD 1000000007
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL

#define MAX 100
#define WHITE 0
#define GRAY 1
#define BLACK 2

int n, M[MAX][MAX];

int prim() {
	int d[MAX],
		p[MAX] = { -1 },
		color[MAX] = {};//WHITE
	fill(d, d + MAX, INF);
	d[0] = 0;

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

		if (minv == INF) { break; }

		color[u] = BLACK;

		for (int v = 0; v < n; v++) {
			if (color[v] != BLACK&&M[u][v] != INF) {
				if (M[u][v] < d[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 a; cin >> a;
			M[i][j] = (a == -1) ? INF : a;
		}
	}

	cout << prim() << endl;

	return 0;
}