Go: package main

import (
	"bufio"
	"container/heap"
	"fmt"
	"os"
	"strconv"
)

type Node struct {
	id     int
	weight int
}

func init() {
	sc.Split(bufio.ScanWords)
}

type NodeHeap []Node

func (h NodeHeap) Len() int           { return len(h) }
func (h NodeHeap) Less(i, j int) bool { return h[i].weight < h[j].weight }
func (h NodeHeap) Swap(i, j int)      { h[i], h[j] = h[j], h[i] }

func (h *NodeHeap) Push(x interface{}) {
	*h = append(*h, x.(Node))
}

func (h *NodeHeap) Pop() interface{} {
	old := *h
	n := len(old)
	x := old[n-1]
	*h = old[0 : n-1]
	return x
}

var sc = bufio.NewScanner(os.Stdin)

func scanInt() int {
	num, _ := strconv.Atoi(scanString())
	return num
}

func scanString() string {
	sc.Scan()
	return sc.Text()
}

func init() {
	sc.Split(bufio.ScanWords)
}

func main() {
	n := scanInt()

	list := make([][]int, n)
	seen := make([]bool, n)
	for i := range list {
		list[i] = make([]int, n)
		for j := range list[i] {
			list[i][j] = scanInt()
		}
	}

	start := Node{id: 0, weight: 0}
	h := &NodeHeap{start}
	heap.Init(h)

	sum := 0
	for h.Len() > 0 {
		node := heap.Pop(h).(Node)
		if seen[node.id] {
			continue
		}
		seen[node.id] = true
		sum += node.weight

		for i, w := range list[node.id] {
			if w < 0 {
				continue
			}
			heap.Push(h, Node{id: i, weight: w})
		}
	}

	fmt.Println(sum)
}

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

int n,m=0,tmp,p[110],ans=0,a,b,c; //m:辺の数
tuple<int,int,int> line[10010]; //辺

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);
}

int main(){
    cin>>n;
    init(n);
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            cin>>tmp;
            if(j>i){
                if(tmp!=-1){
                    line[m]=make_tuple(tmp,i,j);
                    m++;
                }
            }
        }
    }
    sort(line,line+m);
    init(n);
    for(int i=0;i<m;i++){
        c=get<0>(line[i]);
        a=get<1>(line[i]);
        b=get<2>(line[i]);
        if(!(is_same_set(a,b))){
            unite(a,b);
            ans+=c;
        }
        //cout<<c<<a<<b<<endl;
    }
    cout<<ans<<endl;
}

C++: #include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
#include<vector>
#include<queue>
using namespace std;

struct Edge{
  int src,dst;
  int weight;
  Edge(int src,int dst,int weight):
    src(src),dst(dst),weight(weight){}
};

typedef vector<Edge> Edges;
typedef vector<Edges> Graph;

bool operator < (const Edge &m,const Edge &n){
  return m.weight!=n.weight ? m.weight>n.weight :
    m.src!=n.src ? m.src>n.src : m.dst>n.dst;
}

struct UnionFind{
  vector<int> data;
  UnionFind(int size):
    data(size,-1){}

  int root(int x){
    return data[x]<0 ? x :
      data[x]=root(data[x]);
  }

  void unionset(int x,int y){ //bool->void
    x=root(x);y=root(y);
    if(x!=y){
      if(data[y]<data[x])
	swap(x,y);
      data[x]+=data[y];
      data[y]=x;
    }
    //return x!=y;
  }

  bool findset(int x,int y){
    return root(x)==root(y);
  }
};

/*test用関数
void PrintGraph(Graph);
void PrintEdges(Edges);
void PrintPriority_queue(priority_queue<Edge>);
*/

pair<int,Edges> MinimumSpinningForest(Graph g){
  int n=g.size();
  UnionFind uf(n);
  priority_queue<Edge> que;

  for(int i=0;i<n;i++){
    for(Edges::iterator l=g[i].begin();l!=g[i].end();l++){
      if((*l).src<(*l).dst && (*l).weight>0)
	que.push(*l);
    }
  }

  //PrintPriority_queue(que);
  int sum=0;
  Edges select;

  while((int)select.size()<n && !que.empty()){
    Edge tmp=que.top();que.pop();

    if(!uf.findset(tmp.src,tmp.dst)){
      uf.unionset(tmp.src,tmp.dst);
      select.push_back(tmp);
      sum+=tmp.weight;
    }
  }

  return pair<int,Edges>(sum,select);
}


int main(int argc,char* argv[]){
  Graph g;
  int n,x;
  pair<int,Edges> p;

  cin>>n; 
  for(int i=1;i<=n;i++){
    for(int l=1;l<=n;l++){
      cin>>x;
      Edge tmp(i,l,x);
      Edges input;input.push_back(tmp);
      g.push_back(input);
    }
  }

  //PrintGraph(g);

  p=MinimumSpinningForest(g);

  //PrintEdges(p.second);
  
  cout<<p.first<<endl;
  return 0;
}

C++: # include<iostream>
using namespace std;
const int MAX = 100;
const int INFTY = (1 << 21);
const int WHITE = 0;
const int GREY = 1;
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 (color[i] != BLACK && minv > d[i]) {
				minv = d[i];
				u = 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] = GREY;
				}
			}
		}
	}

	int sum = 0;
	for (int i = 0; i < n; i++) {
		if (p[i] != -1) sum += d[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>
#include<vector>
#include<algorithm>
#include<queue>
#include<cstring>
#define P pair<int , int>
using namespace std;

int main(){
  int n, a[109][109];
  bool check[109];
  priority_queue<P, vector<P>, greater<P> > Q;
  cin >> n;
  for(int i=0;i<n;i++){
    for(int j=0;j<n;j++)cin >> a[i][j];
  }
  memset(check, false, sizeof(check));
  int ans = 0;
  Q.push(P(0, 0));
  while(!Q.empty()){
    P p = Q.top();Q.pop();
    if(check[p.second])continue;
    ans += p.first;
    check[p.second] = true;
    for(int i=0;i<n;i++){
      if(!check[i] && a[p.second][i]!=-1){
        Q.push(P(a[p.second][i], i));
      }
    }
  }
  cout << ans << endl;
}

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;
}