Go: package main

import (
	"bufio"
	"container/heap"
	"fmt"
	"os"
	"strconv"
)

type Node struct {
	id     int
	weight int
}

func init() {
	sc.Split(bufio.ScanWords)
}

type NodeHeap []Node

func (h NodeHeap) Len() int           { return len(h) }
func (h NodeHeap) Less(i, j int) bool { return h[i].weight < h[j].weight }
func (h NodeHeap) Swap(i, j int)      { h[i], h[j] = h[j], h[i] }

func (h *NodeHeap) Push(x interface{}) {
	*h = append(*h, x.(Node))
}

func (h *NodeHeap) Pop() interface{} {
	old := *h
	n := len(old)
	x := old[n-1]
	*h = old[0 : n-1]
	return x
}

var sc = bufio.NewScanner(os.Stdin)

func scanInt() int {
	num, _ := strconv.Atoi(scanString())
	return num
}

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

func init() {
	sc.Split(bufio.ScanWords)
}

func main() {
	n := scanInt()

	list := make([][]int, n)
	seen := make([]bool, n)
	for i := range list {
		list[i] = make([]int, n)
		for j := range list[i] {
			list[i][j] = scanInt()
		}
	}

	start := Node{id: 0, weight: 0}
	h := &NodeHeap{start}
	heap.Init(h)

	sum := 0
	for h.Len() > 0 {
		node := heap.Pop(h).(Node)
		if seen[node.id] {
			continue
		}
		seen[node.id] = true
		sum += node.weight

		for i, w := range list[node.id] {
			if w < 0 {
				continue
			}
			heap.Push(h, Node{id: i, weight: w})
		}
	}

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