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 <string>
#include <algorithm>
#include <functional>
#include <vector>
#include <utility>
#include <cstring>
#include <iomanip>
#include <numeric>
#include <cmath>
#include <queue>
#define MAX_N 110
using namespace std;
typedef long long ll;
const int INF = 1<<30; 
const int MOD = 1e9 + 7;
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};
int cost[MAX_N][MAX_N];
int mincost[MAX_N];
bool used[MAX_N];
int n;
int prim()
{
    int res = 0;
    mincost[0] = 0;
    while(true)
    {
        int v = -1;
        for(int u = 0; u < n; u++)
        {
            if(!used[u] && (v == -1 || mincost[u] < mincost[v])) v = u;
        }
        if(v == -1) break;
        used[v] = true;
        res += mincost[v];
        for(int u = 0; u < n; u++) mincost[u] = min(mincost[u], cost[v][u]);
    }
    return res;
}
int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    cin >> n;
    memset(used, false, sizeof(used));
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < n; j++)
        {
            cin >> cost[i][j];
            if(cost[i][j] == -1) cost[i][j] = INF;
        }
        mincost[i] = INF;
    }
    int ans = prim();
    cout << ans << endl;
    return 0;
}

C++: #include <iostream>
#include <queue>
#include <algorithm>
#include <vector>


using namespace std;


using ll = long long;


const ll MAX = 100;


ll adj[MAX][MAX];
ll d[MAX] = {};
bool finished[MAX] = {};
ll n;


void msp()
{
    priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> pq;
    pq.push(make_pair(0, 0));

    while (!pq.empty()) {
        auto p = pq.top(); pq.pop();
        if (finished[p.second])
            continue;

        for (ll j = 0; j < n; j++) {
            if (adj[p.second][j] == -1 || finished[j])
                continue;
            pq.push(make_pair(adj[p.second][j], j));
        }
        // cout << p.second << ": " << p.first << endl;
        d[p.second] = p.first;
        finished[p.second] = true;
    }
}


int main()
{
    cin >> n;
    for (ll i = 0; i < n; i++) {
        for (ll j = 0;j < n; j++) {
            cin >> adj[i][j];
        }
    }

    msp();

    ll acc = 0;
    for (ll i = 0; i < n; i++)
        acc += d[i];
    cout << acc << endl;
}

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

const int inf = 100000;
const int NIL = -1;

int G[100][100], d[100];
bool color[100];

int main(){
    int n, minc, tag, ans = 0;
    cin >> n;
    for(int i = 0; i < n; i++){
        d[i] = inf;
        color[i] = false;
        for(int j = 0; j < n; j++) cin >> G[i][j];
    }
    d[0] = 0;
    while(true){
        minc = inf;
        for(int i = 0; i < n; i++){
            if(color[i] == false && d[i] < minc){
                minc = d[i];
                tag = i;
            }
        }
        if(minc == inf) break;
        ans += minc;
        color[tag] = true;
        for(int v = 0; v < n; v++){
            if(G[tag][v] != -1){
                if(color[v] == false && G[tag][v] < d[v]){
                    d[v] = G[tag][v];
                }
            }
        }
    }
    cout << ans << endl;
    return 0;
}

C++: #include <bits/stdc++.h>

#define NIL -2
#define INF 1000000001

using namespace std;

void prim(const vector<vector<int> > &link_weight,vector<int> &color, vector<int> &mincost_array,vector<int> &parents){
    int n = mincost_array.size();
    int mincost;
    int u;
    while(true){
        mincost = INF;
        for(int i = 0;i < n;i++){
            if(color[i] != 1 && mincost_array[i] < mincost){
                mincost = mincost_array[i];
                u = i;      
            }
        }
        if(mincost == INF) break;
        color[u] = 1;

        for(int v = 0;v < n;v++){
            if(color[v] != 1 && link_weight[u][v] < INF){
                if(link_weight[u][v] < mincost_array[v]){
                    mincost_array[v] = link_weight[u][v];
                    parents[v] = u;
                    color[v] = 0;
                }
            }
        }
    }
}

int main(){
    int mat_size;
    int temp;
    int cost = 0;

    scanf("%d",&mat_size);
    vector<vector<int> > link_weight(mat_size,vector<int>(mat_size,INF));
    vector<int> color(mat_size,-1);
    vector<int> mincost_array(mat_size,INF);
    vector<int> parents(mat_size,-1);

    for(int i = 0;i < mat_size;i++){
        for(int j = 0;j < mat_size;j++){
            scanf("%d",&temp);
            if(temp != -1) link_weight[i][j] = temp;
        }
    }
    mincost_array[0] = 0;
    prim(link_weight,color,mincost_array,parents);
    for(int i = 0;i < mat_size;i++) cost += mincost_array[i];
    printf("%d\n",cost);

    return 0;   
}