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++: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define WHITE 0
#define VISITED 1
#define CANDIDATE 2

int n;

int prim(vvi &g) {
  vi d(n, INT_MAX), s(n, WHITE), p(n, -1); 
  d[0] = 0;

  int total = 0;
  while (true) {
    int min_cost = INT_MAX, u;
    rep(i, n) {
      if (s[i] != VISITED && d[i] < min_cost) {
        min_cost = d[i];
        u = i;
      }
    }

    if (min_cost == INT_MAX) break;

    s[u] = VISITED;
    if (p[u] != -1) total += g[p[u]][u];

    rep(v, n) {
      if (s[v] != VISITED && g[u][v] != -1) {
        if (g[u][v] < d[v]) {
          d[v] = g[u][v];
          p[v] = u;
          s[v] = CANDIDATE;
        }
      }
    }
  }

  return total;
}

int main() {
  cin >> n;

  vvi g(n, vi(n, -1));
  rep(i, n) {
    rep(j, n) {
      int c;
      cin >> c;

      g[i][j] = c == -1 ? INT_MAX : c;
    }
  }
  
  int total = prim(g);
  cout << total << endl;
}

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

#define MAX   100
#define INFTY (1 << 21)
#define WHITE 0
#define GRAY  1
#define BLACK 2

int n, M[MAX][MAX];

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

  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 <stdio.h>
#define INF 999999

int n, k;
int color[3000], d[3000], fathernode[30];
int map[3000][3000];

int dijkstra(int s)
{
    for(int i = 0; i < n; i ++)
    {
        d[i] = INF;
        fathernode[i] = -1;
    }
    d[s] = 0;
    while(true)
    {
        int min = INF;
        int u = -1;
        for(int i = 0; i < n; i ++)
        {
            if(min > d[i] && color[i] != 1)
            {
                u = i;
                min = d[i];
            }
        }
        if(u == -1)
            break;
        color[u] = 1;//染色，以防后面重复使用
        for(int i = 0; i < n; i ++)
        {
            if(color[i] != 1 && map[u][i] != INF)
                if(d[i] > d[u] + map[u][i])
                    d[i] = d[u] + map[u][i];
        }
    }
    return 0;
}

int main(void)
{
    int x, y, v;
    scanf("%d", &n);
    for(int i = 0; i < n; i ++)
        for(int j = 0; j < n; j ++)
            map[i][j] = INF;
    for(int i = 0; i < n; i ++)
    {
        scanf("%d%d", &x, &k);
        for(int j = 0; j < k; j ++)
        {
            scanf("%d%d", &y, &v);
            map[x][y] = v;
        }
    }
    dijkstra(0);
    
    for(int i = 0; i < n; i ++)
    {
        if(d[i] != INF)
            printf("%d %d", i, d[i]);
        printf("\n");
    }
    return 0;
}