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 <iostream>

using namespace std;
#define MAX 100
#define NIL (1<<21)
#define WHITE 0
#define GRAY 1
#define BLACK 2
int G[MAX][MAX];//图的邻接矩阵
int color[MAX];//节点状态
int n;
int d[MAX];//最小权值
int p[MAX];//父亲节点
void prim(){
    for(int i=0;i<n;i++){
        color[i] = WHITE;
        d[i] = NIL;
        p[i] = -1;
    }
    d[0] = 0;
    while(1){
        int u;
        int mincost = NIL;
        for(int i=0;i<n;i++){//循环找到下一个要加入到T中的
            if(color[i]!=BLACK&&d[i]<mincost){
                mincost = d[i];
                u = i;
            }
        }
        if(mincost==NIL){
            break;
        }
        color[u] = BLACK;
        for(int v=0;v<n;v++){//给相邻顶点的d赋值（与之前的相比较，找到最小的）
            if(color[v]!=BLACK&&G[u][v]!=-1){
                if(G[u][v]<d[v]){
                    d[v] = G[u][v];
                    p[v] = u;
                    color[v] = GRAY;
                }
            }
        }
    }


}
int main()
{
    cin>>n;
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            cin>>G[i][j];
        }
    }
    prim();
    int sum = 0;
    for(int i=0;i<n;i++){
        sum += d[i];
    }
    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++: #include <iostream>
using namespace std;

#define INF 1000000

int main(){
  long n;
  long G[100][100],d[100];
  long minc,mi,i;
  char flgs[i];
  long suma=0;
  
  cin >> n;
  for (i=0;i<n;i++){
    for (long j=0;j<n;j++){
      cin >> G[i][j];
      if (G[i][j]==-1) G[i][j]=INF;
    }
    d[i]=INF;
    flgs[i]=0;
  }

  d[0]=0;
  while (true){
    minc=INF;
    for (i=0;i<n;i++){
      if (flgs[i]!=1&&d[i]<minc){
	minc=d[i];
	mi=i;
      }
    }
    if (minc==INF) break;
    flgs[mi]=1;
    //for (i=0;i<n;i++)cout << flgs[i] << " ";
    for (i=0;i<n;i++){
      if (G[mi][i]==INF)continue;
      if (flgs[i]!=1&&d[i]>G[mi][i]){
	d[i]=G[mi][i];
	//cout << d[i];
      }
    }
  }
  
  for (i=0;i<n;i++){
    //cout << d[i]<<endl;
    suma+=d[i];
  }
  cout << suma<<endl;
  
  return 0;
}

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

const int inf=0x3f3f3f3f;
int G[110][110];
int d[110];
bool vis[110];
int n;

void Dijsktra(){
    d[0]=0;
    for(int i=0;i<n;i++){
        int u=-1,minn=inf;
        for(int j=0;j<n;j++){
           // if(d[j]<minn&&vis[j]==false){
           if(d[u]<minn&&vis[u]==false){
                u=j;
                minn=d[j];
            }
        }
        if(u==-1)
            return ;
        vis[u]=true;
        for(int v=0;v<n;v++){
            if(vis[v]==false&&G[u][v]!=-1&&d[u]+G[u][v]<d[v]){
                d[v]=d[u]+G[u][v];
            }
        }
    }
}

int prim(){
    d[0]=0;
    int ans=0;
    for(int i=0;i<n;i++){
        int u=-1,minn=inf;
        for(int j=0;j<n;j++){
           // if(d[j]<minn&&vis[j]==false){
           if(d[j]<minn&&vis[j]==false){
                u=j;
                minn=d[j];
            }
        }
        if(u==-1)
            return -1;
        vis[u]=true;
        ans+=d[u];
        for(int v=0;v<n;v++){
            if(vis[v]==false&&G[u][v]!=-1&&G[u][v]<d[v]){
                d[v]=G[u][v];
            }
        }
    }
    return ans;
}

int main(){
    //fill(G[0],G[0]+110*110,inf);
    scanf("%d",&n);
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            scanf("%d",&G[i][j]);
        }
    }
    //memset(d,0,sizeof(d));
    memset(vis,0,sizeof(vis));
    fill(d,d+110,inf);
    printf("%d\n",prim());

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