Go: package main

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

const (
	MAX = 100
	INF = 1 << 21
	WHITE = 0
	GRAY = 1
	BLACK = 2
)

var (
	n int
	M [MAX][MAX]int
)

func prim() int {
	var u, minv int
	var d [MAX]int
	var p [MAX]int
	var color [MAX]int

	for i := 0; i < n; i++ {
		d[i] = INF
		p[i] = -1
		color[i] = WHITE
	}
	d[0] = 0
	for {
		minv = INF
		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] != INF {
				if d[v] > M[u][v] {
					d[v] = M[u][v]
					p[v] = u
					color[v] = GRAY
				}
			}
		}
	}
	sum := 0
	for i := 0; i < n; i++ {
		if p[i] != -1 {
			sum += M[i][p[i]]
		}
	}
	return sum
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Scan()
	n, _ = strconv.Atoi(scanner.Text())
	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			scanner.Scan()
			e, _ := strconv.Atoi(scanner.Text())
			if e == -1 {
				M[i][j] = INF
			} else {
				M[i][j] = e
			}
		}
	}
	fmt.Println(prim())
}

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