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