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 <vector>
#include <algorithm>

struct edge {
	int x;
	int y;
	int cost;
	edge(int x, int y, int cost) { this->x = x; this->y = y; this->cost = cost; }
};

bool operator<(const edge& e, const edge& f) { return e.cost < f.cost; }
bool operator<=(const edge& e, const edge& f) { return e.cost <= f.cost; }
bool operator>(const edge& e, const edge& f) { return e.cost > f.cost; }
bool operator>=(const edge& e, const edge& f) { return e.cost >= f.cost; }
bool operator==(const edge& e, const edge& f) { return e.cost == f.cost; }
bool operator!=(const edge& e, const edge& f) { return e.cost != f.cost; }

int root(int* par, int x)
{
	if (par[x] == x) return x;
	else return root(par, par[x]);
}
	
bool isSame(int* par, int x, int y)
{
	return root(par, x) == root(par, y);
}

void unite(int* par, int x, int y)
{
	if (!isSame(par, x, y)) par[root(par, x)] = root(par, y);
}

int main()
{
	int n; std::cin >> n;
	
	int *par = new int[n];
	for (int i=0; i<n; i++) par[i] = i;
	
	std::vector<edge> edge_list;
	
	for (int i=0; i<n; i++) {
		for (int j=0; j<n; j++) {
			int cost;
			std::cin >> cost;
			if (i < j && cost >= 0) edge_list.push_back( edge(i, j, cost) );
		}
	}
	
	std::sort(edge_list.begin(), edge_list.end());
	
	int totalcost = 0;
	int num_of_edges = 0;
	for (std::vector<edge>::iterator it = edge_list.begin(); it != edge_list.end(); ++it) {
		if (isSame(par, (*it).x, (*it).y)) continue;
		unite(par, (*it).x, (*it).y);
		totalcost += (*it).cost;
		num_of_edges++;
		// std::cout << (*it).x << "," << (*it).y << " " << (*it).cost << std::endl;
		if (num_of_edges == n-1) break;
	}
	
	std::cout << totalcost << std::endl;

	delete[] par;
	
	return 0;
}

C++: #include<stdio.h>
static const int INFTY = (1<<21);
#define MAX 100
#define WHITE 0
#define GRAY 1
#define BLACK 2


int n,v,i,M[MAX][MAX];
int prim(void);

int main(){
  int j,e;
  scanf("%d",&n);

  for(i=0;i<n;i++){
    for(j=0;j<n;j++){
      scanf("%d",&e);
      M[i][j]=(e==-1)?INFTY:e;
    }
  }
  printf("%d",prim());
  return 0;
}

int prim(){
  int u,minv,sum=0;
  int d[MAX],p[MAX],color[MAX];

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

  d[0]=0;

  while(1){
    minv=INFTY;
    u=-1;
    for(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(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;
        }
      }
    }
  }
  for(i=0;i<n;i++){
    if(p[i]!=-1)sum+=M[i][p[i]];
  }
  return sum;
}

C++: #include<iostream>
#include<vector>
#include<queue>
#include<algorithm>
//#include<operator>

using namespace std;


class UFT{
public:
  int P[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 root(P[a]);
  }

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

  void unite(int a,int b){
    //aのルートだったもののルートをbのルートにする
    P[root(a)]=root(b);
  }
};

bool is_same_set_all(UFT a,int n){
  for(int i=0;i<n-1;i++){
    if(!(a.is_same_set(i,i+1))){return false;}
  }
  return true;
}

class edge{
public:
  int from_Node;
  int to_Node;
  int cost;
};

//本来は<さえオーバーロードすれば大丈夫が四つのオペレータをオーバロードしなければならなかった(何故？？)
bool operator< (const edge& e1,const edge& e2){
  return e1.cost < e2.cost;
}


bool operator> (const edge& e1,const edge& e2){
  return e1.cost > e2.cost;
}


bool operator<= (const edge& e1,const edge& e2){
  return e1.cost <= e2.cost;
}

bool operator>= (const edge& e1,const edge& e2){
  return e1.cost >= e2.cost;
}


int main(){
  #if 0
  このようにデバッグができる(比較演算子のオーバーロード)
  edge a={0,1,2},b={1,2,3};
  if(a<b){cout << "a<b" << endl;}
  if(b<a){cout << "b<a" << endl;}
  if(a>b){cout << "a>b" << endl;}
  if(a<=b){cout << "a<=b" << endl;}
  #endif


  int n;cin >> n;
  priority_queue<
  edge,//要素の型
  vector<edge>,//内部コンテナはvectorで(デフォ)
  greater<edge>//昇順
  > ve;
  for(int i=0;i<n;i++){
    for(int j=0;j<n;j++){
      edge e;int c;
      cin >> c;
      if(c!=-1 and i<j){
        e.from_Node=i;e.to_Node=j;e.cost=c;
        ve.push(e);
      }
    }
  }
  UFT G;G.init(n);
  int co=0;
  while(!(is_same_set_all(G,n))){
    if(!(G.is_same_set(ve.top().from_Node,ve.top().to_Node))){
      G.unite(ve.top().from_Node,ve.top().to_Node);
      co+=ve.top().cost;
      ve.pop();
    }else{
      ve.pop();
    }
  }
  cout << co << endl;

}

C++: #include <bits/stdc++.h>
#define MAX 20000
using namespace std;

int main(void){
  ios::sync_with_stdio(false);
  cin.tie(0);

  int n; cin >> n;
  vector< vector<int> > v(n, vector<int>(n));
  for(int i = 0; i < n; i++){
    for(int j = 0; j < n; j++){
      cin >> v[i][j];
      if(v[i][j] == -1) v[i][j] = MAX;
    }
  }

  vector<int> cost(n, MAX), used(n, false);
  cost[0] = 0;
  int ans = 0;
  int t;

  while(true){
    t = -1;
    for(int i = 0; i < n; i++){
      if(!used[i] && (t == -1 || cost[t] > cost[i])) t = i;
    }
    if(t == -1) break;
    used[t] = true;
    ans += cost[t];

    for(int i = 0; i < n; i++){
      cost[i] = min(cost[i], v[t][i]);
    }
  }

  cout << ans << endl;

  return 0;
}