Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

const (
	WHITE int = iota
	GRAY
	BLACK
)

type graph struct {
	G [][]int
	C []int
	D []int
	P []int
}

var INFTY = 1 << 32

func constructGraph() *graph {
	sc := bufio.NewScanner(os.Stdin)
	sc.Scan()
	n, _ := strconv.Atoi(sc.Text())

	g := make([][]int, n)
	c := make([]int, n)
	d := make([]int, n)
	p := make([]int, n)

	for i := 0; i < n; i++ {
		sc.Scan()
		fields := strings.Fields(sc.Text())
		g[i] = toIntSlice(fields)
	}

	return &graph{g, c, d, p}
}

func (g *graph) initMst() {
	for i := 0; i < len(g.G); i++ {
		g.C[i] = WHITE
		g.D[i] = INFTY
	}
	g.D[0] = 0
	g.P[0] = -1
}

func (g *graph) primMst() int {
	g.initMst()

	var u int
	for {
		mincost := INFTY
		for i := 0; i < len(g.G); i++ {
			if g.C[i] != BLACK && g.D[i] < mincost {
				mincost = g.D[i]
				u = i
			}
		}

		if mincost == INFTY {
			break
		}

		g.C[u] = BLACK

		for i := 0; i < len(g.G); i++ {
			if g.C[i] != BLACK && g.existEdge(u, i) {
				if g.G[u][i] < g.D[i] {
					g.D[i] = g.G[u][i]
					g.P[i] = u
				}
			}
		}
	}

	totalCost := 0
	for _, cost := range g.D {
		totalCost += cost
	}
	return totalCost
}

func (g *graph) existEdge(u, v int) bool {
	return g.G[u][v] != INFTY && g.G[v][u] != INFTY
}

func toIntSlice(s []string) []int {
	a := make([]int, len(s))
	for i, v := range s {
		num, _ := strconv.Atoi(v)
		if num == -1 {
			num = INFTY
		}
		a[i] = num
	}
	return a
}

func main() {
	g := constructGraph()
	fmt.Println(g.primMst())
}

C++: #include <bits/stdc++.h>
#define FOR(i, m, n) for(int i = m;i < n;i++)
#define REP(i,n) for(int i=0;i<n;i++)
#define FORq(i, m, n) for(int i = m;i <= n;i++)
#define SCD(n) scanf("%d",&n)
#define SCD2(m,n) scanf("%d%d",&m,&n)
#define SCD3(m,n,k) scanf("%d%d%d",&m,&n,&k)
#define PB push_back
#define MP make_pair
#define PRINTD(n) printf("%d\n",n)
#define PRINTLLD(n) printf("%lld\n",n);
#define DEBUG printf("%s\n","debug")
using namespace std;
//////////////////////////////////////////////////////
//http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_12_A&lang=jp


typedef pair<int,int> P;

typedef pair<int,P> PP;

const int UNIMAX = 1000;
int uni[UNIMAX] = {}; //parent -- size of member , kid -- leader name,def -1

int uniroot(int a){ //return a's leader
	if (uni[a] < 0) return a;
	return uni[a] = uniroot(uni[a]); //uni[a] => 0 , reconnect root directly
}

bool uniconnect(int a,int b){
	a = uniroot(a);
	b = uniroot(b);
	
	if(a==b) return false;
	
	if(uni[a] > uni[b]){
		a ^= b;
		b ^= a;
		a ^= b;
	}
	
	uni[a] = uni[a] + uni[b]; // membersize++
	uni[b] = a;
	return true;
}

bool uniisConnect(int a,int b){
	return (uniroot(a) == uniroot(b));
}

int unisize (int a){
	return (-uni[uniroot(a)]);
}

int unireset() {
	REP(i,UNIMAX){
		uni[i] = -1;
	}
}


int main() {
	int n; SCD(n);
	static int d[UNIMAX][UNIMAX];
	priority_queue<PP,vector<PP>,greater<PP> > Q; //(cost ,(a,b)) a->b
	
	FORq(i,1,n){
		FORq(j,1,n){
			int x; SCD(x);
			d[i][j] = x;
			if ((x != -1) and (i>=j)) {
				Q.push(MP(x,MP(i,j)));
			}
		}
	}
	
	unireset();
	
	int connected = 1;
	int cost = 0;
	
	while((connected <= n) and (!Q.empty())){
		PP now = Q.top(); Q.pop();
		int ncost = now.first;
		int a = now.second.first;
		int b = now.second.second;
		
		if (uniisConnect(a,b) == false) {
			uniconnect(a,b);
			connected++;
			cost += ncost;
		}
		
	}
	
	PRINTD(cost);
	
}

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

static const int MAX = 10000;
static const int INFTY = (1<<21);
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;

int n;
vector<pair<int,int> > adj[MAX];

void dijkstra(){
    priority_queue<pair<int,int> > PQ;
    int color[MAX];
    int d[MAX];
    for(int i=0; i<n; i++){
        d[i] = INFTY;
        color[i] = WHITE;
    }
    
    d[0] = 0;
    PQ.push(make_pair(0,0));
    color[0] = GRAY;
    
    while(!PQ.empty()){
        pair<int,int> f = PQ.top(); PQ.pop();
        int u = f.second;
        color[u] = BLACK;
        
        if(d[u] < f.first*(-1)) continue;
        
        for(int j=0; j<adj[u].size(); j++){
            int v = adj[u][j].first;
            if(color[v]==BLACK) continue;
            if(d[v]>d[u]+adj[u][j].second){
                d[v] = d[u]+adj[u][j].second;
                PQ.push(make_pair(d[v]*(-1),v));
                color[v] = GRAY;
            }
        }
    }
    
    for(int i=0; i<n; i++){
        cout << i << " " << (d[i]==INFTY ? -1 : d[i]) << endl;
    }
}

int main(){
    int k,u,v,c;
    
    cin >> n;
    for(int i=0; i<n; i++){
        cin >> u >> k;
        for(int j=0; j<k; j++){
            cin >> v >> c;
            adj[u].push_back(make_pair(v,c));
        }
    }
    
    dijkstra();
    
    return 0;
}

C++: #include<iostream>

using namespace std;

int main()
{
    int n, A[100][100], B[100] = { 0 };
    int min;
    cin >> n;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            cin >> A[i][j];
            if(A[i][j]!='-1')
            B[i] += A[i][j];
        }
    }
    min = B[0];
    for (int i = 1; i < n; i++) {
        if (min > B[i])min = B[i];
    }
    cout << min << endl;
    return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cassert>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)
#define rep(i,n) FOR(i,0,n)
#define DEBUG(x) cout<<#x<<": "<<x<<endl
#define vint vector<int>
#define vdouble vector<double>
#define vstring vector<string>
using namespace std;
#include <functional>

#include<map>
#include<set>
#include<queue>

typedef long long ll;
typedef unsigned long long ull;

const int MAX_N = 100;

int N;

class Edge{
public:
	int from, to, cost;
	Edge(int from = 0, int to = 0, int cost = 0) :
		from(from), to(to), cost(cost){}
	
	bool operator < (const Edge &e) const{
		return cost < e.cost;
	}
	bool operator > (const Edge &e) const{
		return cost > e.cost;
	}

};

class Node{
public:
	vector<Edge> edges;
	int color;
	Node(){
		color = 0;
	}
};

class Graph{
public:
	vector<Node> nodes;
	Graph(int n) {
		rep(i, n) { Node n;  nodes.push_back(n); }
	}
};

int main() {

	cin >> N;
	Graph g(N);

	rep(i, N)
	   rep(j, N){
		int cost; cin >> cost;
		if (cost != -1){
			Edge e(i, j, cost);
			g.nodes[i].edges.push_back(e);
		}
	}

	rep(i, N) { g.nodes[i].color = 0; }

	int i = 0;
	g.nodes[i].color = 1;

	priority_queue<Edge, vector<Edge>, greater<Edge> > pq;
	for (auto e : g.nodes[i].edges){
		pq.push(e);
	}

	int ret = 0;
	while (!pq.empty()){
		Edge e = pq.top(); pq.pop();
		Node *n = &g.nodes[e.to];
		if (n->color != 0){ continue; }
		
		n->color = 1;
		ret += e.cost;

		for (auto e : n->edges){
			pq.push(e);
		}
	}

	cout << ret << endl;
}