Go: package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 500001), 500000)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func main() {
	fp := os.Stdin
	if len(os.Args) > 1 {
		fp, _ = os.Open(os.Args[1])
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(os.Stdout)

	n := getNextInt(scanner)

	g := make([][]int, n)
	ans := make([][]int, n)

	inf := 1 << 30
	for i := 0; i < n; i++ {
		g[i] = make([]int, n)
		ans[i] = make([]int, n)
		for ii := 0; ii < n; ii++ {
			g[i][ii] = getNextInt(scanner)
			if g[i][ii] == -1 {
				g[i][ii] = inf
			}
			ans[i][ii] = -1
		}
	}

	im := make(IntMap, 1)

	im.increment(1)

	for len(im) < n {
		min := inf
		u := -1
		v := -1
		for i := range im {
			for ii := 0; ii < n; ii++ {
				if im.get(ii) != 0 {
					continue
				}
				if min > g[i][ii] {
					min = g[i][ii]
					u = i
					v = ii
				}
			}
		}
		im.increment(v)
		ans[u][v] = min
		ans[v][u] = min
	}

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

	fmt.Fprintln(writer, sum)
	writer.Flush()
}

// IntMap ...
type IntMap map[int]int

func (m IntMap) get(key int) int {
	if _, ok := m[key]; ok {
		return m[key]
	}
	return 0
}
func (m IntMap) increment(key int) {
	if _, ok := m[key]; ok == false {
		m[key] = 0
	}
	m[key]++
}

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