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<cstdio>
#define W 0
#define G 1
#define B 2
#define inf 5000
#define MAX 100
#define NIL -1
using namespace std;

int n;
int A[MAX][MAX];

int Prim(){
    int color[MAX],d[MAX],p[MAX];
    int min,u,sum = 0;
    for(int i = 0; i < n; i++){
        color[i] = W;
        d[i] = inf;
        p[i] = NIL;
    }
    
    d[0] = 0;
    
    while(1){
        u = -1;
        min = inf;
        for(int i = 0; i < n; i++){
            if(d[i] < min && color[i] != B){
                u = i;
                min = d[i];
            }
        }
        
        if(u == -1) break;
            
        color[u] = B;
            
        for(int i = 0; i < n; i++){
            if(color[i] != B && A[u][i] != NIL){
                if(A[u][i] < d[i]){
                    d[i] = A[u][i];
                    color[i] = G;
                    p[i] = u;
                }
            }
        }
    }
    
    for(int i = 0; i < n; i++) {
        
        if(p[i] != -1) sum += A[i][p[i]];
        
    }
    
    return sum;
     
}

int main(){
    int x;
    scanf("%d",&n);
    for(int i = 0; i < n; i ++){
        for(int j = 0; j < n; j++){
            scanf("%d",&x);
            if( x == -1) A[i][j] = NIL;
            else A[i][j] = x; 
        }
    }
    
    cout << Prim() << endl;
    return 0;
}

C++: #if 0
????????£????????????1.5h
??¢?????????????????§??????????????¢??°????????£????????????sum????¨??????¨????????????????????£????????§?????????
???????????¢??????????????§????????????????????????????????????"-1"???????????????????????????????????§???????????§????????¨???????°???????????????????
#endif

#include <iostream>
#include <algorithm>
#include <utility>
using namespace std;

int P[10000+10];

void init(int N){
	for(int i=0;i<=N;++i) P[i]=i;
}
int root(int a){
	if(P[a]==a)return a;
	else return(P[a]=root(P[a]));
}
bool is_same_set(int a,int b){
	return root(a)==root(b);
}
void unite(int a,int b){
	P[root(a)]=root(b);
}
int main(){
	int N;
	cin>>N;
	int A[N][N];
	
	for(int i=0; i<N;i++){
		for(int j=0;j<N;j++){
			cin>>A[j][i]; //???????????\???
		}
	}

	int M = 0; 
	int cost[N*N],src[N*N],dst[N*N];
	for (int r=0; r<N;r++)
		for (int c=0; c<r;c++) //r<c???????§?????????????
			if (A[r][c] > -1) {
				cost[M] = A[r][c];
				src[M] = r;
				dst[M] = c;
				M += 1;
			}
	pair<int,int> edges[2000+100];
	for(int i=0;i<M;i++)
		edges[i]=make_pair(cost[i],i); //first???????????????second???ID
	sort(edges,edges+M);
	
	//for(int i=0;i<M;++i)
		//cout<<edges[i].first<<endl;

	init(N);
	int sum=0;
	for(int i=0;i<M;i++){
		if(is_same_set(src[edges[i].second],dst[edges[i].second])==0){
			unite(src[edges[i].second],dst[edges[i].second]);
			sum+=edges[i].first;
		}
	}
		cout<<sum<<endl;
}

C++: #include <stdio.h>
#include <math.h>
#include <vector>
 
using namespace std;
 
int main(){
 
    int n,tmp,sum=0,tmp_min;
    scanf("%d",&n);
    int table[n][n],check[n],num_of_group=0,parent,child;
    vector<int> Group;
 
    for(int i=0;i<n;i++){
        for(int k=0;k<n;k++){
            scanf("%d",&tmp);
            table[i][k]=tmp;
        }
        check[i]=0;
    }
 
    Group.push_back(0);
    check[0]=1;
    num_of_group = 1;
    while(num_of_group<n){
        tmp_min=2001;
 
        for(int i=0;i<num_of_group;i++){
            for(int k=0;k<n;k++){
                if(table[Group[i]][k]!=-1&&check[k]==0&&table[Group[i]][k]<tmp_min){
                    tmp_min = table[Group[i]][k];
                    parent=Group[i];
                    child = k;
                }
            }
        }
        Group.push_back(child);
        sum+=table[parent][child];
        check[child]=1;
        num_of_group++;
    }
 
    printf("%d\n",sum);
 
    return 0;
}

C++: #include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;

int x[10000][10000];
int MIN[10000];
int MINX = 0;

int main() {
	memset(MIN, 127, sizeof(MIN));
	int n, a;
	cin >> n;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			cin >> a;
			x[i][j] = a;
		}
	}
	MIN[0] = 0;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			for (int k = 0; k < n; k++) {
				if (x[j][k] != -1) {
					MIN[k] = min(MIN[k], MIN[j] + x[j][k]);
				}
			}
		}
	}
	for (int i = 0; i < n; i++) {
		MINX = max(MINX, MIN[i]);
	}
	cout << MINX << endl;
	return 0;
}