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
	num := 0
	for len(edges) > 0 && num < n-1 {
		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
		num += 1
	}
	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 <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;
}