Go: package main

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

//util
var sc = bufio.NewScanner(os.Stdin)

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

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func printList(list []int) {
	n := len(list)
	buf := bufio.NewWriter(os.Stdout)
	for _, v := range list[0 : n-1] {
		buf.Write([]byte(fmt.Sprintf("%d ", v)))
	}
	buf.Write([]byte(fmt.Sprintf("%d\n", list[n-1])))
	buf.Flush()
}

func max(a, b int) int {
	if a > b {
		return a
	} else {
		return b
	}
}

func min(a, b int) int {
	if a > b {
		return b
	} else {
		return a
	}
}

//solve
const (
	MAX   = 100
	INFTY = (1 << 21)
	WHITE = 0
	GRAY  = 1
	BLACK = 2
)

//V: グラフの頂点全体の集合
//T: MSTに属する頂点の集合
var (
	n     int
	M     [MAX][MAX]int //重みを記録する隣接行列
	color [MAX]int
	p     [MAX]int //MSTにおける頂点の親を記録
	d     [MAX]int //Tに属する頂点と、V−Tに属する頂点を繋ぐ辺の中で、重みが最小のものを記録
)

func prim() int {
	var u, minv int
	//初期化
	for i := 0; i < n; i++ {
		d[i] = INFTY
		p[i] = -1
		color[i] = WHITE
	}

	d[0] = 0

	for {
		//頂点uを決めていく
		//つまり、Tに属する頂点と、V−Tに属する頂点を繋ぐ辺の中で、重みが最小のものを決める
		minv = INFTY
		u = -1
		for i := 0; i < n; i++ {
			//未訪問かつ、辺の重みが最小となるuを選ぶ
			if minv > d[i] && color[i] != BLACK {
				u = i
				minv = d[i]
			}
		}

		if u == -1 {
			break
		}

		color[u] = BLACK
		for v := 0; v < n; v++ {
			//未訪問かつ、辺が存在するとき
			if color[v] != BLACK && M[u][v] != INFTY {
				if d[v] > M[u][v] {
					d[v] = M[u][v]
					p[v] = u
					color[v] = GRAY
				}
			}
		}
	}

	sum := 0
	for i := 0; i < n; i++ {
		if p[i] != -1 {
			sum += M[i][p[i]]
		}
	}
	return sum
}

func main() {
	sc.Split(bufio.ScanWords)
	n = nextInt()

	for i := 0; i < n; i++ {

		for j := 0; j < n; j++ {
			tmp := nextInt()
			if tmp == -1 {
				tmp = INFTY
			}
			M[i][j] = tmp
		}

	}

	fmt.Println(prim())

}

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;
}

C++: #include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <cmath>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <stdlib.h>
#include <stdio.h>
#include <bitset>
#include <cstring>
#include <deque>
#include <iomanip>
#include <limits>
#include <fstream>
using namespace std;
#define FOR(I,A,B) for(int I = (A); I < (B); ++I)
#define CLR(mat) memset(mat, 0, sizeof(mat))
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> PP;
// union - find tree !!!!!!!!!!!!!!!!!!!!!!!!!
vector<int> par; //oya
vector<int> rnk; //ki no hu ka sa
// n要素で初期化
void init(int n){
    par.resize(n);rnk.resize(n);
    FOR(i, 0, n){par[i] = i;rnk[i] = 0;}
}
//木の根を求める
int find(int x){
    if(par[x] == x) return x;
    else return par[x] = find(par[x]);
}
//xとyの属する集合を併合
void unite(int x, int y){
    x = find(x);y = find(y);
    if(x == y) return;
    if(rnk[x] < rnk[y])par[x] = y;
    else {
      par[y] = x;
      if(rnk[x] == rnk[y]) rnk[x]++;
    }
    return;
}
bool isSame(int x, int y){return find(x) == find(y);}
int main()
{
  ios::sync_with_stdio(false);
  cin.tie(0);
  priority_queue<PP, vector<PP>, greater<PP> > que;
  int n;
  cin >> n;
  init(n+1);
  FOR(i,1,n+1){
    FOR(j,1,n+1){
      int cost;
      cin >> cost;
      if(cost == -1) cost = 1e9;
      que.push(PP(cost, P(i, j)));
    }
  }
  int ans = 0;
  while(!que.empty()){
    PP pp = que.top();
    que.pop();
    P p = pp.second;
    if(!isSame(p.first, p.second)){
      ans += pp.first;
      unite(p.first, p.second);
    }
  }
  cout << ans << endl;
}

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

#define MAX 100
#define INFTY 1 << 21
#define WHITE 0
#define GRAY 1
#define BLACK 2

// 隣接行列
int gMatrix[MAX][MAX];

int prim(int n){
    // 頂点
    int u;
    // 最小の重み
    int minV;
    // Tに属する頂点とV - Tに属する頂点をつなぐ辺の中で、重みが最小の辺の重みを記録する
    int weight[MAX];
    // MSTにおける頂点vの親を記録する
    int parent[MAX];
    // 訪問状態を記録する
    int color[MAX];
    
    for(int i = 0;i < n;++i){
        weight[i] = INFTY;
        parent[i] = -1;
        color[i] = WHITE;
    }
    
    weight[0] = 0;
    
    while(1){
        minV = INFTY;
        u = -1;
        
        for(int i = 0;i < n;++i){
            if(minV > weight[i] && color[i] != BLACK){
                // 最小の重みを記録する
                u = i;
                minV = weight[i];
            }
        }
        // 全て訪問した
        if(u == -1)break;
        // 訪問済み
        color[u] = BLACK;
        for(int v = 0;v < n;++v){
            if(color[v] != BLACK && gMatrix[u][v] != INFTY){
                if(weight[v] > gMatrix[u][v]){
                    weight[v] = gMatrix[u][v];
                    parent[v] = u;
                    color[v] = GRAY;
                }
            }
        }
    }
    int sum = 0;
    for(int i = 0;i < n;++i){
        if(parent[i] != -1) sum += gMatrix[i][parent[i]];
    }
    
    return sum;
}

int main(){
    int n;
    cin >> n;
    
    for(int i = 0;i < n;++i){
        for(int j = 0;j < n;++j){
            int edge;
            cin >> edge;
            gMatrix[i][j] = (edge == -1) ? INFTY : edge;
        }
    }
    
    cout << prim(n) << endl;
    return 0;
}

C++: #include <iostream>

using namespace std;

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

void visit(){
    int ima = 0;
    int mine = 100000000;
    for (int k = 0; k < n-1; k++) {
        mine = 10000000;
        for (int i = 0; i < n; i++) {
            if (flg[i] == 0 && mine > t[i]) {
	            mine = t[i];
	            ima = i;
            }
        }
        sum += mine;
        flg[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++) {
        flg[i] = 0;
        t[i] = 100000000;
    }
    t[0] = 0;
    flg[0] = 1;
    for(int i = 0; i < n;i++){ 
        if(a[0][i] != -1){
            t[i] = a[0][i];
        }
    }
    visit();
    cout << sum << endl;
    return 0; 
}

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

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

void visit(){
  int ima = 0;
  int mine = 100000000;
  
  
  for(int k=0;k<n;k++) {
    
    for(int i=0;i<n;i++) {
      if(flg[i] == 0 && mine > t[i]) {
	mine = t[i];
	ima = i;
      }
    }
    sum += mine;
    flg[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++) {
    flg[i] = 0;
      t[i] = 100000000;
  }
  
  t[0] = 0;
  flg[0] = 1;
  for(int i=0;i<n;i++) 
    if(a[0][i] != -1) t[i] = a[0][i];

  visit();

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