Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

const BIG_COST = 3000

const (
	ST_NOT_YET = iota
	ST_JOINED
)

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	nNodes := scanInt(sc)
	costs := make([][]int, nNodes)

	for r := 0; r < nNodes; r++ {
		costs[r] = make([]int, nNodes)
		for c := 0; c < nNodes; c++ {
			costs[r][c] = scanInt(sc)
		}
	}

	states := make([]int, nNodes)
	minCosts := make([]int, nNodes)

	for i := 1; i < nNodes; i++ {
		minCosts[i] = BIG_COST
	}

	for {
		var u int
		minCost := BIG_COST

		for i := 0; i < nNodes; i++ {
			if states[i] != ST_JOINED && minCosts[i] < minCost {
				minCost = minCosts[i]
				u = i
			}
		}

		// 全てのノードが MST に JOIN できたので終了。
		if minCost == BIG_COST {
			break
		}

		states[u] = ST_JOINED

		for v := 0; v < nNodes; v++ {
			cost := costs[u][v]

			// MST内の u と MST外の v との辺の重みをそれぞれ minCosts[v] として
			// 保存しておく。このうち辺の重みが最小となる v が次のループで MST に JOIN する。
			if states[v] != ST_JOINED && cost != -1 && cost < minCosts[v] {
				minCosts[v] = cost
			}
		}
	}

	sum := 0
	for _, cost := range minCosts {
		sum += cost
	}
	fmt.Println(sum)
}

func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	v, _ := strconv.Atoi(sc.Text())
	return v
}

C++: #include<cstdio>
static const int kMaxVertices = 100;
static const int kInfinity = 3000;
static int Adjacency_matrix[kMaxVertices][kMaxVertices];

void Prim(int n){
	bool has_not_been_discovered[kMaxVertices];
	int lowest_cost_edge[kMaxVertices];
	int parent[kMaxVertices];
	for (int i = 0; i < n; ++i)	{
		has_not_been_discovered[i] = true;
		lowest_cost_edge[i] = kInfinity;
	}
	lowest_cost_edge[0] = 0;
	parent[0] = -1;

	while(true) {
		int mincost = kInfinity;
		int vertex_to_add;
		for (int i = 0; i < n; ++i)	{
			if(has_not_been_discovered[i] && lowest_cost_edge[i] < mincost){
				mincost = lowest_cost_edge[i];
				vertex_to_add = i;
			}
		}

		if(mincost == kInfinity)
			break;

		has_not_been_discovered[vertex_to_add] = false;
		int vertex_added = vertex_to_add;

		for (int i = 0; i < n; ++i)	{
			if(has_not_been_discovered[i] && Adjacency_matrix[vertex_added][i] < lowest_cost_edge[i]) {
				lowest_cost_edge[i] = Adjacency_matrix[vertex_added][i];
				parent[i] = vertex_added;
			}
		}
	}
	int total_cost = 0;
	for (int i = 0; i < n; ++i)
		total_cost += lowest_cost_edge[i];
	printf("%d\n", total_cost);
}

int main() {
	int number_of_vertices;
	scanf("%d", &number_of_vertices);
	for (int vertex_from = 0; vertex_from < number_of_vertices; ++vertex_from) {
		for (int vertex_to = 0; vertex_to < number_of_vertices; ++vertex_to)	{
			int tmp;
			scanf("%d", &tmp);
			Adjacency_matrix[vertex_from][vertex_to] = (tmp < 0) ? kInfinity : tmp;
		}
	}
	Prim(number_of_vertices);
	return 0;
}

C++: #include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#define N 110
#define INF 0x3f3f3f3f
using namespace std;
typedef struct {
    int w;
    int s;
    int e;
}Edge;
Edge edge[N*N];
int m[N][N];
int father[N];
int n;
int k = 0;  //edge的下标
int result = 0;
int findfather(int x){
    return x == father[x] ? x : findfather(father[x]);
}
int cmp(Edge a, Edge b){
    return a.w < b.w;
}
void Kruscal(){
    for(int u = 0; u < k; u++){
        int s = edge[u].s;
        int e = edge[u].e;
        if(findfather(s) != findfather(e)){
            father[findfather(e)] = s;
            result += edge[u].w;
        }
    }
    printf("%d\n", result);
}
int main(){

    scanf("%d", &n);
    for(int i = 1; i <= n; i++) father[i] = i;
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= n; j++){
            int b;
            scanf("%d", &b);
            m[i][j] = b;
        }
    }
    for(int i = 1; i <= n; i++){
        for(int j = i; j <= n ;j++){
            if(m[i][j] != -1) {
                edge[k].w = m[i][j];
                edge[k].s = i;
                edge[k].e = j;
                k++;
            }
        }
    }
    sort(edge, edge+k, cmp);
    Kruscal();
    return 0;
}

C++: #include <iostream>
#include <utility>
#include <algorithm>
using namespace std;
const int MAX = 10005;
int P[MAX];
pair<int, pair<int,int> > C[MAX]; //<重み,<始点,終点>>

void init()
{
	for (int i = 0; i < MAX; ++i)
	{
		P[i] = i;
	}
}

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

bool same(int a, int b)
{
	if (root(a) == root(b))
	{
		return true;
	}
	else
	{
		return false;
	}
}

void unite(int a, int b)
{
	P[root(b)] = root(a); 
}

int main()
{
	int n;
	cin >> n;
	int cnt = 0;
	int temp;
	for (int i = 0; i < n; ++i)
	{
		for (int j = 0; j < n; ++j)
		{
			cin >> temp;
			if (temp != -1)
			{
				C[cnt].first = temp;
				C[cnt].second.first = i;
				C[cnt].second.second = j;
				cnt++;
			}
		}
	}

	init();
	int ans = 0;
	sort(C,C+cnt);
	for (int i = 0; i < cnt; ++i)
	{
		if (same(C[i].second.first,C[i].second.second) == false)
		{
			ans += C[i].first;
			unite(C[i].second.first,C[i].second.second);
		}
	}

	cout << ans << endl;
	return 0;
}

C++: 
#include<stdio.h>

#define MAX 100
#define INFINITY 10000

int n;
int M[MAX][MAX], color[MAX], d[MAX], p[0];

enum {
    WHITE,
    GRAY,
    BLACK
};

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

    while (true) {
        int min = INFINITY;
        int target = -1;
        for (int i = 0; i < n; ++i) {
            if (d[i] < min && color[i] != BLACK) {
                min = d[i];
                target = i;
            }
        }
        if (target == -1) break;

        color[target] = BLACK;

        for (int v = 0; v < n; ++v) {
            if (color[v] != BLACK && M[target][v] > 0) {
                if(M[target][v] < d[v]) {
                    d[v] = M[target][v];
                    p[v] = target;
                    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() {
    scanf("%d", &n);
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            scanf("%d", &M[i][j]);
        }
    }
    printf("%d\n", prim());
    return 0;
}

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

struct edge{ int u,v,cost;};
vector<edge> E;
int n;

struct UnionFind {
  vector< int > data;
 
  UnionFind(int sz) {
    data.assign(sz, -1);
  }
 
  bool unite(int x, int y) {
    x = find(x), y = find(y);
    if(x == y) return (false);
    if(data[x] > data[y]) swap(x, y);
    data[x] += data[y];
    data[y] = x;
    return (true);
  }
 
  int find(int k) {
    if(data[k] < 0) return (k);
    return (data[k] = find(data[k]));
  }
 
  int size(int k) {
    return (-data[find(k)]);
  }
};


bool comp(const edge& e, edge& f){
    return e.cost < f.cost;
}


int main(){
    cin >> n;
    rep(i,n){
        rep(j,n){
            int c ; cin >> c;
            if(c!=-1){
                edge e = {i,j,c};
                E.push_back(e);
            }
        }
    }
    UnionFind T(n);
    sort(E.begin(),E.end(),comp);
    ll res = 0;
    rep(i,E.size()){
        edge e = E[i];
        if(T.find(e.u)!=T.find(e.v)) {
            T.unite(e.u,e.v);
            res+=e.cost;
        }
    }
    cout << res << endl;
}