Go: package main

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

var scanner = bufio.NewScanner(os.Stdin)

func nextString() string {
	scanner.Scan()
	return scanner.Text()
}

func nextInt() int {
	n, err := strconv.Atoi(nextString())
	if err != nil {
		fmt.Printf("strconv.Atoi failed: %v\n", err)
	}
	return n
}

type ElementKey int
type Node struct {
	key    ElementKey
	parent *Node
	rank   int
}

func newNode(key ElementKey) *Node {
	node := Node{}
	node.key = key
	node.parent = &node
	node.rank = 0

	return &node
}

func union(x, y *Node) {
	xSet := findSet(x)
	ySet := findSet(y)
	link(xSet, ySet)
}

func link(x, y *Node) {
	if x.rank > y.rank {
		y.parent = x
	} else {
		x.parent = y

		if x.rank == y.rank {
			y.rank++
		}
	}
}

func findSet(x *Node) *Node {
	if x != x.parent {
		x.parent = findSet(x.parent)
	}
	return x.parent
}

func newDisjointSets(nSets int) *DisjointSets {
	sets := DisjointSets{}
	sets.keyToNode = map[ElementKey]*Node{}
	for i := 0; i < nSets; i++ {
		key := ElementKey(i)
		sets.keyToNode[key] = newNode(key)
	}
	return &sets
}

type DisjointSets struct {
	keyToNode map[ElementKey]*Node
}

func (sets *DisjointSets) union(x, y ElementKey) {
	xNode := sets.keyToNode[x]
	yNode := sets.keyToNode[y]
	union(xNode, yNode)
}

func (sets DisjointSets) findSet(x ElementKey) *Node {
	xNode := sets.keyToNode[x]
	return findSet(xNode)
}

type Edge struct {
	i, j   int
	weight int
}

func main() {
	scanner.Split(bufio.ScanWords)

	n := nextInt()
	sets := newDisjointSets(n)
	edges := []Edge{}
	a := make([][]int, n)
	for i := 0; i < n; i++ {
		a[i] = make([]int, n)
		for j := 0; j < n; j++ {
			a[i][j] = nextInt()
			if a[i][j] >= 0 {
				e := Edge{
					i:      i,
					j:      j,
					weight: a[i][j],
				}
				edges = append(edges, e)
			}
		}
	}
	sort.Slice(edges, func(i, j int) bool { return edges[i].weight < edges[j].weight })
	totalWeight := 0
	for _, edge := range edges {
		i := ElementKey(edge.i)
		j := ElementKey(edge.j)
		if sets.findSet(i) != sets.findSet(j) {
			totalWeight += edge.weight
			sets.union(i, j)
		}
	}
	fmt.Println(totalWeight)
}

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