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 <bits/stdc++.h>
using namespace std;

using Weight = int;
struct Edge {
    int src, dst;
    Weight weight;
    Edge(int src_, int dst_, int weight_) :
        src(src_), dst(dst_), weight(weight_) { }
};
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;
typedef vector<Weight> Array;
typedef vector<Array> Matrix;

bool operator < (const Edge &e, const Edge &f){ return e.weight > f.weight; }
pair<Weight, Edges> prim(const Graph &g, int r = 0) {
    Edges T; Weight total = 0; vector<int> vis(g.size());
    priority_queue<Edge> q;
    q.emplace(-1, r, 0);
    while (!q.empty()) {
        Edge e = q.top(); q.pop();
        if (vis[e.dst]) continue;
        vis[e.dst] = true;
        total += e.weight; if(e.src != -1) T.push_back(e);
        for(auto& f : g[e.dst]) if (!vis[f.dst]) q.push(f);
    }
    return make_pair(total, T);
}

int main(){
    int n;
    cin >> n;
    Graph g(n);
    for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++){
            int x;
            cin >> x;
            if(x != -1) g[i].emplace_back(i,j,x);
        }
    }
    cout << prim(g).first << endl;
}

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

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

struct Graph {
    enum Color {WHITE, GRAY, BLACK};
    vector<vector<int>> cost;
    vector<Color> color;
    vector<int> mincost;
    Graph(int n) {
        cost.assign(n, vector<int>(n, INF));
        color.assign(n, WHITE);
        mincost.assign(n, INF);
    }
};

int main()
{
    int n;
    cin >> n;
    Graph g(n);
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            cin >> g.cost[i][j];
            g.cost[i][j] = g.cost[i][j] == -1 ? INF : g.cost[i][j];
        }
    }
    
    int ans = 0;

    // Prim
    g.mincost[0] = 0;
    while (true) {
        int u = -1;
        for (int v = 0; v < n; v++) {
            if (g.color[v] == Graph::WHITE && (u == -1 || g.mincost[v] < g.mincost[u])) {
                u = v;
            }
        }
        if (u == -1) break;
        g.color[u] = Graph::BLACK;
        ans += g.mincost[u];
        for (int v = 0; v < n; v++) {
            g.mincost[v] = min(g.mincost[v], g.cost[u][v]);
        }
    }
    cout << ans << endl;
}

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

using namespace std;
typedef pair<int, int> P;   //cost, to
static const int INF = 1e9;
static const int SIZE = 101;
int par[SIZE];
int rk[SIZE];
int d[SIZE];
int n;

struct edge
{
	int u, v, cost;
};

struct edge2
{
	int to, cost;
};

bool comp(const edge& e1, const edge& e2)
{
	return(e1.cost < e2.cost);
}

void init()
{
	for (int i = 0; i < SIZE; ++i)
	{
		par[i] = i;
		rk[i] = 0;
	}
}

int find(int x)
{
	if (x == par[x])
	{
		return(x);
	}
	else
	{
		return(par[x] = find(par[x]));
	}
}

void unite(int a, int b)
{
	int x = find(a);
	int y = find(b);
	if (x == y)
	{
		return;
	}
	if (rk[x] < rk[y])
	{
		par[x] = y;
	}
	else
	{
		par[y] = x;
		if (rk[x] == rk[y])
		{
			++rk[x];
		}
	}
}

bool same(int a, int b)
{
	return(find(a) == find(b));
}

int kruskal(vector<edge>& es)
{
	init();
	int res = 0;
	sort(es.begin(), es.end(), comp);
	for (int i = 0; i < es.size(); ++i)
	{
		edge e = es[i];
		if (!same(e.u, e.v))
		{
			unite(e.u, e.v);
			res += e.cost;
		}
	}
	return(res);
}

int prim(vector<edge2>* G)
{
	vector<bool> isUsed(n);
	for (int i = 0; i < n; ++i)
	{
		isUsed[i] = false;
	}
	priority_queue<P, vector<P>, greater<P> > pque;
	P p = make_pair(0, 0);
	pque.push(p);
	int res = 0;
	while (!pque.empty())
	{
		P p = pque.top();
		pque.pop();
		int v = p.second;
		if (isUsed[v])
		{
			continue;
		}
		isUsed[v] = true;
		res += p.first;
		for (int i = 0; i < G[v].size(); ++i)
		{
			edge2 e = G[v][i];
			pque.push(P(e.cost, e.to));
		}
	}
	return(res);
}


void solve()
{
	cin >> n;

// For Kruskal
	vector<edge> es;
	for (int i = 0; i < n; ++i)
	{
		for (int j = 0; j < n; ++j)
		{
			int cost;
			cin >> cost;
			if (cost != -1)
			{
				edge e;
				e.u = i;
				e.v = j;
				e.cost = cost;
				es.push_back(e);
			}
		}
	}
	cout << kruskal(es) << endl;

// For Prim
	//vector<edge2> G[SIZE];
	//for (int i = 0; i < n; ++i)
	//{
	//	for (int j = 0; j < n; ++j)
	//	{
	//		int cost;
	//		cin >> cost;
	//		if (j >= i && cost != -1)
	//		{
	//			edge2 e1, e2;
	//			e1.to = j;
	//			e1.cost = cost;
	//			e2.to = i;
	//			e2.cost = cost;
	//			G[i].push_back(e1);
	//			G[j].push_back(e2);
	//		}
	//	}
	//}
	//cout << prim(G) << endl;
}

int main()
{
	solve();
	return(0);
}