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<cstdio>
#include<cstdlib>
#include<cstring>
#include<stack>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<vector>

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 )
            {
                // u → v の辺の中で, 最小コストの辺のコストを記録する
                if( d[v] > M[u][v] )
                {
                    // コストの記録
                    d[v] = M[u][v];
                    // 頂点 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(void)
{

    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>
using namespace std;
static const int MAX = 100;
static const int INF = (1 << 21);
static const int BLACK = 2;
static const int WHITE = 0;
static const int GLAY = 1;

int n, M[MAX][MAX];

int prim(){
	int d[MAX], p[MAX], color[MAX];
	for (int i = 0; i < n; i++){
		d[i] = INF;
		p[i] = -1;
		color[i] = WHITE;
	}
	d[0] = 0;
	while (1){
		int u = -1;
		int minv = INF;
		for (int v = 0; v < n; v++){
			if (minv > d[v] && color[v] != BLACK){
				minv = d[v];
				u = v;
			}
		}
		if (u == -1)break;
		color[u] = BLACK;
		for (int v = 0; v < n; v++){
			if (color[v] != BLACK && M[u][v] != INF){
				if (d[v] > M[u][v]){
					d[v] = M[u][v];
					p[v] = u;
					color[v] = GLAY;
				}
			}
		}
	}
	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;
	int e;
	for (int i = 0; i < n; i++){
		for (int j = 0; j < n; j++){
			cin >> e;
			M[i][j] = (e == -1) ? INF : e;
		}
	}
	cout << prim() << endl;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>

class Side {
public:
	int r, l;
	int w;
	Side(int right, int left, int weight) {
		r = right;
		l = left;
		w = weight;
	};
	bool operator>=(Side s) {
		return w >= s.w;
	}
	bool operator<(Side s) {
		return w < s.w;
	}
};

class Node {
public:
	int p; /// parent
	int d; /// depth
	Node() {
		p = -1;
		d = 1;
	}
};

int Find(std::vector<Node> n, int i) {
	int a = i;
	while (1) {
		if (n[a].p == -1) return a;
		a = n[a].p;
	}
}

bool Union(std::vector<Node> &n, int i, int j) {
	int a = Find(n, i);
	int b = Find(n, j);
	if (a == b) return false;
	if (n[a].d <= n[b].d) {
		n[b].p = a;
		n[a].d = n[b].d + 1;
	}
	else {
		n[a].p = b;
		n[b].d = n[a].d + 1;
	}
	return true;
}

int main() {
	int n;
	std::cin >> n;
	std::vector<Side> side;
	std::vector<Node> node(n);

	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			int a;
			std::cin >> a;
			if (a != -1) {
				side.push_back(Side(i, j, a));
			}
		}
	}

	std::sort(side.begin(), side.end());
	int m = side.size();
	int cnt = 0;
	int weight = 0;


	for (int i = 0; i < m; i++) {
		if (Union(node, side[i].r, side[i].l)) {
			cnt++;
			weight += side[i].w;
		}
		if (cnt == n - 1) break;
	}

	std::cout << weight << std::endl;
}

C++: ///
// File:  alds1_12_a.cpp
// Author: ymiyamoto
//
// Created on Wed Oct 18 01:40:49 2017
//

#include <cstdint>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

#define SIZE 100
struct route_t {
  int64_t weight;
  uint32_t index;

  bool operator<(const route_t w) const
  {
    return weight > w.weight;
  }
};

static int32_t graph[SIZE][SIZE];
static bool visited[SIZE];
static priority_queue<route_t> q;

static uint64_t prim(uint32_t n)
{
  uint64_t total = 0;
  q.push({0, 0});
  while (!q.empty()) {
    route_t r = q.top();
    q.pop();

    if (!visited[r.index]) {
      total += r.weight;
      for (uint32_t i = 0; i < n; i++) {
        if (graph[r.index][i] != -1) {
          q.push({graph[r.index][i], i});
        }
      }
      visited[r.index] = true;
    }
  }
  return total;
}

int32_t main()
{
  uint32_t n;
  cin >> n;
  for (uint32_t i = 0; i < n; i++) {
    for (uint32_t j = 0; j < n; j++) {
      cin >> graph[i][j];
    }
  }

  cout << prim(n) << endl;

  return 0;
}

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

int main() {

  int n;
  cin >> n;

  int INF = 10000;
  int graph[n][n];
  for(int i = 0; i < n; i++) {
    for(int j = 0; j < n; j++) {
      int v;
      cin >> v;
      if (v == -1) {
        graph[i][j] = INF;
      } else {
        graph[i][j] = v;
      }
    }
  }

  int answer = 0;

  vector<bool> visited(n);
  vector<int> cost(n);

  visited[0] = 1;

  for (int i = 0; i < n; i++) {
    cost[i] = graph[0][i];
  }

  while (1) {
    int v = -1;
    for (int i = 0; i < n; i++) {
      if (visited[i] || cost[i] < 0) {
        continue;
      }
      if (v < 0 || cost[i] < cost[v]) {
        v = i;
      }
    }
    if (v < 0) {
      break;
    }
    visited[v] = 1;
    answer += cost[v];
    for (int i = 0; i < n; i++) {
      if (graph[v][i] < 0) {
        continue;
      }
      if (cost[i] < 0 || graph[v][i] < cost[i]) {
        cost[i] = graph[v][i];
      }
    }
  }

  cout << answer << endl;

  return 0;
}