Go: package main

import (
	"fmt"
	"sort"
)

type Edge struct {
	I int
	J int
	A int
}

func main() {
	var n int
	fmt.Scan(&n)

	g := make([][]int, n)
	edges := make([]Edge, 0, n*(n-1)/2)
	parents := make([]int, n)
	for i := 0; i < n; i++ {
		g[i] = make([]int, n)
		for j := 0; j < n; j++ {
			fmt.Scan(&g[i][j])
			if g[i][j] > 0 {
				edges = append(edges, Edge{i, j, g[i][j]})
			}
		}
		parents[i] = i
	}
	sort.SliceStable(edges, func(i, j int) bool { return edges[i].A > edges[j].A })

	sum := 0
	for len(edges) > 0 {
		edge := edges[len(edges)-1]
		edges = edges[:len(edges)-1]

		if same(edge.I, edge.J, &parents) {
			continue
		}
		union(edge.I, edge.J, &parents)
		sum += edge.A
	}
	fmt.Println(sum)
}



func root(id int, parents *[]int) int {
	if id == (*parents)[id] {
		return id
	}
	return root((*parents)[id], parents)
}

func union(i, j int, parents *[]int) {
	(*parents)[root(j, parents)] = (*parents)[root(i, parents)]
}

func same(i, j int, parents *[]int) bool {
	return root(i, parents) == root(j, parents)
}

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