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 <bits/stdc++.h>
using namespace std;

const int INF = (1<<30)-1;

template<typename T>
T gcd(T a, T b)
{
	return b ? gcd(b, a%b) : a;
}

template<typename T>
T lcm(T a, T b)
{
	return a / gcd(a, b) * b;
}

//(a^e)%mod
template<typename T>
T power(T a, T e, T mod)
{
	if(e == 0)
	{
		return 1;
	}
	if(e % 2 != 0)
	{
		//a^e=a^(e-1)*a
		return (power(a, e - 1, mod) * a) % mod;
	}
	T res = power(a, e / 2, mod);
	//a^e=a^(e/2)*a^(e/2)
	return (res * res) % mod;
}

template<typename T>
class Tree
{
private:
public:
	T data;
	Tree* parent = NULL;
	std::vector<Tree*> children;
	void setData(T x)
	{
		data = x;
	}
	void setParent(Tree* t)
	{
		parent = t;
	}
	void addChild(Tree* t)
	{
		children.push_back(t);
	}
	Tree(){}
	~Tree(){}
};

template<typename T>
class Graph
{
public:
	T index;
	vector<T> edge;
	void setEdge(T t)
	{
		edge.push_back(t);
	}
	Graph(){}
	~Graph(){}
};

int prim(int size, int cost[101][101])
{
	const int indexed = 1;
	const int WHITE = 0;
	const int BLACK = 1;
	const int GRAY = 2;
	int colour[size + indexed], d[size + indexed], p[size + indexed];
	int u;
	for (int i = indexed; i < size + indexed; ++i)
	{
		colour[i] = WHITE;
		d[i] = INF;
	}
	d[indexed] = 0;
	p[indexed] = -1;

	priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> PQ;
	PQ.push(make_pair(0, indexed));

	while(!PQ.empty())
	{
		u = PQ.top().second;
		PQ.pop();
		colour[u] = BLACK;
		for (int i = indexed; i < size + indexed; ++i)
		{
			if(colour[i] != BLACK && cost[u][i] != INF)
			{
				if(cost[u][i] < d[i])
				{
					PQ.push(make_pair(cost[u][i], i));
					d[i] = cost[u][i];
					p[i] = u;
					colour[i] = GRAY;
				}
			}
		}
	}
	int sum = 0;
	for (int i = indexed; i < size + indexed; ++i)
	{
		if(p[i] != -1)
		{
			sum += cost[i][p[i]];
		}
		// cout << i << " " << p[i] << endl;
	}
	return sum;
}

int main()
{
	using namespace std;
	Graph<int> G[101];
	int costn[101][101];
	int n;
	cin >> n;
	for (int i = 1; i <= n; ++i)
	{
		for (int j = 1; j <= n; ++j)
		{
			int e;
			cin >> e;
			costn[i][j] = (e == -1 ? INF : e);
		}
	}
	cout << prim(n, costn) << endl;

	return 0;
}

C++: #if 0
????????????????§£????????????????????????????????????????????????AOJ????????????????????????????????????wrong???answer?????????????????????
????????§?????????????????\????????????????????£??????????????§??????????????????????????????????????¨???????????????
???????????????????????? Union Find Tree ???????????????????????????????????????????????????????????????????????????Root?????¢?????°??????????????¨??§??????
??????????????§???????????????????????????????????????????????¢??°???????????¢??°?????????????????£??????????????°???????????°???????\???????????????¨???????????¨???
????????????????????????
Minimum Spanning Tree ???????????§???????????¶??????????????????????????¨????????????????????¨?????????????????????????????????????????????????????£?????????
???????????????????????????????????????
#endif

#include <iostream>
#include<algorithm>
using namespace std;


struct edge
{
   int cost;
   int src;
   int dst;
};

edge e[5001];
int A[101][101];
int M=0;
int P[101];

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

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

bool is_same_set(int a, int b)
{
  return root(a) == root(b);
}

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

void checkEdges(int n)
{
    for(int r=0;r<n;r++)
    {
        for(int c=r+1;c<n;c++)
            {
                if(A[r][c]>-1)
                    {
                        e[M].cost=A[r][c];
                        e[M].src=r;
                        e[M].dst=c;
                        M+=1;
                    }
            }
    }
}

bool cmp(edge e1,edge e2)
{
    return e1.cost<=e2.cost;
}
int main()
{
    int n,count=0;
    cin>>n;
    init(n);
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++){
                cin>>A[i][j];
        }
    }
    checkEdges(n);
    sort(e,e+M,cmp);
    for (int i=0; i<M; ++i)
    {
        if(is_same_set(e[i].src,e[i].dst))
        {
            continue;
        }
        unite(e[i].dst,e[i].src);
        count+=e[i].cost;
    }
    cout<<count<<endl;
    return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>

using namespace std;

#define MAX_N 100

int G[MAX_N][MAX_N];

int mincost[MAX_N];
bool used[MAX_N];
int V;
int prim() {
	for (int i = 0; i < V; i++) {
		mincost[i] = INT_MAX;
		used[i] = false;
	}
	mincost[0] = 0;
	int res = 0;

	while (1) {
		int v = -1;
		for (int u = 0; u < V; 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 < V; u++)
			mincost[u] = min(mincost[u], G[v][u]);
	}
	return res;
}

int main() {
	int n, c;
	cin >> n;
	V = n;
	for (int i = 0; i < n; i++)
		for (int j = 0; j < n; j++) {
			cin >> c;
			if (c == -1) c = INT_MAX;
			G[i][j] = c;
		}

	cout << prim() << endl;
	return 0;
}

C++: #include<bits/stdc++.h>
#define REP(i,n) for(long long i=0;i<n;++i)
#define FOR(i,a,b) for(long long i=a;i<b;++i)
using namespace std;
const int MAX=100,INF=1<<30,WHI=0,GRA=1,BLA=2;
int n,m[MAX][MAX];
int prim(){
    int u,minv;
    int d[MAX],p[MAX],color[MAX];
    fill(d,d+n+1,INF);
    fill(p,p+n+1,-1);
    fill(color,color+n+1,WHI);
    d[0]=0;
    while(1){
        minv=INF;
        u=-1;
        REP(i,n){
            if(minv>d[i]&&color[i]!=BLA){
                u=i;
                minv=d[i];
            }
        }
        if(u==-1) break;
        color[u]=BLA;
        REP(v,n){
            if(color[v]!=BLA&&m[u][v]!=INF){
                if(m[u][v]<d[v]){
                    d[v]=m[u][v];
                    p[v]=u;
                    color[v]=GRA;
                }
            }
        }
    }
    int sum=0;
    REP(i,n){
        if(p[i]!=-1) sum+=m[i][p[i]];
    }
    return sum;
}
int main(){
    scanf("%d",&n);
    REP(i,n){
        REP(j,n){
            int e;
            scanf("%d",&e);
            m[i][j]=(e==-1)?INF:e;
        }
    }
    printf("%d\n",prim());
    return 0;
}