Go: package main

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

const (
	MAX = 100
	INF = 1 << 21
	WHITE = 0
	GRAY = 1
	BLACK = 2
)

var (
	n int
	M [MAX][MAX]int
)

func prim() int {
	var u, minv int
	var d [MAX]int
	var p [MAX]int
	var color [MAX]int

	for i := 0; i < n; i++ {
		d[i] = INF
		p[i] = -1
		color[i] = WHITE
	}
	d[0] = 0
	for {
		minv = INF
		u = -1
		for 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 v := 0; v < n; v++ {
			if color[v] != BLACK && M[u][v] != INF {
				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() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Scan()
	n, _ = strconv.Atoi(scanner.Text())
	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			scanner.Scan()
			e, _ := strconv.Atoi(scanner.Text())
			if e == -1 {
				M[i][j] = INF
			} else {
				M[i][j] = e
			}
		}
	}
	fmt.Println(prim())
}

C++: /*
    与えられた重み付きグラフ G=(V,E) に対する最小全域木の辺の重みの総和を計算するプログラムを作成してください。

    入力
    最初の行に G の頂点数 n が与えられます。続く n 行で G を表す n×n の隣接行列 A が与えられます。A の要素 aij は、頂点 i と頂点 j を結ぶ辺の重みを表します。ただし、辺がなければ-1 で示されます。

    出力
    G の最小全域木の辺の重みの総和を１行に出力してください。

    制約
    1≤n≤100
    0≤aij≤2,000 (aij≠−1 のとき)
    aij=aji
    グラフ G は連結である。
*/

#include <iostream>
#include <cstdio>
#include <climits>
#include <cstring>

using namespace std;

#define SWAP(type, a, b) do { type t = a; a = b; b = t; } while(0)

typedef struct {
    int weight;
    int to;
} Edge;

typedef struct {
    Edge *edge;
    int num;
} Heap;

void makeMinHeap(Heap *heap, int idx);
void insert(Heap *heap, int key);
int extract(Heap *heap);

static inline int getParent(int idx) { return idx / 2; }
static inline int getLeft(int idx) { return idx * 2; }
static inline int getRight(int idx) { return idx * 2 + 1; }
static inline bool isCorrectHeapIdx(int idx, int heapSize) { return idx <= heapSize && 1 <= idx; }
static inline bool isEmpty(Heap *heap) { return heap->num <= 0; }

void makeMinHeap(Heap *heap, int idx) {
    int left = getLeft(idx);
    int right = getRight(idx);

    int minIdx = idx;
    if(isCorrectHeapIdx(left, heap->num) && heap->edge[minIdx].weight > heap->edge[left].weight) {
        minIdx = left;
    } if(isCorrectHeapIdx(right, heap->num) && heap->edge[minIdx].weight > heap->edge[right].weight) {
        minIdx = right;
    }

    if(minIdx != idx) {
        SWAP(Edge, heap->edge[minIdx], heap->edge[idx]);
        makeMinHeap(heap, minIdx);
    }
}

void insert(Heap *heap, Edge* key) {
    heap->edge[++heap->num] = *key;
    for(int i = getParent(heap->num); i >= 1; i = getParent(i)) {
        makeMinHeap(heap, i);
    }
}

void extract(Heap *heap, Edge* key) {
    *key = heap->edge[1];
    heap->edge[1] = heap->edge[heap->num--];
    makeMinHeap(heap, 1);
}

int n;
int* weight;

static inline bool isConnected(int s, int t) {
    return weight[s * n + t] != INT_MAX;
}

static inline int getWeight(int s, int t) {
    return weight[s * n + t];
}

int getMimimumSpanningTree() {
    bool* isAddMst = new bool[n]();
    Heap connectEdge;
    Edge edge;
    int sum = 0;
    int v = 0;

    connectEdge.edge = new Edge[n * n];
    connectEdge.num = 0;
    
    while(1) {
        isAddMst[v] = true;

        for(int i = 0; i < n; i++) {
            if(isConnected(v, i) && !isAddMst[i]) {
                edge.weight = getWeight(v, i);
                edge.to = i;
                insert(&connectEdge, &edge);
            }
        }

        while(1) {
            if(isEmpty(&connectEdge)) {
                return sum;
            }

            extract(&connectEdge, &edge);

            if(!isAddMst[edge.to]) {
                break;
            }
        }

        v = edge.to;
        sum += edge.weight;
    }

    return sum;
}

int main() {

    scanf("%d", &n);

    weight = new int[n * n];
    
    for(int i = 0; i < n; i++) {
        for(int j = 0; j < n; j++) {
            int w;
            scanf("%d", &w);
            weight[i * n + j] = (w == -1) ? INT_MAX : w;
        }
    }

    cout << getMimimumSpanningTree() << endl;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repr(i, n) for (int i = (int)(n); i >= 0; i--)
#define REP(i, m, n) for (int i = (int)(m); i <= (int)(n); i++)
#define all(v) v.begin(), v.end()
typedef long long ll;
typedef pair<int,int> pii;
#define fi first
#define se second
#define mp make_pair

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

//const long long INF = 1LL << 60;
const int INF = 1001001001;
const int MAX = 110;

//struct edge{
//  int to, cost;
//  edge(){}
//  edge(int to, int cost): to(to), cost(cost){}
//};

int G[MAX][MAX];
bool seen[MAX];
priority_queue<pii, vector<pii>, greater<pii> > pq;
int n;

int prim(){
  seen[0] = true;
  rep(i, n){
    if(G[0][i] == -1) continue;
    pq.push(mp(G[0][i], i));
  }
  
  int sum = 0;
  while(!pq.empty()){
    pii p = pq.top();
    pq.pop();
    if(seen[p.se]) continue;
    seen[p.se] = true;
    sum += p.fi;
    rep(i, n){
      if(G[p.se][i] == -1) continue;
      pq.push(mp(G[p.se][i], i));
    }
  }

  return sum;
}

int main(){
  cin >> n;
  rep(i, n) rep(j, n) cin >> G[i][j];

  cout << prim() << endl;
 
  return 0;
}

C++: 

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<stack>
#include<queue>
#include<vector>
#include<algorithm>
#include<string>
#include<iostream>
using namespace std;
typedef long long ll;
int mod(ll a){
    int c=a%1000000007;
    if(c>=0)return c;
    else return c+1000000007;
}
typedef pair<int,int> i_i;

#define inf 1<<21
/*
int map[5][5];
int p[5];
int d[5];
bool color[5];

*/
int main(){
    int n;cin>>n;
    int map[n][n];int p[n];int d[n];bool color[n];
    for(int i=0;i<=n-1;i++)for(int j=0;j<=n-1;j++){
        int k;cin>>k;
        map[i][j]=k==-1?inf:k;
    }
    for(int i=0;i<=n-1;i++)p[i]=-1;
    for(int i=0;i<=n-1;i++)d[i]=inf;
    for(int i=0;i<=n-1;i++)color[i]=false;
    
    /*color[0]=true;*/
    d[0]=0;
    
    while(1){
        int mincost=inf;
        int u=-1;
        for(int i=0;i<=n-1;i++){
            if(color[i]==false&&mincost>d[i]){
                mincost=d[i];
                u=i;
            }
        }
        if(u==-1)break;
        
        color[u]=true;
        for(int i=0;i<=n-1;i++){
            if(color[i]==false&&d[i]>map[u][i]){
                d[i]=map[u][i];
                p[i]=u;
            }
        }
        
    }
    ll sum=0;
    for(int i=1;i<=n-1;i++){
        sum+=map[i][p[i]];
    }
    cout<<sum<<endl;
    return 0;
}

C++: #include <stdio.h>

#define MAX_N (105)
#define INF (10000)
int G[MAX_N][MAX_N];
int N;

int dist[MAX_N];
bool arrived[MAX_N];

int prim()
{
    int ret = 0;
    for ( int i = 0; i < N; i++ ) {
	arrived[i] = false;
	dist[i] = INF;
    }

    dist[0] = 0;

    while ( true ) {
	int min_dist = INF;
	int u = 0;
	for ( int i = 0; i < N; i++ ) {
	    if ( !arrived[i] && dist[i] < min_dist ) {
		min_dist = dist[i];
		u = i;
	    }
	}
	
	if ( min_dist == INF ) break;

	ret += dist[u];
	arrived[u] = true;

	for ( int i = 0; i < N; i++ ) {
	    if ( !arrived[i] && (G[u][i] != -1) && (dist[i] > G[u][i]) ) {
		dist[i] = G[u][i];
	    }
	}
    }

    return ret;
}

int main()
{
    int ans = 0;
    scanf("%d", &N);

    for ( int i = 0; i < N; i++ ) {
	for ( int j = 0; j < N; j++ ) {
	    scanf("%d", &G[i][j]);
	}
    }

    ans = prim();

    printf("%d\n", ans);

    return 0;
}