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

#define INF 1000000
#define N_MAX 100
#define WH 0
#define GR 1
#define BK 2

int main() {
    int n;
    cin >> n;
    int A[N_MAX][N_MAX] = {};
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            int a;
            cin >> a;
            if (a == -1) {
                A[i][j] = INF;
            }
            else {
                A[i][j] = a;
            }
        }
    }

    int color[N_MAX] = {};
    int d[N_MAX];
    for (int i = 0; i < n; i++) {
        d[i] = INF;
    }
    int p[N_MAX] = {};

    while (true) {
        int u;
        int min_cost = INF;
        d[0] = 0;
        p[0] = -1;

        for (int i = 0; i < n; i++) {
            if (color[i] != BK && d[i] < min_cost) {
                min_cost = d[i];
                u = i;
            }
        }
        if (min_cost == INF) {
            break;
        }
        color[u] = BK;

        for (int v = 0; v < n; v++) {
            if (color[v] != BK && A[u][v] != INF) {
                if (A[u][v] < d[v]) {
                    d[v] = A[u][v];
                    p[v] = u;
                    color[v] = GR;
                }
            }
        }
    }

    int ans = 0;
    for (int i = 0; i < n; i++) {
        ans += d[i];
    }
    cout << ans << endl;

    return 0;
}

C++: #include <cstdio>
#include <iostream>
#include <queue>
#include <utility>

using namespace std;

const int NIL = 2001;
const int MAX = 100;
int G[MAX][MAX];
int W[MAX];
int n;

void MST() {
	priority_queue<pair<int, int>> PQ;
	PQ.push(make_pair(0, 0));
	W[0] = 0;
	while (!PQ.empty()) {
		int u = PQ.top().second;
		PQ.pop();
		for (int i = 0; i < n; ++i) {
			if (G[u][i] != -1 && G[u][i] < W[i]) {
				PQ.push(make_pair(G[u][i] * -1, i));
				W[i] = G[u][i];
			}
		}
	}
}

int main() {

	scanf("%d", &n);
	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < n; ++j)
			G[i][j] = NIL;
		W[i] = NIL;
	}

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

	MST();

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

	return 0;
}

C++: #include <bits/stdc++.h>
 
using namespace std;

int n;
const int INF = 100100100;

int a[105][105];

int u,minv;
int d[105],p[105],color[105];

int prim(){

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

  while(1){
    minv = INF;
    u = -1;
    for(int i = 0; i < n; i++){
      if(minv > d[i] && color[i] != 2){
        minv = d[i];
        u = i;
      }
    }
    if(u==-1) break;

    color[u] = 2;

    for(int i = 0; i < n; i++){
      if(color[i]!= 2 && a[u][i] != INF){
        if(d[i] > a[u][i]){
          d[i] = a[u][i];
          p[i] = u;
          color[i] = 1;
        }
      }
    }
  }
  int sum = 0;
  for(int i = 0; i < n; i++){
    if(p[i] != -1) sum+=a[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;
      a[i][j] = (e == -1) ? INF:e;
    }
  }
  cout << prim() << endl;
  return 0;
}