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>
//cout<<setprecision(1)<<fixed<<endl;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int MAX_N=100000;
#define eps 1e-7
//#define Kongxiangzhouye

static const int MAX=100;
static const int INF=(1<<21);
static const int WHITE=0;
static const int GRAY=1;
static const int BLACK=2;

int begin[10000];
int to[10000];
int next[10000];
int weight[10000];
int e,n;
void add(int x,int y,int z){
	to[++e]=y;
	next[e]=begin[x];
	begin[x]=e;
	weight[e]=z;
}

struct p{
	int edge;
	int weight;
	bool operator < (const p& u)const{
		return weight>u.weight;
	}
};
int createtree(){
	priority_queue<p> q;
	int flag[100];
	memset(flag,0,sizeof(flag));
	int te=begin[1];
	while(to[te]!=-1){
		if(flag[to[te]]==0)
			q.push((p){te,weight[te]});
		te=next[te];
	}
	flag[1]=1;
	int ans=0;
	while(1){
		int flag2=1;
		for(int i=1;i<=n;i++){
			if(flag[i]==0){
				flag2=0;
				break;
			}
		}
		if(flag2==1)
			break;
		while(flag[to[q.top().edge]]==1)
			q.pop();
		p now=q.top();
		q.pop();
		ans=ans+now.weight;
		flag[to[now.edge]]=1;
		int te=begin[to[now.edge]];
		while(te!=-1){
			if(flag[to[te]]==0)
				q.push((p){te,weight[te]});
			te=next[te];
		}
	}
	return ans;
}
int main(){
	ios::sync_with_stdio(false);
	#ifdef Kongxiangzhouye
		freopen("in.txt","r",stdin);
		freopen("out.txt","w",stdout);
	#endif
	while(cin>>n){
		memset(begin,-1,sizeof(begin));
		memset(next,-1,sizeof(next));
		memset(to,-1,sizeof(to));
		e=0;
		for(int i=1;i<=n;i++){
			for(int j=1;j<=n;j++){
				int te;
				cin>>te;
				if(te!=-1)
					add(i,j,te);
			}
		}
		cout<<createtree()<<endl;
	}

	return 0;
}

C++: #include <iostream>
#include <iomanip>
#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <sstream>
#include <string>
#include <utility>
#include <map>
#include <memory>
#include <set>
#include <vector>
#include <deque>
#include <list>
#include <stack>
#include <queue>
// #include <array>

using namespace std;

class MSTree
{
public:
  int solve();
};

int MSTree::solve()
{
  int n;
  cin >> n;
  
  vector<vector<int>> adj(n, vector<int>(n));
  list<int> t, vt;

  for ( auto i = 0; i < n; ++i )
    for ( auto j = 0; j < n; ++j )
      cin >> adj[i][j];

  t.push_back( 0 );

  for ( auto i = 1; i < n; ++i )
    vt.push_back( i );

  int w = 0;

  while ( vt.size() )
    {
      int mw = 4000;
      list<int>::iterator itm;
      
      for ( auto ita = t.begin(); ita != t.end(); ++ita )
        for ( auto itb = vt.begin(); itb != vt.end(); ++itb )
          {
            if ( adj[*ita][*itb] >= 0 && mw > adj[*ita][*itb] )
              {
                itm = itb;
                mw = adj[*ita][*itb];
              }
          }
      
      w += mw;
      t.push_back( *itm );
      vt.erase( itm );
    }

  cout << w << endl;
    
  return 0;
}

int main()
{
  MSTree m;

  m.solve();

  return 0;
}

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

using namespace std;



int main() {
	int n,tmp;
	cin >> n;
	pair<int, pair<int, int>>* sorted_weight = new pair<int, pair<int, int>>[n * n];
	int* equiv = new int[n];


	int cnt_equiv = 0;
	int cnt_edge = 0;
	int cost = 0;
	
	for (int i = 0;i < n;i++) {
		for (int j = 0;j < n;j++) {
			cin >> tmp;
			sorted_weight[i * n + j] = make_pair(tmp, make_pair(i, j));
		}
		equiv[i] = 0;
	}
	sort(sorted_weight, sorted_weight + n * n);
	for (int i = upper_bound(sorted_weight, sorted_weight + n * n, make_pair(-1, make_pair(n, n))) - sorted_weight;cnt_edge<n-1;i++) {
		if (equiv[sorted_weight[i].second.second] == 0 || equiv[sorted_weight[i].second.first] == 0) {
			cnt_edge++;
			cost += sorted_weight[i].first;
			if (equiv[sorted_weight[i].second.second] == 0 && equiv[sorted_weight[i].second.first] == 0) {
				cnt_equiv++;
				equiv[sorted_weight[i].second.second] = equiv[sorted_weight[i].second.first] = cnt_equiv;
			}
			else if (equiv[sorted_weight[i].second.second] == 0) {
				equiv[sorted_weight[i].second.second] = equiv[sorted_weight[i].second.first];
			}
			else {
				equiv[sorted_weight[i].second.first] = equiv[sorted_weight[i].second.second];
			}
		}
		else if (equiv[sorted_weight[i].second.second] != equiv[sorted_weight[i].second.first]) {
			cnt_edge++;
			cost += sorted_weight[i].first;
			tmp = equiv[sorted_weight[i].second.second];
			for (int j = 0;j < n;j++) {
				if (equiv[j] == tmp)equiv[j] = equiv[sorted_weight[i].second.first];
			}
		}
	}
	
	cout << cost << endl;
}

C++: #include <stdio.h>
#define INF 100000000
#define BLACK 0
#define WHITE 1
#define N 101
 
int n,a[N][N];

int prim(void)
{
  int u,v,i,mincost,d[N],pi[N],total = 0,color[N];
 
  for(u = 1;u <= n;u++){
    d[u] = INF;
    pi[u] = -1;
    color[u] = WHITE;
  }
 
  d[1] = 0;
     
  while(1){
    mincost = INF;
    for(i = 1;i <= n;i++){
      if(color[i] != BLACK && d[i] < mincost){
	mincost = d[i];
	u = i;
      }
    }
     
    if(mincost == INF) break;
    
    color[u] = BLACK;
     
    for(v = 1;v <= n;v++){
      if(color[v] != BLACK && a[u][v] < d[v]){
	pi[v] = u;
	d[v] = a[u][v];
      }
    }
    color[v] = BLACK;
  }
 
  for(i = 1;i <= n; i++){
    if(pi[i] != -1) total += a[i][pi[i]];
  }
 
  return total;
}

 
int main()
{
  int i,j;
 
  scanf("%d",&n);
 
  for(i = 1;i <= n;i++){
    for(j = 1;j <= n;j++){
      scanf("%d",&a[i][j]);
      if(a[i][j] == -1) a[i][j] = INF;
    }
  }
 
   printf("%d\n",prim());
    
   return 0;
 
}