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<climits>
using namespace std;

int main(){
  vector<vector<int> > graph;
  vector<int> master_node;
  vector<int> distance;
  vector<int> to_neighbor;
  int n;

  cin >> n;
  n++;
  graph.resize(n);
  master_node.resize(n);
  distance.resize(n);
  to_neighbor.resize(n);
  for(int i=1;i<n;i++){
    graph[i].resize(n);
    master_node[i]=0;
    distance[i]=INT_MAX;
    to_neighbor[i]=INT_MAX;
  }

  for(int i=1;i<n;i++){
    for(int j=1;j<n;j++){
      int input;

      cin >> input;
      graph[i][j]=(input!=-1?input:INT_MAX);
    }
  }


  int from=1;

  master_node[1]=1;
  distance[1]=0;
  for(int i=1;i<n;i++){
    to_neighbor[i]=(graph[1][i]<to_neighbor[i]?graph[1][i]:to_neighbor[i]);
  }
  while(true){
    int min_distance=INT_MAX,to;

    for(int i=1;i<n;i++){
      if((to_neighbor[i]<min_distance)&&(master_node[i]==0)){
        min_distance=to_neighbor[i];
	to=i;
      }
    }
    if(min_distance==INT_MAX){
      break;
    }else{
      master_node[to]=from;
      distance[to]=min_distance;
      from=to;
      to_neighbor[from]=INT_MAX;
      for(int i=1;i<n;i++){
        to_neighbor[i]=(graph[from][i]<to_neighbor[i]?graph[from][i]:to_neighbor[i]);
      }
    }
  }


  int sum=0;
  
  for(int i=1;i<n;i++){
    sum+=distance[i];
  }
  cout << sum << endl;
}

C++: #include<iostream>
#include<iomanip>
#include<algorithm>
#include<bitset>
#include<cctype>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<functional>
#include<limits>
#include<list>
#include<map>
#include<set>
#include<stack>
#include<string>
#include<sstream>
#include<queue>
#include<vector>
using namespace std;

#define MOD 1000000007
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL

#define MAX 100
#define WHITE 0
#define GRAY 1
#define BLACK 2

int n, M[MAX][MAX];

int prim() {
	int d[MAX],
		p[MAX] = { -1 },
		color[MAX] = {};//WHITE
	fill(d, d + MAX, INF);
	d[0] = 0;

	while (true) {
		int u, minv = INF;
		for (int i = 0; i < n; i++) {
			if (color[i] != BLACK&&d[i] < minv) {
				minv = d[i];
				u = i;
			}
		}

		if (minv == INF) { break; }

		color[u] = BLACK;

		for (int v = 0; v < n; v++) {
			if (color[v] != BLACK&&M[u][v] != INF) {
				if (M[u][v] < d[v]) {
					d[v] = M[u][v];
					p[v] = u;
					color[v] = GRAY;
				}
			}
		}
	}
	int sum = 0;
	for (int i = 0; i < n; i++) {
		if (p[i] != -1)sum += M[i][p[i]];
	}
	return sum;
}

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

	cout << prim() << endl;

	return 0;
}

C++: #include <iostream>
#include <climits>
#include <queue>

using namespace std;
static const int MAX=100;
static const int WHITE=1;
static const int GRAY=2;
static const int BLACK=3;

int n,M[MAX+10][MAX+10];

int prim()
{
    int u,minv;
    int d[MAX],p[MAX],color[MAX];

    for(int i=0;i<n;i++)
    {
      d[i]=INT_MAX;
      p[i]=-1;
      color[i]=BLACK;
    }

    d[0]=0;

    while(1)
    {
        minv=INT_MAX;
        u=-1;
        for(int i=0;i<n;i++)
        {
            if(minv>d[i]&&color[i]!=BLACK)
            {
                u=i;
                minv=d[i];
            }
        }
      if(u==-1) break;
      color[u]=BLACK;
      for(int v=0;v<n;v++)
      {
        if(color[v]!=BLACK && M[u][v]!=INT_MAX)
        {
            if(d[v]>M[u][v])
            {
                d[v]=M[u][v];
                p[v]=u;
                color[v]=GRAY;
            }
         }
      }
     }

    int sum=0;
    for(int i=0;i<n;i++)
      for(int j=0;i<n;j++)
    {
        if(p[i]!=-1) sum+=M[i][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)? INT_MAX:e;
    }

    cout<<prim()<<endl;
    return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i, n) for(int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;

static const int INFTY = 1 << 21;
static const int MAX = 100;
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;

int n, M[MAX][MAX];

int prim() {
    int u, minv;

    int d[MAX], p[MAX], color[MAX];
    rep(i, n) {
        d[i] = INFTY;
        p[i] = -1;
        color[i] = WHITE;
    }
    d[0] = 0;
    while(1) {
        minv = INFTY;
        u = -1;

        // Tに加える頂点:rを選ぶ
        // 最初はd[0]=0以外はINFTYなので、d[0]が選ばれる
        // 探索を１度でもしている頂点の中から、最小の頂点を選ぶ
        // 探索が行われていない頂点は d[v] にINFTYが入っているため判別可能
        rep(i, n) {
            if(minv > d[i] && color[i] != BLACK) {
                u = i;
                minv = d[i];
            }
        }

        if(u == -1) {
            break;
        }
        color[u] = BLACK;

        // rの辺を調べる
        // ここでは、「コストの更新」を目的にしている　d[v]には頂点:vの辺の中から探索時の最小の値が入っている。
        // ここで計算した各辺のコストを使って、次のループで最小のものをrとして選ぶ
        rep(v, n) {
            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;
                }
            }
        }
    }
    int sum = 0;
    rep(i, n) {
        if(p[i] != -1)
            // 計算した最小の辺を選んで足していく作業
            // どの辺からその頂点にアクセスするのが一番コストが低いのかは
            // p が保持している。
            sum += M[i][p[i]];
    }
    return sum;
}
int main() {
    // 制約見て！！！デカかったらlong longにすること！
    cin >> n;
    rep(i, n) {
        rep(j, n) {
            scanf("%d", &M[i][j]);
            if(M[i][j] == -1) {
                M[i][j] = INFTY;
            }
        }
    }

    cout << prim() << endl;
}