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>
using namespace std;

#define INF 1000000

int main(){
  long n;
  long G[100][100],d[100];
  long minc,mi,i;
  bool flgs[i];
  long suma=0;
  
  cin >> n;
  for (i=0;i<n;i++){
    for (long j=0;j<n;j++){
      cin >> G[i][j];
      if (G[i][j]==-1) G[i][j]=INF;
    }
    d[i]=INF;
    flgs[i]=false;
  }

  d[0]=0;
  while (true){
    minc=INF;
    for (i=0;i<n;i++){
      if (!flgs[i]&&d[i]<minc){
	minc=d[i];
	mi=i;
      }
    }
    if (minc==INF) break;
    flgs[mi]=true;
    for (i=0;i<n;i++){
      if (G[mi][i]==INF)continue;
      if (!flgs[i]&&d[i]>G[mi][i]){
	d[i]=G[mi][i];
	//cout << d[i];
      }
    }
  }
  
  for (i=0;i<n;i++){
    suma+=d[i];
  }
  cout << suma<<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(true)
    {
        minv = INFTY;
        u = -1;
        for (int i = 0; i < n; i++)
        {
            if (color[i] != BLACK && d[i] < minv)
            {
                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 ( M[u][v] < d[v])
                {
                    d[v] = M[u][v];
                    p[v] = u;
                    color[v] = GRAY;
                }
            }
        }
    }
    int s = 0;
    for (int i = 0; i < n; i++)
    {
        if (p[i] != -1)
        {
            s += M[i][p[i]];
        }
    }
    return s;
}

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 <climits>
#include <set>

class Graph
{
 private:
  std::vector<std::vector<long> > weight_;
 public:
  Graph(const unsigned long node_num){};


  void insert_weight(const std::vector<long> weight){
    weight_.push_back(weight);
  };


  void display()
  {
    for(unsigned long i = 0, imax = weight_.size(); i < imax; ++i)
    {
      for(unsigned long j = 0, jmax = weight_[i].size(); j < jmax; ++j)
      {
        std::cout << " " << weight_[i][j];
      }
      std::cout << std::endl;
    }
  };


  unsigned long MinimumSpanningTree()
  {
    if(weight_.size() <= 1) return 0;

    struct Edge
    {
      long value_;
      long start_node_;
      long end_node_;
      Edge(long value = LONG_MAX, long sn = -1, long en = -1) : value_(value), start_node_(sn), end_node_(en){};
      void change(long value, long sn, long en)
      {
        value_ = value; start_node_ = sn, end_node_ = en;
      };
      void display()
      {
        std::cout << value_ << " : " << start_node_ << " : " << end_node_ << std::endl;
      };
    };

    unsigned long minimum_spanning_value = 0;

    std::set<long> connect_node; connect_node.insert(0);
    std::set<long> noconnect_node;

    for(unsigned long node_i = 1, imax = weight_.size(); node_i < imax; ++node_i)
      noconnect_node.insert(node_i);

    while(!noconnect_node.empty())
    {
      Edge min_edge;
      for(std::set<long>::iterator it = connect_node.begin(); it != connect_node.end(); ++it)
      {
        for(std::set<long>::iterator noit = noconnect_node.begin(); noit != noconnect_node.end(); ++noit)
        {
          if(weight_[*it][*noit] == -1) continue;
          if(weight_[*it][*noit] < min_edge.value_)
          {
            min_edge.value_ = weight_[*it][*noit];
            min_edge.change(weight_[*it][*noit], *it, *noit);
          }
        }
      }
      connect_node.insert(min_edge.end_node_);
      noconnect_node.erase(min_edge.end_node_);
      minimum_spanning_value += min_edge.value_;
    }

    return minimum_spanning_value;
  };//   unsigned long MinimumSpanningTree()
 protected:
};//    class Graph

int main()
{

  unsigned long node_num = 0;
  std::cin >> node_num; std::cin.ignore();

  Graph graph(node_num);

  for(unsigned long node_i = 0; node_i < node_num; ++node_i)
  {
    std::vector<long> weight(node_num, -1);
    for(unsigned long weight_i = 0, weight_num = node_num; weight_i < weight_num; ++weight_i)
    {
      std::cin >> weight[weight_i]; std::cin.ignore();
    }
    graph.insert_weight(weight);
  }

  std::cout << graph.MinimumSpanningTree() << std::endl;

  return 0;
};//    main()