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 <iostream>
#include <vector>
#include <unordered_set>
#include <utility>
#include <algorithm>
#include <cassert>
using namespace std;

class DisjointSet {
private:
	vector<int> P;		//添え字の番号の頂点の親
	vector< unordered_set<int> > Set;		//添え字の番号の頂点に子として繋がっている頂点の集合
public:
	DisjointSet(int n);
	void unite(int x, int y);
	bool same(int x, int y);
};

DisjointSet::DisjointSet(int n) : P(n), Set(n) {
	for (int i = 0; i<n; ++i) {
		P[i] = i;
		Set[i].insert(i);
	}
}

void DisjointSet::unite(int x, int y) {
	if (P[x] == P[y])
		return;
	int big, small;
	if (Set[P[x]].size() >= Set[P[y]].size()) {
		big = P[x];
		small = P[y];
	}
	else {
		big = P[y];
		small = P[x];
	}
	for (auto i = Set[small].begin(); i != Set[small].end(); ++i) {
		P[*i] = big;
		Set[big].insert(*i);
	}
}

bool DisjointSet::same(int x, int y) {
	return P[x] == P[y];
}

class MSTree : public DisjointSet {
private:
	const int vertex_n;
	vector< pair<int, int> > edge;		//firstは辺の重さ、secondは辺のID(始点・終点)
public:
	MSTree(int n) : DisjointSet(n), vertex_n(n), edge(n*n) {
		for (int i = 0; i < n*n; i++) {
			edge[i].first = -1;
			edge[i].second = i;
		}
	}
	int& GetEdgeWeight(int i, int j) { return edge[vertex_n*i + j].first; }
	int MinEdgeSum();
};

int MSTree::MinEdgeSum() {
	int sum = 0;
	sort(edge.begin(), edge.end());
	for (int i = 0; i < vertex_n*vertex_n; i++) {
		int from = edge[i].second / vertex_n, to = edge[i].second % vertex_n;
		if (edge[i].first == -1 || this->same(from, to))
			continue;
		else {
			this->unite(from, to);
			sum += edge[i].first;
		}
	}
	return sum;
}

int main() {
	int n;
	cin >> n;
	MSTree mst(n);
	for (int i = 0; i < n; i++)
		for (int j = 0; j < n; j++)
			cin >> mst.GetEdgeWeight(i, j);
	cout << mst.MinEdgeSum() << endl;
}

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

int n;
int a[101][101];
int flag[101];
int t[101];
int sum = 0;

void prim(){
  int ima = 0;
  int mine = 100000000;
  
  
  for(int k=0;k<n-1;k++) {
    mine = 10000000;
    for(int i=0;i<n;i++) {
      if(flag[i] == 0 && mine > t[i]) {
	mine = t[i];
	ima = i;
      }
    }
    sum += mine;
    flag[ima] = 1;
    
    for(int i=0;i<n;i++) 
      if(a[ima][i] != -1) 
	if(t[i] > a[ima][i] ) t[i] = a[ima][i];
    
    
  }
}

int main() {

  cin >> n;

  for(int i=0;i<n;i++) 
    for(int j=0;j<n;j++) 
      cin >> a[i][j];
  
  for(int i=0;i<n;i++) {
    flag[i] = 0;
    t[i] = 100000000;
  }
  
  t[0] = 0;
  flag[0] = 1;
  for(int i=0;i<n;i++) 
    if(a[0][i] != -1) t[i] = a[0][i];

  prim();

  cout << sum << endl;
 
  return 0; 
  
}

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

struct Edge{
  int weight;
  int to;
  Edge(int w,int t):weight(w),to(t){}
};

const int inf = 1<<28;

bool operator>(Edge e1,Edge e2){
  if(e1.weight != e2.weight) return (e1.weight > e2.weight);
  else return (e1.to > e2.to);
}

int main(){
  int n;
  cin>>n;
  vector<vector<Edge> > Graph(n);
  for(int i=0;i<n;i++){
    for(int j=0;j<n;j++){
      int temp;
      cin>>temp;
      if(temp != -1){
	Graph[i].push_back(Edge(temp,j));
      }
    }
  }
  priority_queue<Edge,vector<Edge>,greater<Edge> > pq;
  pq.push(Edge(0,0));
  vector<bool> used(n,false);
  vector<int> cost(n,inf);
  int ans = 0;
  while(pq.size()){
    Edge e = pq.top();
    pq.pop();
    if(used[e.to]) continue;
    used[e.to] = true;
    ans += e.weight;
    for(int i=0;i<Graph[e.to].size();i++){
      Edge &edge = Graph[e.to][i]; 
      if(cost[edge.to] > edge.weight){
	cost[edge.to] = edge.weight;
	pq.push(edge);
      }
    }
  }
  cout<<ans<<endl;
  return 0;
}