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<algorithm>

using namespace std;

int P[110];
pair<int,int> bridge[10010];

void init(int N){
  for(int i = 0;i<N;i++){
    P[i] = i;
  }
}

int root(int a){
  if(P[a] == a){
    return a;
  }
  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,a[10010],b[10010],w,count = 0,result = 0,x,y;
  cin >>n;
  init(n);
  for(int i=0;i<n;i++){
    for(int k=0;k<n;k++){
      cin >> w;
      if(w >= 0){
        bridge[count].first = w;
        a[count] = i;
        b[count] = k;
        bridge[count].second = count;
        count ++;
      }
    }
  }
  sort(bridge,bridge+count);
  for(int i=0;i<count;i++){
    x = a[bridge[i].second];
    y = b[bridge[i].second];
    if(!is_same_set(x,y)){
      unite(x,y);
      result += bridge[i].first;
      //cout << x <<" " << y << " " << bridge[i].first << endl;
    }
  }
  cout << result << endl;
}

C++: #include <bits/stdc++.h>
#define r(i,a,n) for(int i=a;i<n;i++)
using namespace std;
int main(){
	int n,p;
	while(cin>>n>>p,n){
		int q,a[10000][1261],ch[10000][1261],b,c,d,e;
		r(i,0,p)r(j,0,1261)ch[i][j]=a[i][j]=0;
		cin>>q;
		r(i,0,q){
			cin>>b>>c>>d>>e;
			if(e==1){a[d-1][b]++;ch[d-1][b]=c;}
			else{
				for(int j=b-1;j>=0;j--){
					if(ch[d-1][j]==c)break;
					a[d-1][j]++;
				}
			}
		}
		cin>>q;
		while(q--){
			int s=0;
			cin>>b>>c>>d;
			r(i,b,c)if(a[d-1][i])s++;
			cout<<s<<endl;
		}
	}return 0;
}

C++: #include <iostream>
#include <queue>
#include <vector>
#include <utility>
using namespace std;
typedef pair<int,int> Info;
int main(){
        int N;
        cin >> N;
        vector <vector<int> > adjancey(N,vector<int>(N));
        priority_queue<Info, vector<Info>,greater<Info> > descending;
        for(int i=0 ; i<N ; i++ )
                for(int j=0 ; j<N ; j++ )
                        cin >> adjancey[i][j];
        Info wn;
        int now=0,counter=0;
        vector <bool> past(N,false);
        while(true){
                past[now]=true;
                for(int i=0 ; i<N ; i++ ){
                        if(past[i])
                                continue;
                        if(adjancey[now][i]==-1)
                                continue;
                        wn.first=adjancey[now][i];
                        wn.second=i;
                        descending.push(wn);
                }
                wn=descending.top();descending.pop();
                now=wn.second;
                if(past[now])break;
                counter+=wn.first;
        }
        cout << counter << endl;
        return 0;
}

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

#define INF 2000000000

int x[10000][10000];
int MIN[10000];
int p[10000];

int prim(){
    int v,MINX;
    int col[10000];
    for(int i=0;i<n;i++){
        MIN[i]=INF;
        p[i]=-1;
        col[i]=0;
    }
    MIN[0]=0;
    while(true){
        MINX=INF;
        v=-1;
        for(int i=0;i<n;i++){
            if(MINX > MIN[i] && col != 2){
                u=i;
                MINX = MIN[i];
            }
        }
        if(u==-1){
            break;
        }
        color[u]=black;
        for(int k=0;k<n;k++){
            if(color != 0 && x[u][k]!=-1 && MIN[k]>x[u][k]){
                MIN[k]=x[u][k];
                col[k]=1;
            }
        }
    }
    int sum=0;
    for(int i=0;i<n;i++){
        sum += x[i][MIN[i]];
    }
    return sum;
}

int main() {
	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;
		}
	}
    cout<<prim()<<endl;
	return 0;
}

C++: #include <stdio.h>
int main(){
    int n, i, j, t, a[100][100];
    int u, min, d[100], p[100], v[100];
    scanf("%d", &n);
    for(i = 0; i < n; i++) for(j = 0; j < n; j++) {
        scanf("%d", &t);
        a[i][j] = (t==-1)?1000000000:t;
    }
    for(i = 0; i < n; i++) {
        d[i] = 1000000000;
        p[i] = -1;
        v[i] = 0;
    }
    d[0] = 0;
    while(1) {
        min = 1000000000;
        u = -1;
        for(i = 0; i < n; i++) {
            if(min > d[i] && v[i] != 2) {
                u = i;
                min = d[i];
            }
        }
        if(u == -1) break;
        v[u] = 2;
        for(j = 0; j < n; j++) {
            if(v[j] != 2 && a[i][j] != 1000000000){
                if(d[j] > a[u][j]) {
                    d[j] = a[u][j];
                    p[j] = u;
                    v[j] = 1;
                }
            }
        }
        t = 0;
        for(i = 0; i < n; i++) {
            if(p[i] != -1) t+=a[i][p[i]];
        }
    }
    printf("%d\n", t);
    return 0;
}

C++: #include<iostream>
using namespace std;
static const int MAX = 100;
static const int INFTY = (1 << 21);
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;

int n, M[MAX][MAX];

int prim() {
	int u, minv;
	int d[MAX], p[MAX], color[MAX];
	
	for(int i = 0; i < n; i++) {
		d[i] = INFTY;
		p[i] = -1;
		color[i] = WHITE;
	}
	
	d[0] = 0;
	
	while(1) {
		minv = INFTY;
		u = -1;
		for(int i = 0; i < n; i++) {
			if(minv > d[i] && color[i] != BLACK) {
				u = i;
				minv = d[i];
			}
		}
	   if(u == -1) break;
	   color[u] = BLACK;
	   for(int v = 0; v < n; v++) {
           if(color[v] != BLACK && M[u][v] != INFTY){
		   	  if(d[v] > M[u][v]) {
	   		      d[v] = M[u][v];
   		          p[v] = u;
           	   	  color[v] = GRAY;
   	           }
           }
   	   }	
	}
	int sum = 0; 
	for(int i = 0; i < n; i++) {
		if(p[i] != -1) sum += M[i][p[i]];
	}
	return sum;
} 

int main() {
	cin >> n;
	
	for(int i = 0; i < n; i++) {
		for(int j = 0; j < n; j++){
			int e;
			cin >> e;
			M[i][j] = (e == -1) ? INFTY :e;
		}
	}
	
	cout << prim() << endl;
	
	return 0;
}