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++: /*
* @Author: Samson
* @Date:   2018-06-12 18:24:44
* @Last Modified by:   Samson
* @Last Modified time: 2018-06-13 12:48:59
*/
//   @URL : http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_12_A
#include<bits/stdc++.h>
#include<algorithm>
#include <cstdlib>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long LL;
const int MAXN = 1e2+10;

int p[MAXN],color[MAXN],a[MAXN][MAXN],t,n,d[MAXN];

int prim()
{
	int minv,u,v;
	memset(p,-1,sizeof p);
	memset(d,INF,sizeof d);

	d[2] = 0;
	int ans = 0;

	while(1)
	{
		minv = INF;
		u = -1;
		for(int i = 1; i <= n; ++i)
		{
			if(minv > d[i] && !color[i])
				minv = d[i],u = i;
		}
		if(u == -1)	break; //空集
		color[u] = 1; //从集合中删除
		for(int v = 1; v <= n; ++v)
		{
			if(!color[v] && a[u][v] != INF)
			{
				if(d[v] > a[u][v])
				{
					d[v] = a[u][v];
					p[v] = u;
				}
			}
		}
	}

	for(int i = 1; i <= n; ++i)
		if(p[i] != -1)
			ans += a[i][p[i]];
	return ans;
}

int main(void)
{
	ios::sync_with_stdio(false); 
	//cin.tie(0);
	cin>>n;
	for(int i = 1; i <= n; ++i)
	{
		for(int j = 1; j <= n; ++j)
		{
			cin>>t;
			a[i][j] = t==-1?INF:t;
		}
	}
	cout<<prim()<<'\n';
	return 0;
}

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

#define N 100
#define MAX 10000;

int main(void) {
    int n;
    int A[N][N];
    int min1, min2; //最小値の重みをもつ要素の添え字,min1は行,min2は列
    bool check[N]; //通ったらtrue
    int check_num[N]; //通った頂点の添え字
    int count; //通った頂点の数
    int weight;
    
    cin >> n;
    
    for(int i = 0; i < n; ++i) {
        for(int j = 0; j < n; ++j) {
            cin >> A[i][j];
            if(A[i][j] == -1) A[i][j] = MAX;
        }
    }
    
    for(int i = 0; i < n; ++i) check[i] = false;
    
    //スタートは0
    check[0] = true;
    check_num[0] = 0;
    count = 1;
    weight = 0;
    while(count < n) {
        min1 = check_num[0];
        min2 = check_num[0];
        for(int i = 0; i < count; ++i) {
            for(int j = 0; j < n; ++j) {
                if(A[check_num[i]][j] < A[min1][min2] && !check[j]) {
                    min1 = check_num[i];
                    min2 = j;
                }
            }
        }
        weight += A[min1][min2];
        check[min2] = true;
        check_num[count] = min2;
        ++count;
    }
    
    cout << weight << endl;
}

C++: #include<iostream>
#include<queue>

const static int MAX = 100;
const static int INF = 1001001001;
int A[MAX][MAX];
int d[MAX];
int p[MAX];
int color[MAX];

int main(void){
    int n, tmp, u, mincost, sum;
    std::cin >> n;
    for(int i = 0; i < n; i++){
	for(int j = 0; j < n; j++){
	    std::cin >> tmp;
	    if(tmp == -1) A[i][j] = INF;
	    else A[i][j] = tmp;
	}
    }

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

    while(true){
	mincost = INF;
	for(int i = 0; i < n; i++){
	    if(mincost > d[i] && color[i] == 0){
		mincost = d[i];
		u = i;
	    }
	}
	color[u] = 1;

	if(mincost == INF) break;

	for(int i = 0; i < n; i++){
	    if(d[i] > A[u][i] && color[i] == 0){
		d[i] = A[u][i];
		p[i] = u;
	    }
	}
    }

    sum = 0;
    for(int i = 0; i < n; i++){
	color[i] = 0;
    }
    for(int i = 0; i < n; i++){
	int cur = i;
	while(p[cur] != -1 && color[cur] == 0){
	    sum += A[p[cur]][cur];
	    color[cur] = 1;
	    cur = p[cur];
	}
    }
    std::cout << sum << std::endl;



    return 0;
}

C++: #include<iostream>
using namespace std;
static const int MAX=100;
static const int INFTY=(1<<21);
static const int white=0;
static const int gray=1;
static const int black=2;

int n,M[MAX][MAX];

int prim(){
    int u,minv;
    int d[MAX],p[MAX],color[MAX];
    for(int i=0;i<n;i++){
	d[i]=INFTY;
	p[i]=-1;
	color[i]=white;
    }
    d[0]=0;
    while(1){
	minv=INFTY;
	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]!=INFTY){
		if(d[v]>M[u][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)?INFTY:e;
	}
    }
    cout<<prim()<<endl;
    return 0;
}

C++: #include <iostream>
 using namespace std;
 static const int WHITE = 0;
 static const int GRAY = 1;
 static const int BLACK =2;
 static const int MAX = 100;
 static const int INFTY = (1<<21);
 
 int n,M[MAX][MAX];
 
 int prim(){
 	int u,miv;
 	int d[MAX],p[MAX],color[MAX];
 	
 	for(int i=0;i<n;i++){
 		d[i] = INFTY;
 		p[i] = -1;
 		color[i] = WHITE;
 	}
 	
 	d[0]=0;
 	
 	while(1){
 		u=-1;
 		miv = INFTY;
 		for(int i=0;i<n;i++){
 			if(color[i]!=BLACK&&d[i]<miv){
 				miv = d[i];
 				u=i;
 			}
 		}
 		if(miv==INFTY) break;
 		
 		color[u] = BLACK;
 		
 		for(int i=0;i<n;i++){
 			if(color[i]!=BLACK&&M[u][i]!=INFTY){
 				if(d[i]>M[u][i]){
 					d[i] = M[u][i];
 					p[i] = u;
 					color[i] = GRAY;
 				}
 			}
 		}
 	}
 	int sum=0;
 	for(int i=0;i<n;i++){
 		if(p[i]!=-1)
 		sum+=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)?INFTY:e;
 		}
 	}
 	cout<<prim()<<endl;
 	return 0;
 }