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 <iostream>
#include<vector>
#include<stack>
#include<cstdio>
using namespace std;

#define MAX (1<<21)

int n,M[101][101];
int vis[101],d[101],p[101];

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


int main(){
    int i,j,k,m;
    cin >> n;
    for( i = 0; i < n; i++ ){
        for( j = 0; j < n; j++ ){
            cin >> k;
            M[i][j] = (k == -1) ? MAX : k;
        }
    }
    cout << prim() << endl;
}

C++: #include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <vector>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <functional>
  
#define mp       make_pair
#define pb       push_back
#define all(x)   (x).begin(),(x).end()
#define rep(i,n) for(int i=0;i<(n);i++)
  
using namespace std;
  
typedef    long long          ll;
typedef    unsigned long long ull;
typedef    vector<bool>       vb;
typedef    vector<int>        vi;
typedef    vector<vb>         vvb;
typedef    vector<vi>         vvi;
typedef    pair<int,int>      pii;
  
const int INF=1<<29;
const double EPS=1e-9;
  
const int dx[]={1,0,-1,0,1,1,-1,-1},dy[]={0,-1,0,1,1,-1,-1,1};

class UnionFind {
public:
    int N;
    std::vector<int> parent;
    UnionFind(int n) : N(n), parent(N, -1) {
    }
    void Union(int a, int b) {
        a = root(a);
        b = root(b);
        int sizea = size(a);
        int sizeb = size(b);
        if (sizea > sizeb) {
            int temp = a;
            a = b;
            b = temp;
        }
        parent[b] += parent[a];
        parent[a] = b;
    }
    int root(int a) {
        if (parent[a] < 0) return a;
        return parent[a] = root(parent[a]);
    }
    int size(int a) {
        return -parent[root(a)];
    }
    bool Find(int a, int b) {
        return root(a) == root(b);
    }
};

struct edge {
    int a;
    int b;
    int c;
    bool operator<(const edge &rhs) const {
        return c < rhs.c;
    }
    edge(int a_, int b_, int c_) : a(a_), b(b_), c(c_) {

    }
};

int main() {
    int N;
    cin >> N;

    UnionFind uf(N);
    std::vector<edge> e;
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            int c;
            cin >> c;
            if (c == -1) continue;
            e.push_back((edge){i, j, c});
        }
    }

    sort(e.begin(), e.end());

    int ans = 0;
    for (int i = 0; i < e.size(); i++) {
        if (!uf.Find(e[i].a, e[i].b)) {
            uf.Union(e[i].a, e[i].b);
            ans += e[i].c;
        }
    }
    cout << ans << endl;
    return 0;
}

C++: #include<iostream>
#include<climits>
#include<vector>
#include<list>
#include<functional>
#include<algorithm>
#include<string>
#include<cmath>
#include<complex>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<deque>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) for(int i=0;i<(n);i++)
#define INF (int)1e9
#define INFL (ll)1e11
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int prim(vector<vector<int>> a) {
	int n = a.size();
	priority_queue<P, vector<P>, greater<P>> pq;
	pq.push(P(0, 0));
	set<int> mst;
	int res = 0;
	while (mst.size() < n) {
		int s = pq.top().second;
		res += pq.top().first;
		pq.pop();
		mst.insert(s);
		REP(j, n) {
			if (a[s][j] >= 0 && mst.find(j) == mst.end()) {
				pq.push(P(a[s][j], j));
			}
		}
	}
	return res;
}

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);

	int n;
	cin >> n;
	vector<vector<int>> a(n, vector<int>(n));
	REP(i, n)REP(j, n) cin >> a[i][j];

	cout << prim(a) << "\n";
	
	return 0;
}

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][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;
}