Go: package main

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

const (
	WHITE int = iota
	GRAY
	BLACK
)

type graph struct {
	G [][]int
	C []int
	D []int
	P []int
}

var INFTY = 1 << 32

func constructGraph() *graph {
	sc := bufio.NewScanner(os.Stdin)
	sc.Scan()
	n, _ := strconv.Atoi(sc.Text())

	g := make([][]int, n)
	c := make([]int, n)
	d := make([]int, n)
	p := make([]int, n)

	for i := 0; i < n; i++ {
		sc.Scan()
		fields := strings.Fields(sc.Text())
		g[i] = toIntSlice(fields)
	}

	return &graph{g, c, d, p}
}

func (g *graph) initMst() {
	for i := 0; i < len(g.G); i++ {
		g.C[i] = WHITE
		g.D[i] = INFTY
	}
	g.D[0] = 0
	g.P[0] = -1
}

func (g *graph) primMst() int {
	g.initMst()

	var u int
	for {
		mincost := INFTY
		for i := 0; i < len(g.G); i++ {
			if g.C[i] != BLACK && g.D[i] < mincost {
				mincost = g.D[i]
				u = i
			}
		}

		if mincost == INFTY {
			break
		}

		g.C[u] = BLACK

		for i := 0; i < len(g.G); i++ {
			if g.C[i] != BLACK && g.existEdge(u, i) {
				if g.G[u][i] < g.D[i] {
					g.D[i] = g.G[u][i]
					g.P[i] = u
				}
			}
		}
	}

	totalCost := 0
	for _, cost := range g.D {
		totalCost += cost
	}
	return totalCost
}

func (g *graph) existEdge(u, v int) bool {
	return g.G[u][v] != INFTY && g.G[v][u] != INFTY
}

func toIntSlice(s []string) []int {
	a := make([]int, len(s))
	for i, v := range s {
		num, _ := strconv.Atoi(v)
		if num == -1 {
			num = INFTY
		}
		a[i] = num
	}
	return a
}

func main() {
	g := constructGraph()
	fmt.Println(g.primMst())
}

C++: #include <iostream>
#include <sstream>
#include <cstdio>
#include <cmath>
#include <cctype>
#include <string>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <functional>

using namespace std;
using ll = long long int;
#define rep(i,n) for(int i = 0; i < n; i++)
#define FOR(i, a, b)  for(int i = (a); i < (b) ; i++)

#define pb push_back
#define SORT(v,n) sort(v, v+n)
#define ALL(x) (x).begin(),(x).end()
#define debug(x) cerr << #x << ": " << x << '\n'
#define elif else if
#define itn ll
#define int ll
const int INF = 100100100;
const int MOD = (int)1e9 + 7;
const double EPS = 1e-9;
///                →,↑,←,↓,↗,↖,↙,↘
int dx[8] = { 1, 0, -1, 0, 1, -1, -1, 1 };
int dy[8] = { 0, 1, 0, -1, 1, 1, -1, -1 };

static const int MAX = 100;
static const int White = 0;
static const int Gray = 1;
static const int Black = 2;

int n, M[MAX][MAX],color[MAX];

int prim() {
	int d[MAX], p[MAX];
	int i,sum = 0;
	rep(i, n) {
		d[i] = INF;
		p[i] = -1;
		color[i] = White;
	}
	d[0] = 0;
	while (true) {
		int u = -1;
		int minv = INF;
		rep(i, n) {
			if (color[i] != Black && d[i] < minv) {
				minv = d[i];
				u = i;
			}
		}
		if (u == -1)
			break;
		color[u] = Black;
		int v;
		rep(v, n) {
			if (color[v] != Black && M[u][v] != INF) {
				color[v] = Gray;
				d[v] = M[u][v];
				p[v] = u;
			}
		}
	}
	rep(i, n) {
		if (p[i] != -1) {
			sum += M[i][p[i]];
		}
	}
	return sum;
}

signed main() {
	ios::sync_with_stdio(false);
	cin >> n;
	int i,j;
	rep(i, n) {
		rep(j, n) {
			int a;
			cin >> a;
			M[i][j] = (a == -1) ? INF : a;
		}
	}
	cout << prim() << '\n';
	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 <bits/stdc++.h>
#define INF 2001
using namespace std;
int M[100][100],d[100],p[100];
bool vist[100]={0};
void prim(int n){
	for(int i=0;i<n;i++) d[i] = INF;
	d[0] = 0;
	p[0] = -1;
	int mincost, u;
	while(1){
		mincost = INF;
		for(int i=0;i<n;i++)
			if(!vist[i] && d[i]<mincost)
				mincost = d[i], u = i;
		if(mincost == INF) break;
		vist[u] = 1;
		for(int i=0; i<n; i++)
			if(!vist[i] && M[u][i]!=-1 && M[u][i]<d[i])
				d[i] = M[u][i],
				p[i] = u;
	}
}
int main()
{	int n; cin>>n;
	for(int i = 0;i < n;i++)
		for(int j = 0;j < n;j++)
			scanf("%d",&M[i][j]);
	prim(n);
	long long ans = 0;
	for(int i=1; i<n; i++)
		ans += M[i][p[i]];
	cout << ans << endl;
	return 0;
}

C++: #include <iostream>
#include<vector>
#include<algorithm>
#include<numeric>
using namespace std;
int inf=(1<<12);


int main(void){
    int n,u;
int total=0;
int m[100][100];
int d[100];

    cin>>n;
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            int k;
            cin>>k;
            if(k==-1){
                m[i][j]=inf;
            }
            else{
                m[i][j]=k;
            }
            d[j]=inf;
        }
    }
    
    d[0]=0;
    while(1){
        u=-1;
        int min=inf;
        for(int i=0;i<n;i++){
            if(min>d[i]&&d[i]!=-1){
                min=d[i];
                u=i;
            }
        }
        if(u==-1)break;
        total+=min;
        d[u]=-1;
        for(int i=0;i<n;i++){
            if(d[i]!=-1&&d[i]>m[u][i]){
                d[i]=m[u][i];
            }
        }
    }
    cout<<total<<endl;
    return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;

#define MAX 100
#define NOT -1
#define FND -2
#define CMP -3

int n, route;
int G[MAX+1][MAX+1];
int d[MAX+1][2];
int p[MAX+1];

void make_graph(void){
  scanf("%d", &n);

  for(int i=0; i<n; i++){
    for(int j=0; j<n; j++){
      scanf("%d", &G[i][j]);
      if(G[i][j] == -1) G[i][j]=INT_MAX;
    }
  }
}

void prim(void){
  int u, m, mst=0;

  for(int i=0; i<n; i++){
    d[i][0]=INT_MAX;
    d[i][1]=0;
    p[i]=-1;
  }
  
  d[0][0]=0; d[0][1]=FND;
  
  while(true){
    u=-1;
    m=INT_MAX;
    
    for(int i=0; i<n; i++){
      if(d[i][0] < m && d[i][1] != CMP){
	u=i; m=d[i][0];
      }
    }

    if(u == -1) break;
    d[u][1]=CMP;

    for(int i=0; i<n; i++){
      if(d[i][1] != CMP && G[u][i] != INT_MAX){
	if(d[i][0] > G[u][i]){
	  d[i][0]=G[u][i];
	  d[i][1]=FND;
	  p[i]=u;
	}
      }
    }

  }

  for(int i=0; i<n; i++){
    if(p[i] != -1) mst+=G[i][p[i]];
  }

  printf("%d\n", mst);
}

int main(void){
  make_graph();
  prim();
  
  return 0;
}

C++: //最少全域木
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int G[103][103];
int V;
int NG[103][103];//新しくできるほう
int used[103];
int main(){
	int ans=0;
	cin>>V;
	for(int i=0;i<V;i++){
		for(int j=0;j<V;j++){
		cin>>G[i][j];
		}
	}
	for(int i=0;i<103;i++)for(int j=0;j<103;j++)NG[i][j]=-1;
	used[0]=1;
	for(int i=0;i<V;i++){
		int u=-1,a,b;
		for(int j=0;j<V;j++){
		if(used[j]==0)u=i;
		}
		if(u==-1)break;
		int now=99999999;
		for(int j=0;j<V;j++){
			for(int k=0;k<V;k++){
			if(now>G[j][k]&&G[j][k]!=-1&&used[j]==1&&used[k]==0){
			now=G[j][k];
			a=j,b=k;
			}
			}
		}
		ans+=G[a][b];
		used[b]=1;
		NG[a][b]=G[a][b];
	}
	cout<<ans<<endl;
	return 0;
}