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<map>
#include<limits>
using namespace std;

const int MAX = numeric_limits<int>::max();

class Node{
public:
  int _id;
  bool _isFound;
  vector<int> _adj_index; // 隣接点のid
  vector<int> _adj_vertices;  // 隣接点との距離(重み)

  void init(int id, int n){
    _id = id;
    _isFound = false;
    for(auto i = 0; i < n; i++){
      _adj_vertices.push_back(MAX);
    }
  }
};

void inputGraph(vector<Node>& graph){
  for(auto i = 0; i < graph.size(); i++){
    graph[i].init(i, graph.size());
    for(auto j = 0; j < graph.size(); j++){
      int tmp;
      cin >> tmp;
      if(tmp != -1){
        graph[i]._adj_index.push_back(j);
        graph[i]._adj_vertices[j] = tmp;
      }
    }
  }
}

int mst(vector<Node>& graph, int search){
  auto sum = 0;
  vector<int> searched; // 木に追加済みのNodeのid
  searched.push_back(search);
  graph[searched[0]]._isFound = true;
  int minNodeIndex;

  while(searched.size() < graph.size()){
    auto compare = MAX;

    for(auto i = 0; i < searched.size(); i++){
      for(auto j = 0; j < graph[searched[i]]._adj_index.size(); j++){
//  cout << "call in for " << j << endl;
//  cout << compare << endl;
        Node searching = graph[searched[i]];
        if(!graph[searching._adj_index[j]]._isFound &&
           searching._adj_vertices[searching._adj_index[j]] < compare){
          // 木の隣接点で最短
          compare = searching._adj_vertices[searching._adj_index[j]];
          minNodeIndex = searching._adj_index[j];
 //         cout << "added" << endl;
        }
      }
    }
    if(compare != MAX){
      sum += compare;
      graph[minNodeIndex]._isFound = true;
      searched.push_back(minNodeIndex);
//      cout << endl << "minNodeIndex:" << minNodeIndex << endl;
    }

  }
  return sum;
}

int main(){
  int n;
  cin >> n;
  vector<Node> graph(n);

  inputGraph(graph);
  cout << mst(graph, 0) << endl;

  return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

int main() {
    int n; cin >> n;

    int adj[n][n];
    int inf = 2001;

    for(int i = 0; i < n; i++) {
        for(int j = 0; j < n; j++) {
            int k; cin >> k;
            adj[i][j] = k == -1 ? inf : k;
        }
    }

    int sd[n] = {1};
    int ans = 0;

    while(true) {
        int a = -1, c = inf;

        for(int i = 0; i < n; i++) {
            if(sd[i] == 0) { 
                if(a == -1) {
                    a = i;
                }
                continue;
            }
            for(int j = 0; j < n; j++) {
                if(adj[i][j] < c && sd[j] == 0) {
                    c = adj[i][j];
                    a = j;
                }
            }
        }

        if(a == -1) break;

        ans += c;
        sd[a] = 1;
    }

    cout << ans << endl;

}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cassert>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)
#define rep(i,n) FOR(i,0,n)
#define DEBUG(x) cout<<#x<<": "<<x<<endl
#define vint vector<int>
#define vdouble vector<double>
#define vstring vector<string>
using namespace std;

#include<map>
#include<set>
#include<queue>

typedef long long ll;
typedef unsigned long long ull;

const int MAX_N = 1000000;
const int INFTY = (1 << 21); // 2097152
// const ll INFTY = (1 << 60);

class Edge{
public:
	int from, to, cost;
	Edge(int from = 0, int to = 0, int cost = 0) :
			from(from), to(to), cost(cost){}

	bool operator < (const Edge &e) const{
		return cost < e.cost;
	}
	bool operator >(const Edge &e) const{
		return cost > e.cost;
	}

};

class Node{
public:
	vector<Edge> edges;
	int dist;

	Node(){
		dist = INFTY;
	}
};

class Graph{
public:
	vector<Node> nodes;
	Graph(int n) {
		rep(i, n) { Node n;  nodes.push_back(n); }
	}
	Node& operator[](int i){
		return nodes[i];
	}
};

class Data{

public:
	int dist;
	Edge *edge;
	Data(int dist, Edge *edge) {
		this->dist = dist;
		this->edge = edge;
	}

	bool operator < (const Data &d) const{
		return dist < d.dist;
	}
	bool operator >(const Data &d) const{
		return dist > d.dist;
	}
};

Graph G(0);
int N;

int prim(){

    priority_queue<Edge, vector<Edge>, greater<Edge>> pq;
    Edge e0 = Edge(-1, 0, 0);
    pq.push(e0);

    while(!pq.empty()){
        Edge e = pq.top(); pq.pop();
        Node &n = G[e.to];
        if(n.dist != INFTY) continue;
        n.dist = e.cost;
        // cerr << n.dist << endl;

        for(auto &ee: n.edges){
            pq.push(ee);
        }
    }

    int ret = 0;
    rep(i, N) ret += G[i].dist;
    return ret;
}

int main() {
	cin >> N;
	G = Graph(N);

    rep(i, N){
        rep(j, N) {
            int cost;
            cin >> cost;
            if(cost != -1){
                int from = i; int to=j;
                G[from].edges.push_back(Edge(from, to, cost));
            }
        }
    }

	cout << prim() << endl;
}

C++: #include <iostream>
#include <queue>
#include <climits>
using namespace std;

const int N = 100;
int n;
int A[N][N];
bool visit[N] = {0};
priority_queue< pair<int, int> > Q;

int main()
{
	cin >> n;
	for (int i=0; i<n; i++)
	{
		for (int j=0; j<n; j++)
		{
			cin >> A[i][j];
		}
	}

	Q.push(pair<int, int>(0,0));

	int cost = 0;

	while (Q.size())
	{
		pair<int, int> q = Q.top();
		Q.pop();
		int k = q.second;
		if (visit[k]) continue;
		cost += q.first;
		visit[k]=true;
		for(int j=0;j<n;j++)
		{
			int c = A[k][j];
			if (c <= 0) continue;
			if (visit[j]) continue;
			Q.push(pair<int, int>(-c,j));
		}
	}

	cout << -cost << endl;
		
	return 0;
}