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++: //
// Created by tattsun on 2017/02/16.
//

#include <iostream>

#define MAX_N 101
#define INFTY 20000
#define NIL -1

enum Color {
    White,
    Glay,
    Black,
};

using namespace std;

int n;
int w[MAX_N][MAX_N];
int d[MAX_N], p[MAX_N];
Color color[MAX_N];

int min(int x, int y) {
    if(x < y) return x;
    return y;
}

int solve() {
    d[0] = 0;

    while(true) {
        int u = NIL, c = INFTY;

        for(int i=0; i<n; i++) {
            if(d[i] < c && color[i] != Black) {
                u = i;
                c = d[i];
            }
        }

        if(u == NIL)
            break;

        color[u] = Black;

        for(int i=0; i<n; i++) {
            if(color[i] != Black && w[u][i] != INFTY) {
                if(d[i] > w[u][i]) {
                    d[i] = w[u][i];
                    p[i] = u;
                    color[i] = Glay;
                }
            }
        }
    }


    int tc = 0;
    for(int i=0; i<n; i++) {
        if(p[i] != NIL)
            tc += w[i][p[i]];
    }
    return tc;
}

int main() {
    for (int i = 0; i < MAX_N; i++) {
        d[i] = INFTY;
        p[i] = NIL;
        color[i] = White;
        for (int j = 0; j < MAX_N; j++)
            w[i][j] = INFTY;
    }

    int p;
    cin >> n;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            cin >> p;
            w[i][j] = (p == -1) ? INFTY : p;
        }
    }

    cout << solve() << endl;
}

C++: #include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define rrep(ri,n) for(int ri = (int)(n-1); ri >= 0; ri--)
#define rep2(i,x,n) for(int i = (int)(x); i < (int)(n); i++)
#define repit(itr,x) for(auto itr = x.begin(); itr != x.end(); itr++)
#define rrepit(ritr,x) for(auto ritr = x.rbegin(); ritr != x.rend(); ritr++)
#define ALL(n) begin(n), end(n)
using ll = long long;
using namespace std;

int main(){
  int n;
  cin >> n;
  vector<vector<int>> gr(n, vector<int>(n));
  rep(i, n){
    rep(j, n){
      int in;
      cin >> in;
      gr.at(i).at(j) = in;
    }
  }
  vector<bool> chk(n, false);
  int ans = 0;
  priority_queue<pair<int, int>> pq;
  pq.push(make_pair(0, 0));
  int co = 0;
  while(!pq.empty()){
    auto p = pq.top(); pq.pop();
    if(chk.at(p.second) == true) continue;
    chk.at(p.second) = true;
    ans += p.first;
    // cerr << ans << endl;
    co++;
    if(co == n) break;

    rep(i, n){
      if(gr.at(p.second).at(i) == -1) continue;
      if(chk.at(i) == true) continue;
      pq.push(make_pair(-1*gr.at(p.second).at(i), i));
    }
  }
  cout << -1*ans << endl;
  return 0;
}

C++: #include <iostream>
#include <queue>
#include <utility>
#include <vector>

#define MAX_NODE 10000
#define INF 1000000000

using namespace std;

struct Edge {
	int to, w;
};
typedef pair<int, int> P;

vector<Edge> adj[MAX_NODE];
int minW[MAX_NODE];
bool flag[MAX_NODE];

int prim(int n, int start) {
	priority_queue<P, vector<P>, greater<P>> que;
	fill(minW, minW + n, INF);
	fill(flag, flag + n, false);

	int sum = 0;
	minW[start] = 0;
	que.push({0, start});
	while (!que.empty()) {
		P p = que.top();
		que.pop();
		int from = p.second;

		if (flag[from])
			continue;

		flag[from] = true;
		sum += p.first;

		for (Edge& e : adj[from])
			if (e.w < minW[e.to]) {
				minW[e.to] = e.w;
				que.push({minW[e.to], e.to});
			}
	}

	return sum;
}

int main() {
	int n;

	cin >> n;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			int x;
			cin >> x;
			if (x != -1) {
				adj[i].push_back({j, x});
			}
		}
	}

	printf("%d\n", prim(n, 0));

	return 0;
}

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[10001][1262],ch[10001][1262],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;
}