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 <cmath>
#include <vector>
#include <string>
#include <algorithm>

constexpr int INF = 3000;

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    int n, aij, mincost, minu, sum=0;
    std::cin >> n;
    std::vector<std::vector<int>> a(n);
    std::vector<int> d(n), p(n), color(n); // white:0, gray:1, black:2
    for (int i=0; i<n; i++) {
        for (int j=0; j<n; j++) {
            std::cin >> aij;
            a[i].push_back(aij);
        }
        d[i] = INF;
    }
    d[0]=0;
    p[0]=-1;
    while (true) {
        mincost = INF;
        for (int i=0; i<n; i++) {
            if (color[i] < 2 and d[i] < mincost) {
                mincost = d[i];
                minu = i;
            }
        }
        if (mincost == INF) {
            break;
        }
        color[minu] = 2;
        for (int v=0; v<n; v++) {
            if (color[v] < 2 and a[minu][v] >= 0 and d[v] > a[minu][v]) {
                d[v] = a[minu][v];
                p[v] = minu;
                color[v] = 1;
            }
        }
    }

    for (int i=0; i<n; i++) {
        sum += d[i];
    }
    std::cout << sum << "\n";

    return 0;
}

C++: #include <stdio.h>
#define rep(i, n) for(int i = 0; i < n; ++i)

int main(void) {
  int n, i, j, ans = 0;
  scanf("%d", &n);
  int a[n][n];
  char f[n];
  rep(i, n) rep(j, n) scanf("%d", &a[i][j]);
  rep(i, n) f[i] = 0;
  f[0] = 1;
  rep(i, n - 1) {
    int min = 10000, minx, miny;
    rep(j, n) {
      rep(k, n) {
        if(f[j] && !f[k] && a[j][k] > 0 && a[j][k] < min) min = a[j][k], minx = j, miny = k;
      }
    }
    ans += min;
    f[miny] = 1;
  }
  printf("%d\n", ans);
  return 0;
}

C++: #include <iostream>
#include <queue>
#include <utility>
#include <functional>
#include <vector>
using namespace std;
int cost[100][100];
int prim(int n,int s){
	typedef pair<int,int> P;
	bool ischeck[n];
	int mincost = 0;
	priority_queue<P,vector<P>,greater<P> > pq;
	P p,tp;
	for(int i = 0;i < n;++i){
		ischeck[i] = false;
		if(cost[s][i] != -1){
			p.first = cost[s][i];
			p.second = i;
			pq.push(p);
		}
	}
	ischeck[s] = true;
	while(!pq.empty()){
		p = pq.top();
		pq.pop();
		if(ischeck[p.second]) continue;
		else{
			ischeck[p.second] = true;
			mincost += p.first;
			for(int i = 0;i < n;++i){
				if(cost[p.second][i] != -1){
					tp.first = cost[p.second][i];
					tp.second = i;
					pq.push(tp);
				}
			}
		}
	}
	return mincost;
}
int main(){
	int n;
	cin >> n;
	for(int i = 0;i < n;++i){
		for(int j = 0;j < n;++j){
			cin >> cost[i][j];
		}
	}
	cout << prim(n,0) << endl;
}

C++: #include <stdio.h>
#include <limits.h>
int n,M[100][100];
int prim();
int main(){
  int i,j,w;
  scanf("%d",&n);
  for(i=0;i<n;i++){
    for(j=0;j<n;j++){
      scanf("%d",&w);
      if(w==-1)M[i][j]=INT_MAX;
      else M[i][j]=w;
    }
  }
  printf("%d\n",prim());
  return 0;
}
int prim(){
  int sum,i,u,mincost,C[100],d[100],p[100];
  for(i=0;i<n;i++){
    d[i]=INT_MAX;
    C[i]=0;
    p[i]=-1;
  }
  d[0]=0;
  while(1){
    u=-1;
    mincost=INT_MAX;
    for(i=0;i<n;i++){
      if(C[i]!=2&&d[i]<mincost){
        mincost=d[i];
        u=i;
      }
    }
    if(u==-1)break;
    C[u]=2;
    for(i=0;i<n;i++){
      if(C[i]!=2&&M[u][i]!=INT_MAX){
        if(d[i]>M[u][i]){
          d[i]=M[u][i];
          p[i]=u;
          C[i]=1;
        }
      }
    }
  }
  sum=0;
  for(i=0;i<n;i++){
    if(p[i]!=-1)sum+=M[i][p[i]];
  }
  return sum;
}

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

class TEdge;
struct TNode
{
	int TreeIndex;
	std::set<const TEdge*> Connected;
	
	TNode();
	void SetIndex(int Index, const TEdge* Edge);
};

class TEdge
{
	std::pair<TNode*, TNode*> FEnds;
	int FWeight;
public:
	TEdge(TNode* EndA, TNode* EndB, int Weight);
	
	bool operator<(const TEdge& Other)const;
	int  Weight()const {return FWeight;}
	bool ToMekeCycle()const;
	
	void JoinTree(int TreeIndex)const;
};

TNode::TNode()
:TreeIndex(0)
{
}

void TNode::SetIndex(int Index, const TEdge* Edge)
{
	if(TreeIndex >= Index) return;
	TreeIndex = Index;
	for(std::set<const TEdge*>::const_iterator It = Connected.begin(); It != Connected.end(); ++It){
		(*It)->JoinTree(Index);
	}
	Connected.insert(Edge);
}

TEdge::TEdge(TNode* EndA, TNode* EndB, int Weight)
:FEnds(EndA, EndB), FWeight(Weight)
{
}

bool TEdge::operator<(const TEdge& Other)const
{
	return Weight() < Other.Weight();
}

bool TEdge::ToMekeCycle()const
{
	return FEnds.first->TreeIndex > 0 && FEnds.first->TreeIndex == FEnds.second->TreeIndex;
}

void TEdge::JoinTree(int TreeIndex)const
{
	FEnds.first->SetIndex(TreeIndex, this);
	FEnds.second->SetIndex(TreeIndex, this);
}

class TGraph
{
	std::vector<TNode> FNodes;
	std::vector<TEdge> FEdges;
public:
	TGraph(int NodeCount);
	
	void AddEdge(int NodeIndexA, int NodeIndexB, int Weight);
	int WeightMinimumSpanningTree();
};

TGraph::TGraph(int NodeCount)
:FNodes(NodeCount)
{
}

void TGraph::AddEdge(int NodeIndexA, int NodeIndexB, int Weight)
{
	if(NodeIndexA <= NodeIndexB || Weight < 0) return;
	FEdges.push_back(TEdge(&FNodes[NodeIndexA], &FNodes[NodeIndexB], Weight));
}

int TGraph::WeightMinimumSpanningTree()
{
	std::sort(FEdges.begin(), FEdges.end());
	unsigned int TreeCount = 0;
	int Weight = 0;
	for(std::vector<TEdge>::iterator It = FEdges.begin(); It != FEdges.end() && TreeCount < FNodes.size() - 1; ++It){
		if(It->ToMekeCycle()) continue;
		It->JoinTree(TreeCount + 1);
		Weight += It->Weight();
		++TreeCount;
	}
	return Weight;
}

int main()
{
	int NodeCount;
	std::cin >> NodeCount;
	TGraph Graph(NodeCount);
	
	for(int i = 0; i < NodeCount; ++i)
	{
		for(int j = 0; j < NodeCount; ++j){
			int Weight;
			std::cin >> Weight;
			Graph.AddEdge(i, j, Weight);
		}
	}
	
	std::cout << Graph.WeightMinimumSpanningTree() << std::endl;
	return 0;
}