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>
 using namespace std;
 static const int WHITE = 0;
 static const int GRAY = 1;
 static const int BLACK =2;
 static const int MAX = 100;
 static const int INFTY = (1<<21);
 
 int n,M[MAX][MAX];
 
 int prim(){
 	int u,miv;
 	int d[MAX],p[MAX],color[MAX];
 	
 	for(int i=0;i<n;i++){
 		d[i] = INFTY;
 		p[i] = -1;
 		color[i] = WHITE;
 	}
 	
 	d[0]=0;
 	
 	while(1){
 		u=-1;
 		miv = INFTY;
 		for(int i=0;i<n;i++){
 			if(color[i]!=BLACK&&d[i]<miv){
 				miv = d[i];
 				u=i;
 			}
 		}
 		if(miv==INFTY) break;
 		
 		color[u] = BLACK;
 		
 		for(int i=0;i<n;i++){
 			if(color[i]!=BLACK&&M[u][i]!=INFTY){
 				if(d[i]>M[u][i]){
 					d[i] = M[u][i];
 					p[i] = u;
 					color[i] = GRAY;
 				}
 			}
 		}
 	}
 	int sum=0;
 	for(int i=0;i<n;i++){
 		if(p[i]!=-1)
 		sum+=p[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;
 			M[i][j]=(e==-1)?INFTY:e;
 		}
 	}
 	cout<<prim()<<endl;
 	return 0;
 }

C++: #include<stdio.h>
#include<limits.h>
#define NIL -1
static const int MAX = 500;
static const int INFTY = (1<<21);
main(){
  int n, i, j, e, v, sum, u , minV, A[MAX];
  /*your code*/
  int M[MAX][MAX], key[MAX], p[MAX];
  
  scanf("%d", &n);
  
  for ( i = 0; i< n; i++ ){
    for ( j = 0; j < n; j++ ){
      scanf("%d", &e);
      M[i][j] = (e==-1)?INFTY:e;
    }
  }
  /*
    your code
  */
  for(i = 0; i < n; i++){
    key[i] = INT_MAX;
    p[i] = NIL;
    A[i] = NIL;
  }
  
  key[0]=0;
  
  while(1){
    
    u = NIL;
    
    minV = INT_MAX;
    
    for(i = 0;  i < n; i++){
      
      if(key[i] < minV && A[i] == NIL){
	
	u = i;
	
	minV = key[i];
	
      }
      
    }  
    if(u == NIL)break;
    
    A[u] = u; 
    
    for(v = 0; v < n; v++){
      if(M[u][v] < key[v] && A[v] == NIL){
	key[v] = M[u][v];
	p[v] = u;
      }
    }
  }
  
  sum = 0;
  
  for ( i = 0; i < n; i++ ){
    if ( p[i] != -1 ) sum +=M[p[i]][i];
  }
  
  printf("%d\n", sum);
  
  
  return 0;
}

C++: #include <iostream>
#include <cstdio>
#define MAX_VERTEXES 100
#define NIL -1
#define ZERO 0
#define INFTY 2001//>2000
using namespace std;

//think easy make easy


int n;//num of vertexes(頂点)
int matrix_graph[MAX_VERTEXES+1+1][MAX_VERTEXES+1+1];//1オリジン
bool vertix_set[MAX_VERTEXES+1+1];
int mincst = 0;

void findMinEdge(){//次に加える辺を決定する関数
	int minval_tmp = INFTY;
	int next_vertex;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			if(vertix_set[i] == true && vertix_set[j] == false){
				if(matrix_graph[i][j] < minval_tmp && matrix_graph[i][j] != NIL){
					next_vertex = j;
					minval_tmp = matrix_graph[i][j];
				}
			}
		}
	}

	mincst += minval_tmp;
	vertix_set[next_vertex] = true;
}

int main(){
	scanf("%d",&n);


	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			matrix_graph[i][j] = ZERO;
		}
	}

	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			scanf("%d",&matrix_graph[i][j]);
		}
	}

	/*確認用
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			if(j<n) printf("%d ",matrix_graph[i][j]);
			else printf("%d\n",matrix_graph[i][j]);
		}
	}
	*/

	int minval_tmp = INFTY;
	int next_vertex1;
	int next_vertex2;

	for(int i=1;i<=n;i++){
		vertix_set[i] = false;
	}

	for(int i=1;i<=n;i++){//しつこい
		for(int j=1;j<=n;j++){
			if(matrix_graph[i][j] < minval_tmp && matrix_graph[i][j] != NIL){
				minval_tmp = matrix_graph[i][j];
				next_vertex1 = i;
				next_vertex2 = j;
			}
		}
	}
	vertix_set[next_vertex1] = true;
	vertix_set[next_vertex2] = true;
	mincst += minval_tmp;

	

	for(int i=1;i<n-1;i++){
		findMinEdge();
	}
	cout <<mincst <<endl;

	return 0;
}

C++: #include <iostream>
#include <vector>
#include <string>
#include <bitset>
#include <cmath>
#include <iomanip>
#include <stack>
#include <queue>
#include <algorithm>
#include <deque>
using namespace std;

#define INF 1e18
#define int long long

signed main() {
    int n; cin >> n;
    vector<vector<int> > adj(n, vector<int> (n));
    for(int i = 0; i < n; i++) {
        for(int j = 0; j < n; j++) {
            int a; cin >> a;
            adj[i][j] = (a == -1 ? INF : a);
        }
    }

    int u , minv;
    int unvisited = 0; int visited = 1; int fixed = 2;
    vector<int> dist(n, INF), parent(n, -1), status(n, unvisited);

    dist[0] = 0;

    while(1) {
        u = -1;
        minv = INF;
        for(int i = 0; i < n; i++) {
            if(minv > dist[i] && status[i] != fixed) {
                u = i;
                minv = dist[i];
            }
        }
        if(u == -1) break;
        status[u] = fixed;
        for(int v = 0; v < n; v++) {
            if(status[v] != fixed && adj[u][v] != INF) {
                if(dist[v] > adj[u][v]) {
                    dist[v] = adj[u][v];
                    parent[v] = u;
                    status[v] = visited;
                }
            }
        }
    }
    int sum = 0;
    for(int i = 0; i < n; i++) {
        sum += dist[i];
    }

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

C++: #include <iostream>
#include <limits>
#define N 110

int main(){
  int n;
  std::cin >> n;
 
  int A[N][N];
  int d[N] = {};
  int p[N];
  for(int i=0; i<n; i++){
    d[i] = std::numeric_limits<int>::max();
    p[i] = -1;
    for(int j=0; j<n; j++){
      std::cin >> A[i][j];
      if( A[i][j] == -1){ //no line
        A[i][j] = std::numeric_limits<int>::max();
      }
    }
  }
  d[0] = 0;

  int used[N] = {};
  while(1){
    int u = -1;
    int min = std::numeric_limits<int>::max();
    for(int i=0; i<n; i++){
      if( used[i] != -1 && d[i]<min ){
        min = d[i];
        u = i;
      }
    }
    if(min==std::numeric_limits<int>::max()) break;
    used[u] = -1;

    for(int i=0; i<n; i++){
      if( used[i] != -1 && A[u][i] < d[i]){
        p[i] = u;
        d[i] = A[u][i];
        used[i] = 1;
      }
    }
  }
  int MST=0;
  for(int i=0; i<n; i++){
    if(p[i] != -1) MST += A[i][p[i]];
      }

  std::cout << MST << std::endl;

  return 0;
}