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<stdio.h>
using namespace std;
const int sss=1<<21;
int n;
int a[1000][1000];
int d[1000],p[10000],color[1000];
int prim(){
    int u,minv;
    for(int i=0;i<n;i++){
        d[i]=sss;
        p[i]=-1;
    }
    d[0]=0;
    while(1){
        minv=sss;
        u=-1;
        for(int i=0;i<n;i++){
            if(minv>d[i]&&color[i]!=1){
                u=i;
                minv=d[i];
            }
        }
        if(u==-1) break;
        color[u]=1;
        for(int v=0;v<n;v++){
            if(color[v]!=1&&a[u][v]!=sss){
                if(d[v]>a[u][v]){
                    d[v]=a[u][v];
                    p[v]=u;
                }
            }
        }
    }
    int sum=0;
    for(int i=0;i<n;i++){
        if(p[i]!=-1) {
            sum+=a[p[i]][i];
        }
    }
    return sum;
}
int main(){
    scanf("%d",&n);
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            int e;scanf("%d",&e);
            a[i][j]=e==-1?sss:e;
        }
    }
    printf("%d\n",prim());
    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 a[Max],p[Max],color[Max];

    for(int i=0;i<n;i++){
        a[i]=Infty;
        p[i]=-1;
        color[i]=White;
    }
    a[0]=0;

    while(1){
        minv=Infty;
        u=-1;
        for(int i=0;i<n;i++){
            if(minv>a[i]&&color[i]!=Black){
                u=i;
                minv=a[i];
            }
        }
        if(u==-1)break;
        color[u]=Black;
        for(int v=0;v<n;v++){
            if(color[v]!=Black&&M[u][v]!=Infty){
                if(a[v]>M[u][v]){
                    a[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++: /*
* @Author: Samson
* @Date:   2018-06-12 18:24:44
* @Last Modified by:   Samson
* @Last Modified time: 2018-06-13 11:59:44
*/
//   @URL : http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_12_A
#include<bits/stdc++.h>
#include<algorithm>
#include <cstdlib>
#define white 0
#define gray 1
#define black 2
#define INF 0x3f3f3f3f
using namespace std;
typedef long long LL;
const int MAXN = 1e3+10;

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

int prim()
{
	int minv,u,v;
	for(int i = 0; i < n; ++i)
	{
		d[i] = INF;
		p[i] = -1;
		color[i] = white;
	}
	
	d[0] = 0;

	while(1)
	{
		minv = INF;
		u = -1;
		for(int i = 0; i < n; ++i)
		{
			if(minv > d[i] && color[i] != black)
				minv = d[i],u = i;
		}
		if(u == -1)	break;
		color[u] = black;
		for(int v = 0; v < n; ++v)
		{
			if(color[v] != black && a[u][v] != INF)
			{
				if(d[v] > a[u][v])
				{
					d[v] = a[u][v];
					p[v] = u;
					color[v] = gray;
				}
			}
		}
	}
	int ans = 0;
	for(int i = 0; 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 = 0; i < n; ++i)
	{
		for(int j = 0; j < n; ++j)
		{
			cin>>t;
			a[i][j] = t==-1?INF:t;
		}
	}
	cout<<prim()<<'\n';
	return 0;
}

C++: 
// O(|E||V|)

#include <vector>
#include <cassert>
#include <algorithm>

using namespace std;

class Prim {
#define MAX_V 101

private:
  const int INF = 1e9 + 10;
  struct Edge {
    int to, cost;
    Edge(int to, int cost): to(to), cost(cost) { }
  };

  int V;
  vector<Edge> G[MAX_V];
  int mincost[MAX_V];
  bool used[MAX_V];

public:

  Prim(int V): V(V) { 
    if (V >= MAX_V) {
      assert(false);
    }
  }

  void AddEdge(int from, int to, int cost) {
    G[from].push_back(Edge(to, cost));
  }

  int Solve() {
    for (int i = 0; i < V; i++) {
      mincost[i] = INF;
      used[i]    = false;
    }

    mincost[0] = 0;
    int res = 0;

    while (true) {
      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 (auto &e : G[v]) {
        mincost[e.to] = min(mincost[e.to], e.cost);
      }
    }

    return res;
  }
};

#include <iostream>

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

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

  int ans = prim.Solve();

  cout << ans << endl;

  return 0;
}

C++: #include<iostream>
using namespace std;
#include<queue>
#include<vector>
#include<string>
#include<string.h>
#include<algorithm>
bool b[2003];

#define REP(i, a) for(int i=0;i<a;i++)

int main()
{
	int min,b,a,m[101][101],n ,e,p[101],d[101],u,v; //u ?????????v????????¨
	bool color[101];
	cin>>n;
	REP(i, n) {
		REP(j, n) {
			cin>>m[i][j];
			if(m[i][j]==-1) m[i][j]=2001;
		}
		color[i]=0;
		d[i]=2001;
		p[i]=2001;
	}
	a=0;
	d[0]=0;
	while(1){
		min=2001;
		u=-1;
		REP(i,n){
			if(min>d[i]&&color[i]==0){
				min=d[i];
				u=i;
			}
		}
		if(u==-1) break;
		color[u]=1;
		REP(i,n){
			if(color[i]==0&&m[u][i]!=2001){
				if(d[i]>m[u][i]){
					d[i]=m[u][i];
					p[i]=u;
				}
			}
		}

	}
	min=0;
	REP(i,n){
		if(p[i]!=2001){
		min=min+m[i][p[i]];
		}
	}
	cout<<min<<endl;
	return 0;
}