Go: package main

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

//util
var sc = bufio.NewScanner(os.Stdin)

func nextStr() string {
	sc.Scan()
	return sc.Text()
}

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func printList(list []int) {
	n := len(list)
	buf := bufio.NewWriter(os.Stdout)
	for _, v := range list[0 : n-1] {
		buf.Write([]byte(fmt.Sprintf("%d ", v)))
	}
	buf.Write([]byte(fmt.Sprintf("%d\n", list[n-1])))
	buf.Flush()
}

func max(a, b int) int {
	if a > b {
		return a
	} else {
		return b
	}
}

func min(a, b int) int {
	if a > b {
		return b
	} else {
		return a
	}
}

//solve
const (
	MAX   = 100
	INFTY = (1 << 21)
	WHITE = 0
	GRAY  = 1
	BLACK = 2
)

//V: グラフの頂点全体の集合
//T: MSTに属する頂点の集合
var (
	n     int
	M     [MAX][MAX]int //重みを記録する隣接行列
	color [MAX]int
	p     [MAX]int //MSTにおける頂点の親を記録
	d     [MAX]int //Tに属する頂点と、V−Tに属する頂点を繋ぐ辺の中で、重みが最小のものを記録
)

func prim() int {
	var u, minv int
	//初期化
	for i := 0; i < n; i++ {
		d[i] = INFTY
		p[i] = -1
		color[i] = WHITE
	}

	d[0] = 0

	for {
		//頂点uを決めていく
		//つまり、Tに属する頂点と、V−Tに属する頂点を繋ぐ辺の中で、重みが最小のものを決める
		minv = INFTY
		u = -1
		for i := 0; i < n; i++ {
			//未訪問かつ、辺の重みが最小となるuを選ぶ
			if minv > d[i] && color[i] != BLACK {
				u = i
				minv = d[i]
			}
		}

		if u == -1 {
			break
		}

		color[u] = BLACK
		for 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
				}
			}
		}
	}

	sum := 0
	for i := 0; i < n; i++ {
		if p[i] != -1 {
			sum += M[i][p[i]]
		}
	}
	return sum
}

func main() {
	sc.Split(bufio.ScanWords)
	n = nextInt()

	for i := 0; i < n; i++ {

		for j := 0; j < n; j++ {
			tmp := nextInt()
			if tmp == -1 {
				tmp = INFTY
			}
			M[i][j] = tmp
		}

	}

	fmt.Println(prim())

}

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