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<cstdio>
#include<vector>
#include<algorithm>
#include<utility>
#include<numeric>
#include<iostream>
#include<array>
#include<string>
#include<sstream>
#include<stack>
#include<queue>
#include<list>
#define _USE_MATH_DEFINES

#include<math.h>
#include<map>

#define SENTINEL 1000000001

#define min(a,b) (a)>(b)?(b):(a)
#define max(a,b) (a)>(b)?(a):(b)

using namespace std;

/*int find(int x) 
{
	if (x == parent[x])return x;
	else 
	{
		return parent[x] = find(parent[x]);
	}
}

bool isSame(int x, int y)
{
	return find(x) == find(y);
}

void unite(int x, int y) 
{
	x = find(x);
	y = find(y);

	if (x == y)return;

	if (Rank[x] < Rank[y]) 
	{
		parent[x] = y;
	}
	else
	{
		parent[y] = x;
		if (Rank[x] == Rank[y])Rank[x]++;
	}
}*/

struct UnionFind
{
	vector<int> parent;
	vector<int> rank;

	UnionFind(int n) :parent(n), rank(n, 0)
	{
		for (int i = 0; i < n; i++)
		{
			parent[i] = i;
		}
	}

	int find(int x)
	{
		if (x == parent[x])
		{
			return x;
		}
		else
		{
			return parent[x] = find(parent[x]);
		}
	}

	void unite(int x, int y)
	{
		x = find(x);
		y = find(y);

		if (x == y)
		{
			return;
		}

		if (rank[x] < rank[y])
		{
			parent[x] = y;
		}
		else
		{
			parent[y] = x;
			if (rank[x] == rank[y])
			{
				rank[x]++;
			}
		}
	}

	bool same(int x, int y)
	{
		return find(x) == find(y);
	}
};

struct Edge
{
	int v1, v2, cost;
	Edge(int v1, int v2, int cost) :v1(v1), v2(v2), cost(cost){}
	bool operator<(const Edge& e)const
	{
		return cost < e.cost;
	}
};

int main()
{
	int n;
	cin >> n;

	vector<Edge> e;
	UnionFind uf(n);

	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < n; j++)
		{
			int c;
			cin >> c;
			if (c != -1)
			{
				e.push_back(Edge(i, j, c));
			}
		}
	}

	sort(e.begin(), e.end());

	int cost = 0;
	for(auto& te:e)
	{
		if (!uf.same(te.v1, te.v2))
		{
			uf.unite(te.v1, te.v2);
			cost += te.cost;
		}
	}

	cout << cost << endl;

	return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <climits>


#define MAX 100


using namespace std;


int main()
{
    vector< vector<int> > adj(MAX + 1, vector<int>(MAX, INT_MAX));
    int n;
    cin >> n;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            int cost;
            cin >> cost;
            if (cost >= 0)
                adj[i][j] = cost;
        }
    }

    vector<int> parent(MAX);
    vector<int> in_tree(MAX, false);
    vector<int> min_dists(MAX, INT_MAX);
    int tree_count = 0;

    int acc = 0;
    min_dists[0] = 0;
    for (int tc = 0; tc < n; tc++) {
        int min_cost = INT_MAX;
        int min_v = -1;
        for (int i = 0; i < n; i++) {
            if (min_cost > min_dists[i] && !in_tree[i]) {
                min_cost = min_dists[i];
                min_v = i;
            }
        }

        acc += min_cost;
        in_tree[min_v] = true;

        for (int i = 0; i < n; i++) {
            if (min_dists[i] > adj[min_v][i] && !in_tree[i]) {
                min_dists[i] = adj[min_v][i];
                parent[i] = min_v;
            }
        }
    }

    cout << acc << endl;

    return 0;
}

C++: #include <iostream>
using namespace std;
#define INF 1000000

int main(){
  long n;
  long G[100][100],d[100];
  long minc,mi,i;
  bool flgs[i];
  long suma=0;
  
  cin >> n;
  for (i=0;i<n;i++){
    for (int j=0;j<n;j++){
      cin >> G[i][j];
      if (G[i][j]==-1) G[i][j]=INF;
    }
    d[i]=INF;
    flgs[i]=false;
  }

  d[0]=0;
  while (true){
    minc=INF;
    for (i=0;i<n;i++){
      if (!flgs[i]&&d[i]<minc){
	minc=d[i];
	mi=i;
      }
    }
    if (minc==INF) break;
    flgs[mi]=true;
    for (i=0;i<n;i++){
      if (G[mi][i]==INF)continue;
      if (!flgs[i]&&d[i]>G[mi][i]){
	d[i]=G[mi][i];
	//cout << d[i];
      }
    }
  }
  
  for (i=0;i<n;i++){
    suma+=d[i];
  }
  cout << suma<<endl;
  
  return 0;
}

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

#define ll long long int
#define P pair<ll,ll>

const ll INF = 10000000000;
struct edge{ll to,cost;};

int v;
vector<edge> g[100000];
ll d[100000];

int prim(){
    priority_queue<P,vector<P>,greater<P> > que;
    fill(d,d+v,INF);
    int res = 0;
    d[0] = 0;
    que.push(P(0,0));

    while(!que.empty()){
        P p = que.top(); que.pop();
        ll k = p.second;
        if(d[k] < p.first){
            continue;
        }
        res += d[k];
        for(int i=0; i < g[k].size(); i++){
            edge e = g[k][i];
            if(d[e.to] > /*d[k]*/ + e.cost){
                d[e.to] = /*d[k] */+ e.cost;
                que.push(P(d[e.to],e.to));
            }
        }
    }
    return res;
}

int main() {

    int s;

    cin >> v;

    for(int i=0; i<v; i++){
        for(int j=0;j<v;j++){
            cin >> s;
            if(s!=-1){
                edge ed;
                ed.to = j;
                ed.cost = s;
                g[i].push_back(ed);
            }
        }
    }

    /*for(int i=0; i<v; i++){
        for(int j=0;j<g[i].size();j++){
            cout << g[i][j].to << "," << g[i][j].cost << " ";
        }
        cout << endl;
    }*/

    cout << prim() << endl;

    return 0;
}