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 <algorithm>

using namespace std;
struct edge {
    edge() {};
    edge(int s, int e, int d) : start {s}, end {e}, d {d} {};
    bool operator<(edge y){
        return d < y.d || start < y.start || end < y.end;
    };
    bool operator>(edge y){
        return d > y.d || start > y.start || end > y.end;
    };
    bool operator==(edge y){
        return d == y.d || start == y.start || end || y.end;
    };
    int start;
    int end;
    int d;
};

void dfs(vector<vector<int>> graph, bool* used, int id, int n){
    used[id] = true;
    for(int i = 0; i < n; ++i){
        if(graph[id][i] != -1 || used[i]){
            continue;
        }
        dfs(graph, used, i, n);
    }
}

bool is_connected(vector<vector<int>>& graph){
    int n = graph.size();
    bool used[n];
    for(int i = 0; i < n; ++i){
        used[i] = false;
    }
    dfs(graph, used, 0, n);
    for(int i = 0; i < n; ++i){
        if(!used[i]){
            return false;
        }
    }
    return true;
}

void delete_edges(vector<vector<int>>& graph){
    int edges_num = 0;
    vector<edge> vec {};
    int n = graph.size();
    for(int i = 0; i < n; ++i){
        for(int j = i+1; j < n; ++j){
            if(graph[i][j] != -1){
                ++edges_num;
                vec.push_back(edge(i, j, graph[i][j]));
            }
        }
    }
    sort(vec.begin(), vec.end(), [](edge x, edge y){
            return y.d < x.d;
         });

    for(auto it = vec.begin(); it != vec.end(); ++it){
        edge e = *it;
        graph[e.start][e.end] = -1;
        graph[e.end][e.start] = -1;
        if(is_connected(graph)){
            --edges_num;
            if(edges_num == n-1){
                return;
            }
        } else {
            graph[e.start][e.end] = e.d;
            graph[e.end][e.start] = e.d;
        }
    }
}

vector<vector<int>> gen_spanning_tree(vector<vector<int>>& graph){
    vector<vector<int>> tree {graph};
    delete_edges(tree);
    return tree;

}

int calc_weight(vector<vector<int>> graph){
    int sum = 0;
    int n = graph.size();
    for(int i = 0; i < n; ++i){
        for(int j = i+1; j < n; ++j){
            if(graph[i][j] == -1){
                continue;
            }
            sum += graph[i][j];
        }
    }
    return sum;
}

int main(){
    int n;
    cin >> n;
    vector<vector<int>> graph {n, vector<int>(n, 0)};
    for(int i = 0; i < n; ++i){
        for(int j = 0; j < n; ++j){
            cin >> graph[i][j];
        }
    }
    auto tree = gen_spanning_tree(graph);
    cout << calc_weight(tree) << endl;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define MAX 100
#define INF 1<<25
int n,cost[MAX][MAX];
int mincost[MAX];
bool used[MAX];
int prim(){
  for(int i=0;i<n;i++)mincost[i]=INF,used[i]=false;
    mincost[0]=0;
  int res=0;
  while(1){
    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]=1;
    res+=mincost[v];
    for(int u=0;u<n;u++)mincost[u]=min(mincost[u],cost[v][u]);
  }
return res;
}
int main(){
  cin>>n;
  for(int i=0;i<n;i++){
    for(int j=0;j<n;j++){
      int e;
      cin>>e;
      cost[i][j]=(e==-1)?INF:e;
    }
  }
  cout<<prim()<<endl;
}

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

using namespace std;

// int search_xyz(int e);
int tree[100];

int root(int a);
bool is_same_set(int p, int q);
void joint(int x, int y);

int main()
{
    int n, tempa, k = 0;
    cin >> n;
    int e = n * (n - 1) / 2;
    pair<int, pair<int, int>

         >
        a[e];
    for (int i = 0; i < n; i++)
    {
        tree[i] = i;
        for (int j = 0; j < n; j++)
        {
            cin >> tempa;
            if (i < j)
            {
                a[k] = make_pair(tempa, make_pair(i, j));
                k++;
            }
        }
    }
    sort(a, a + e);
    int result = 0;
    for (int i = 0; i < e; i++)
    {
        if (a[i].first == -1)
            continue;

        if (is_same_set(a[i].second.first, a[i].second.second))
        {
            continue;
        }
        else
        {
            joint(a[i].second.first, a[i].second.second);
            result += a[i].first;
        }
    }
    cout << result << endl;
    return 0;
}

int root(int a)
{
    if (tree[a] == a)
    {
        return a;
    }
    else
    {
        tree[a] = root(tree[a]);
        return tree[a];
    }
}

bool is_same_set(int p, int q)
{
    return root(p) == root(q);
}

void joint(int x, int y)
{
    tree[root(x)] = root(y);
}