Go: package main

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

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

const MAX = 100
const WHITE = 0
const GRAY = 1
const BLACK = 2

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Scan()
	n, _ = strconv.Atoi(sc.Text())

	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			sc.Scan()
			c, _ := strconv.Atoi(sc.Text())
			if c != -1 {
				M[i][j] = c
			} else {
				M[i][j] = math.MaxInt64
			}

		}
	}
	prim()
}

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

	for i := 0; i < n; i++ {
		d[i] = math.MaxInt64
		p[i] = -1
		color[i] = WHITE
	}

	d[0] = 0

	for {
		minv = math.MaxInt64
		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] != math.MaxInt64 {
				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]]
		}
	}
	fmt.Println(sum)

}

C++: #include <bits/stdc++.h>
using namespace std;

const int MAX=105;
int state[MAX],N,S[MAX][MAX];
priority_queue<pair<int,int>> Q;

int prim(){
    int sum=0,cnt=1;
    for(int i=0;i<N;i++){
        state[i]=0;
    }
    state[0]=1;
    for(int i=0;i<N;i++){
        if(S[0][i]!=10000) Q.push(make_pair(-S[0][i],i));
    }
    while(1){
        int a=Q.top().second;
        if(state[a]==0){
        sum+=(-Q.top().first);
        Q.pop();
        state[a]=1;
        cnt++;
        for(int i=0;i<N;i++){
            if(S[a][i]!=10000000&&state[i]==0) Q.push(make_pair(-S[a][i],i));
        }
        }
        else Q.pop();
        if(cnt==N) break;
    }
    return sum;
}


int main(){

    cin>>N;
    for(int i=0;i<N;i++){
        for(int j=0;j<N;j++){
            int a;cin>>a;
            if(a==-1) S[i][j]=10000000;
            else S[i][j]=a;
        }
    }
    cout<<prim()<<endl;
}

C++: #include<iostream>
#include<vector>
#include<queue>
using namespace std;
enum COLOR{WHITE, BLACK};

struct Edge{
    int w;
    int i;
    int j;
    Edge(int w_, int i_, int j_) : w(w_), i(i_), j(j_) {}
    bool operator< (const Edge& right) const{
        return w > right.w;
    }
};

int main(){
    int n;
    cin >> n;
    vector<vector<int> > G(n, vector<int>(n));
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            cin >> G[i][j];
            if (G[i][j] == -1) G[i][j] = 1e8;
        }
    }
    vector<COLOR> state(n, WHITE);
    priority_queue<Edge> prim;
    state[0] = BLACK;
    int sum = 0;
    int idx_new_gray = 0;
    for (int i = 1; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (state[j] != WHITE) continue;
            prim.push(Edge(G[idx_new_gray][j], idx_new_gray, j));
        }
        while (state[prim.top().j] == BLACK) prim.pop();
        sum += G[prim.top().i][prim.top().j];
        state[prim.top().j] = BLACK;
        idx_new_gray = prim.top().j;
        prim.pop();
    }
    cout << sum << endl;
    return 0;
}

C++: #include<iostream>
using namespace std;
static const int MAX = 100;
static const int INFTY = (1<<21);
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;

int n,M[MAX][MAX];
int prim(){
  int u,minv;
  int d[MAX],p[MAX],color[MAX];
  for(int i=0;i<n;i++){
    d[i]=INFTY;
    p[i]=-1;
    color[i]=WHITE;
  }
  d[0]=0;
  while(1){
    minv=INFTY;
    u=-1;
    for(int 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(int 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;
      }
    }
  }
  }
  int sum=0;
  for(int i=0;i<n;i++){
    if(p[i] != -1) sum += M[i][p[i]];
  }
  return sum;
}
int main(){
  cin>>n;
  for(int i=0;i<n;i++){
    for(int j=0;j<n;j++){
      int e; cin>>e;
      M[i][j] = (e==-1) ? INFTY : e;
    }
  }
  cout << prim() << endl;
  return 0;
}

C++: //
//  ALDS_12_A.cpp
//  
//
//  Created by 杵鞭俊 on 2018/07/24.
//

#include <iostream>
using namespace std;
static const int MAX=100;
static const int INFTY=(1<<21);
static const int WHITE=0;
static const int GRAY=1;
static const int BLACK=2;

int n,M[MAX][MAX];

int prim(){
    int u,minv;
    int d[MAX],p[MAX],color[MAX];
    
    for(int i=0;i<n;i++){
        d[i]=INFTY;
        p[i]=-1;
        color[i]=WHITE;
    }
    d[0]=0;
    while (1) {
        minv=INFTY;
        u=-1;
        for(int 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 (int 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;
                }
            }
        }
    }
    
    int sum=0;
    for (int i=0; i<n; i++) {
        if(p[i]!=-1) sum+=M[i][p[i]];
    }
    return sum;
}


int main(){
    cin >> n;
    for (int i=0; i<n; i++) {
        for (int j=0; j<n; j++) {
            int e; cin>> e;
            M[i][j]=(e==-1)?INFTY:e;
        }
    }
    
    cout << prim()<<endl;
    return 0;
}

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

static const int MAX = 100;
static const int UNVISITED = 0;
static const int VISITING = 1;
static const int VISITED = 2;

static const int INFTY = (1 << 21);


int n, M[MAX][MAX];

int prim()
{
	int d[MAX], p[MAX];
    
	for(int i = 0;i < n;i++)
	{
	  d[i]=INFTY; 
	  p[i]=0;
	}
	
	d[0]=-1;//加入第一个点，-1表示该点在集合U中，否则在集合V中
	int num = 0,ans = 0, e = 0;//e为最新加入集合的点
    while(num < n-1)//加入m-1条边
   {
	int micost=INFTY,miedge=-1;
	for(int i=0;i<n;i++)
		if(d[i]!=-1)
		{
			int temp=M[e][i];
			if(temp<d[i])
			{
				d[i]=temp;
				p[i]=e;
			}
			if(d[i]<micost)
				micost=d[miedge=i];
		}

	ans+=micost;
	d[e=miedge]=-1;
	num++;
	}
	return ans;
}

int main() {
   cin >> n;

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

   cout << prim() << endl;

   return 0;
}