Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

const BIG_COST = 3000

const (
	ST_NOT_YET = iota
	ST_JOINED
)

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	nNodes := scanInt(sc)
	costs := make([][]int, nNodes)

	for r := 0; r < nNodes; r++ {
		costs[r] = make([]int, nNodes)
		for c := 0; c < nNodes; c++ {
			costs[r][c] = scanInt(sc)
		}
	}

	states := make([]int, nNodes)
	minCosts := make([]int, nNodes)

	for i := 1; i < nNodes; i++ {
		minCosts[i] = BIG_COST
	}

	for {
		var u int
		minCost := BIG_COST

		for i := 0; i < nNodes; i++ {
			if states[i] != ST_JOINED && minCosts[i] < minCost {
				minCost = minCosts[i]
				u = i
			}
		}

		// 全てのノードが MST に JOIN できたので終了。
		if minCost == BIG_COST {
			break
		}

		states[u] = ST_JOINED

		for v := 0; v < nNodes; v++ {
			cost := costs[u][v]

			// MST内の u と MST外の v との辺の重みをそれぞれ minCosts[v] として
			// 保存しておく。このうち辺の重みが最小となる v が次のループで MST に JOIN する。
			if states[v] != ST_JOINED && cost != -1 && cost < minCosts[v] {
				minCosts[v] = cost
			}
		}
	}

	sum := 0
	for _, cost := range minCosts {
		sum += cost
	}
	fmt.Println(sum)
}

func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	v, _ := strconv.Atoi(sc.Text())
	return v
}

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()