Go: package main

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

const (
	MAX   = 100
	INFTY = 1 << 21
	NIL   = -1
)

const (
	WHITE = iota
	GRAY
	BLACK
)

type Graph [MAX][MAX]int

func (g *Graph) prim(n int) int {
	var (
		u, minv     = 0, 0
		d, p, color [MAX]int
	)

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

	for {
		minv = INFTY
		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 && g[u][v] != INFTY {
				if d[v] > g[u][v] {
					d[v] = g[u][v]
					p[v] = u
					color[v] = GRAY
				}
			}
		}
	}

	sum := 0
	for i := 0; i < n; i++ {
		if p[i] != -1 {
			sum += g[i][p[i]]
		}
	}

	return sum
}

func answer(reader io.Reader) string {
	sc := bufio.NewScanner(reader)
	sc.Split(bufio.ScanWords)

	sc.Scan()
	n, _ := strconv.Atoi(sc.Text())

	var g Graph
	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			sc.Scan()
			e, _ := strconv.Atoi(sc.Text())
			if e == -1 {
				g[i][j] = INFTY
				continue
			}
			g[i][j] = e
		}
	}

	return fmt.Sprint(g.prim(n))
}

func main() {
	fmt.Println(answer(os.Stdin))
}

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

int n,d[MAX+10],p[MAX+10],color[MAX+10];
int W[MAX+10][MAX+10];

int prim()
{
  int v,k,mini,flag,u=0;
  for(int i=0;i<n;i++)
  {
      d[i]=INT_MAX;
      p[i]=-1;
  }
  d[0]=0;
  while(1)
  {
      mini=INT_MAX;
      flag=1;
      for(int i=0;i<n;i++)
      {
          if(d[i]<mini&&color[i]!=BLACK)
          {
              u=i;
              mini=d[i];
              flag=0;
          }
      }
      if(flag) break;
      color[u]=BLACK;
      for(v=0;v<n;v++)
      {
          if(W[u][v]!=INT_MAX&&color[v]!=BLACK)
          {
            if(d[v]>W[u][v])
            {
              d[v]=W[u][v];//更新当前点到最小生成树的距离
              p[v]=u;//每一轮都需要跑更新前驱结点
            }
          }
      }
  }
  int sum=0;
  for(int i=0;i<n;i++)
  {
      if(p[i]!=-1) sum+=W[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;
         W[i][j]=(e==-1)?INT_MAX:e;
        }
        color[i]=WHITE;
    }
    cout<<prim()<<endl;
}

C++: #include<stdio.h>

#define INFTY 10000
#define black 2
#define gray 1
#define white 0
#define MAX 100

int n,A[MAX][MAX];

int prim()
{
  int i,v,u,min_v;
  int a[MAX],b[MAX],IRO[MAX];

  for(i=0;i<n;i++)
    {
      a[i] = INFTY;
      b[i] = -1;
      IRO[i] = white;
    }

  a[0] = 0;

  while(1)
    {
      min_v = INFTY;
      u = -1;
      for(i=0;i<n;i++)
	{
	  if(min_v > a[i] && IRO[i] != black)
	    {
	      u = i;
	      min_v = a[i];
	    }
	}
      if(u==-1) break;
      IRO[u] = black;
      for(v=0;v<n;v++)
	{
	  if(IRO[v] != black && A[u][v] != INFTY)
	    {
	      if(a[v]>A[u][v])
		{
		  a[v]=A[u][v];
		  b[v] = u;
		  IRO[v] = gray;
		}
	    }
	}
    }
  int sum =0;

  for(i=0;i<n;i++)
    {
      if(b[i] != -1) sum += A[i][b[i]];
    }

  return sum;
}

int main()
{
  int e,i,j;
  scanf("%d",&n);

  for(i=0;i<n;i++)
    {
      for(j=0;j<n;j++)
	{
	  scanf("%d",&e);
	  A[i][j] = (e==-1) ? INFTY : e;
	}
    }
  printf("%d\n",prim());


  return 0;
}

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

const int MAX = 100;
const int INFINITY = 2 << 21;
int n;
int G[MAX][MAX];
int distances[MAX];
int parents[MAX];
int states[MAX];

int prim() {
  int id = 0;
  distances[id] = 0;

  while (1) {
    int min = INFINITY;

    for (int i = 0; i < n; i++) {
      if (states[i] == 0 && distances[i] < min) {
        min = distances[i];
        id = i;
      }
    }

    if (min == INFINITY) break;

    for (int i = 0; i < n; i++) {
      if (G[id][i] == -1) continue;
      if (G[id][i] < distances[i]) {
        distances[i] = G[id][i];
        parents[i] = id;
      }
    }

    states[id] = 1;
  }

  int sum = 0;
  for (int i = 0; i < n; i++) {
    if (parents[i] != -1) sum += distances[i];
  }
  return sum;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> n;

  for (int i = 0; i < n; i++) {
    distances[i] = INFINITY;
    parents[i] = -1;
    states[i] = 0;
    for (int j = 0; j < n; j++) {
      cin >> G[i][j];
    }
  }

  cout << prim() << endl;
  return 0;
}

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

const int N = 100;
int n;
int A[N][N];
priority_queue< pair<int, int> > Q;

int main()
{
	cin >> n;
	for (int i=0; i<n; i++)
	{
		for (int j=0; j<n; j++)
		{
			cin >> A[i][j];
		}
	}

	int mcost = -INT_MAX;

	for (int i=0; i<n; i++)
	{
		bool visit[N] = {0};

		Q.push(pair<int, int>(0,i));

		int cost = 0;

		while (Q.size())
		{
			pair<int, int> q = Q.top();
			Q.pop();
			int k = q.second;
			if (visit[k]) continue;
			cost += q.first;
			visit[k]=true;
			for(int j=0;j<n;j++)
			{
				int c = A[k][j];
				if (c <= 0) continue;
				if (visit[j]) continue;
				Q.push(pair<int, int>(-c,j));
			}
		}

		cout << i << ' ' << -cost << endl;

		if (mcost < cost) mcost = cost;
	}

	cout << -mcost << endl;
		
	return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

#define Vi vector<int>
#define FOR(i,s,e) for (int i=s; i<e; i++)
#define ITER(v) v.begin(), v.end()

constexpr int N_MAX = 100;

int weight[N_MAX][N_MAX];

Vi mst(const int n) {
    bool isrouted[n] = {false};
    Vi route;
    route.push_back(0);
    isrouted[0] = true;
    FOR(t,1,n) {
        int minval = -1;
        int nextnode = -1;
        for (const int i : route)
        FOR(j,0,n) {
            if (isrouted[j]) { continue; }
            const int w = weight[i][j];
            if (w < 0) { continue; }
            if (minval < 0 || minval > w) {
                minval = w;
                nextnode = j;
            }
        }
        route.push_back(nextnode);
        isrouted[nextnode] = true;
    }
    return move(route);
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    int n;
    cin >> n;

    FOR(i,0,n) {
        FOR(j,0,n) {
            cin >> weight[i][j];
        }
    }

    Vi route = mst(n);
    int cost = 0;
    for (const int i : route) {
        if (i == *route.begin()) { continue; }
        cost += weight[i][i-1];
    }
    cout << cost << endl;

    return 0;
}