Go: package main

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

var scanner = bufio.NewScanner(os.Stdin)

func nextString() string {
	scanner.Scan()
	return scanner.Text()
}

func nextInt() int {
	n, err := strconv.Atoi(nextString())
	if err != nil {
		fmt.Printf("strconv.Atoi failed: %v\n", err)
	}
	return n
}

type ElementKey int
type Node struct {
	key    ElementKey
	parent *Node
	rank   int
}

func newNode(key ElementKey) *Node {
	node := Node{}
	node.key = key
	node.parent = &node
	node.rank = 0

	return &node
}

func union(x, y *Node) {
	xSet := findSet(x)
	ySet := findSet(y)
	link(xSet, ySet)
}

func link(x, y *Node) {
	if x.rank > y.rank {
		y.parent = x
	} else {
		x.parent = y

		if x.rank == y.rank {
			y.rank++
		}
	}
}

func findSet(x *Node) *Node {
	if x != x.parent {
		x.parent = findSet(x.parent)
	}
	return x.parent
}

func newDisjointSets(nSets int) *DisjointSets {
	sets := DisjointSets{}
	sets.keyToNode = map[ElementKey]*Node{}
	for i := 0; i < nSets; i++ {
		key := ElementKey(i)
		sets.keyToNode[key] = newNode(key)
	}
	return &sets
}

type DisjointSets struct {
	keyToNode map[ElementKey]*Node
}

func (sets *DisjointSets) union(x, y ElementKey) {
	xNode := sets.keyToNode[x]
	yNode := sets.keyToNode[y]
	union(xNode, yNode)
}

func (sets DisjointSets) findSet(x ElementKey) *Node {
	xNode := sets.keyToNode[x]
	return findSet(xNode)
}

type Edge struct {
	i, j   int
	weight int
}

func main() {
	scanner.Split(bufio.ScanWords)

	n := nextInt()
	sets := newDisjointSets(n)
	edges := []Edge{}
	a := make([][]int, n)
	for i := 0; i < n; i++ {
		a[i] = make([]int, n)
		for j := 0; j < n; j++ {
			a[i][j] = nextInt()
			if a[i][j] >= 0 {
				e := Edge{
					i:      i,
					j:      j,
					weight: a[i][j],
				}
				edges = append(edges, e)
			}
		}
	}
	sort.Slice(edges, func(i, j int) bool { return edges[i].weight < edges[j].weight })
	totalWeight := 0
	for _, edge := range edges {
		i := ElementKey(edge.i)
		j := ElementKey(edge.j)
		if sets.findSet(i) != sets.findSet(j) {
			totalWeight += edge.weight
			sets.union(i, j)
		}
	}
	fmt.Println(totalWeight)
}

C++: #include<iostream>
using namespace std;
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;
static const int MAXN = 100;
static const int INTFY = (1 << 21);

int n, M[MAXN][MAXN];

int prim()
{
	int u, minv;
	int d[MAXN], p[MAXN], color[MAXN];
	
	for(int i = 0; i < n; i++)
	{
		d[i] = INTFY;
		p[i] = -1;
		color[i] = WHITE;
	}
	
	d[0] = 0;
	
	while(true)
	{
		minv = INTFY;
		u = -1;
		for(int i = 0; i < n; i++)
		{
			if(minv > d[i] && color[i] != BLACK)
			{
				u = i;
				minv = d[i];
			}
		}

		if(u == -1) break;
		color[u] = BLACK;

		for(int v = 0; v < n; v++)
		{
			if(color[v] != BLACK && M[u][v] != INTFY)
			{
				if(M[u][v] < d[v])
				{
					d[v] = M[u][v];
					p[v] = u;
					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 ()
{
	cin >> n;
	for(int i = 0; i < n; i++)
	{
		for(int j = 0; j < n; j++) 
		{
			int e;
			cin >> e;
			M[i][j] = (e == -1)? INTFY : e; 
		}
	}
	cout << prim() << endl;
	
	return 0;
}

C++: //http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_12_A&lang=jp
#include<vector>
#include<iostream>
#include<queue>
#include<functional>
using namespace std;

constexpr int INF = 1e9;

struct edge
{
	int from, to, cost;
	bool operator>(const edge& other)
	const{
		return cost > other.cost;
	}
};

using edges = vector<edge>;//辺を集めた
using graph = vector<edges>;//i番目のvectorには頂点iから延びる辺全てが並んでる

void add_edge(graph& g, int from, int to, int cost)
{
	g[from].push_back(edge{ from,to,cost });
	g[to].push_back(edge{ to,from,cost });
}

edges prim(const graph& g)
{
	const int N = g.size();//頂点の数
	vector <bool> used(N);
	used[0] = true;
	priority_queue<edge, vector<edge>, greater<edge>>p_que;
	for (auto e : g[0])
	{
		p_que.push(e);
	}
	edges mst;//最小全域木
	while (!p_que.empty())
	{
		auto e=p_que.top();//重みが最小の辺にアクセス
		p_que.pop();//重みが最小の辺をpriority_queueから削除
		if (used[e.to])//重み最小の辺の行き先がすでに訪れたことがある
		{
			continue;
		}
		//重み最小の辺の行き先がすでに訪れたことがない
		mst.push_back(e);//最小全域木に辺を追加
		used[e.to] = true;
		for (auto new_e : g[e.to])//集合Tに新しく含まれた頂点につながっている辺をpriority_queueに追加
		{
			p_que.push(new_e);
		}
	}
	return mst;

}

int main()
{
	int V, E;//Vは頂点　Eは辺の数
	cin >> V >> E;
	graph g(V);//「各頂点とそこから延びる辺」を頂点の数だけ並べた
	for (int i = 0; i < E; i++)
	{
		int from, to, cost;
		cin >> from >> to >> cost;
		add_edge(g, from, to, cost);
	}
	const auto mst = prim(g);
	int ans = 0;
	for (auto& e : mst)
	{
		ans += e.cost;
	}
	cout << ans << endl;
	return 0;
}

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

int main(){
    int n;cin >> n;
    A.assign(n,vector<int>(n));
    for(auto &i:A)for(auto &j:i) cin >> j;
    vector<int> imat(n);//今行き終わった頂点
    bool flag = true;
    imat[0] = 1;
    int ans = 0;
    while(flag){
        pair<int,int> pa(2001,-1);
        for(int now=0;now<n;now++){
            if(!imat[now]) continue;
            for(int to=0;to<n;to++){
                if(imat[to]) continue;
                if(A[now][to]==-1) continue;
                if(pa.first>A[now][to]){
                    pa.first = A[now][to];
                    pa.second = to;
                }
            }
        }
        imat[pa.second] = 1;
        ans+=pa.first;
        bool e = true;
        for(int i=0;i<n;i++){
            if(imat[i]==0){
                e = false;
                break;
            }
        }
        if(e) flag = false;
    }
    cout << ans << endl;
}

C++: #include<iostream>
#include<climits>
using namespace std;
typedef struct{
  int a[2000];
  bool f;
}node;

node T[103];
int m;
int n;
int ans;
int cnt;
int num;
void route(){
  m=INT_MAX;
  if(cnt==n)return;
  for(int i=0;i<n;i++){
    if(T[i].f==true){
      for(int j=0;j<n;j++){
	if(m>T[i].a[j]){
	  m=T[i].a[j];
	  num=j;
	}
      }
    }
  }
  ans+=m;
  cnt++;
  T[num].f=true;
  route();
}
int main(){
  int key;
  cin>>n;
  for(int i=0;i<n;i++){
    for(int j=0;j<n;j++){
      cin>>key;
      if(key==-1)T[i].a[j]=INT_MAX;
      else T[i].a[j]=key;
    }
  }
  T[0].f=true;
  route();
  cout<<ans<<endl;
  return 0;
}

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

#define int long long int
#define rep(a,b,c) for(int a=b;a<c;a++)
#define repm(a,b,c) for(int a=(b-1);a>=c;a--)
#define pb push_back
#define str string
#define sf(a) scanfs("%d",&a)
#define pb push_back
#define mp make_pair
#define Fi first
#define Se second
#define ALL(v) (v).begin(), (v).end()
using namespace std;
const int INF = 1e18 + 9;
const int Mod = 1e9 + 7;
inline int replac(str s){double ans=0;rep(i,0,s.length()){ans+=(s[i]-'0')*pow(10,s.length()-i-1);}return (int)ans;}
inline string numstr(int m){str s="";while(m>0){char c;int a=m/10;if(a>0)a=m%(a*10);else a=m;c=(char)('0'+a);s+=c;m/=10;}str st="";for(int i=s.size()-1;i>=0;i--){st+=s[i];}return st;}
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef vector<pii> vii;
int cost[1001][1001];///i->j=cost;
int mincost[1001];///shougoX->minimum-cost
bool used[1001];///inX?
int N;///ten-kazu
int prim(){
    rep(i,0,N){
        mincost[i]=INF;
        used[i]=false;
    }
    mincost[0] = 0;
    int res = 0;

    while(true){
        int v = -1;
        rep(u,0,N){
            if(!used[u]&&(v == -1 || mincost[u] < mincost[v]))v=u;
        }
        if(v == -1)break;
        used[v] = true;
        res += mincost[v];

        rep(i,0,N){
            mincost[i] = min(mincost[i], cost[v][i]);
        }
    }
    return res;
}

signed main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    map<int,int> mx,my;
    int n;
    cin >> n;
    N=n;
    rep(i,0,1001){
        rep(j,0,1001){
            cost[i][j]=INF;
        }
    }
    rep(i,0,n){
        rep(j,0,n){
            int x;cin >> x;
            if(x==-1)continue;
            cost[i][j]=x;
            cost[j][i]=x;
        }
    }
    cout << prim() << endl;
    return 0;
}