Go: package main

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

const R = 100000000

var sc = bufio.NewScanner(os.Stdin)

var n int
var A [101][101]int

func nextInt() int {
	sc.Scan()
	i, _ := strconv.Atoi(sc.Text())
	return i
}
func prim() int {
	var u, Minimum, sum int
	var d, p, color [101]int

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

	for {
		Minimum = R
		u = -1
		for i := 0; i < n; i++ {
			if Minimum > d[i] && color[i] != 2 {
				u = i
				Minimum = d[i]
			}
		}
		if u == -1 {
			break
		}
		color[u] = 2
		for j := 0; j < n; j++ {
			if color[j] != 2 && A[u][j] != R {
				if d[j] > A[u][j] {
					d[j] = A[u][j]
					p[j] = u
					color[j] = 1
				}
			}
		}
	}

	for i := 0; i < n; i++ {
		if p[i] != -1 {
			sum += A[i][p[i]]
		}
	}
	return sum
}
func main() {
	sc.Split(bufio.ScanWords)
	n = nextInt()

	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			e := nextInt()
			if e == -1 {
				A[i][j] = R
			} else {
				A[i][j] = e
			}
		}
	}
	fmt.Println(prim())
}

C++: #include<cstdio>
#include<iostream>
#include<algorithm>
#define Inf 1<<21
#define Lens 105

using namespace std;
int N;
int w[Lens][Lens], p[Lens], d[Lens];
char c[Lens];

int prime()
{
	d[0] = 0;
	int num = 0;
	while (true)
	{
		int minv = Inf;
		int u = -1;
		for (int i = 0; i < N; i++)
		{
			if (minv > d[i] && c[i] == 'w')
			{
				minv = d[i];
				u = i;
			}
		}
		if (u == -1) break;
		c[u] = 'b';
		for (int i = 0; i < N; i++)
		{
			if (c[i] == 'w'&&w[u][i] != -1)
				if (d[i] > w[u][i])
				{
					d[i] = w[u][i];
					p[i] = u;
				}
		}
	}
	int sum = 0;
	for (int i = 1; i < N; i++)
	{
		sum = sum + w[i][p[i]];
	}
	return sum;
}

int main()
{
	cin >> N;
	for (int i = 0; i < N; i++)
	{
		c[i] = 'w';
		d[i] = Inf;
		p[i] = -1;
		for (int j = 0; j < N; j++)
		{
			int k;
			cin >> k;
			w[i][j] = (k == -1) ? Inf : k;
		}
	}
	cout << prime() << endl;
	return 0;
}

C++: #include <bits/stdc++.h>
#define nMAX 100
#define MAX 2000
using namespace std;
int n, res=0;
vector<vector<int>> M;
vector<int> now;

int buildT()
{
    int tryn, min, d[nMAX], p[nMAX], pass[nMAX];
    for(int i=0; i<n; i++)
    {
        if(i!=0) d[i] = MAX+1;
        p[i] = -1;
        pass[i] = 0;
    }
    while(1)
    {
        min = MAX + 1;
        tryn = -1;
        for(int i=0; i<n; i++)
        {
            if(min > d[i] && pass[i] !=now[2])
            {
                min = d[i];
                tryn = i;
            }
        }
        if(min == MAX + 1) break;
        pass[tryn] = now[2]; //went
        for(int i=0; i<n; i++)
        {
            if(pass[i] != now[2] && M[tryn][i] !=MAX + 1)
            {
                if(d[i] > M[tryn][i])
                {
                    d[i] = M[tryn][i];
                    p[i] = tryn;
                    pass[i] = now[1];
                }
            }
        }

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

int main()
{
    now.resize(3);
    for(int i=0; i<3; i++) now[i] = i;
    //0 NON 1TRYING 2DEAD
    //input
    cin >> n;
    M.assign(n, vector<int> (n));
    for(int i=0; i<n; i++)
    {
        for(int j=0; j<n; j++)
        {
            cin >> M[i][j];
            if(M[i][j] == -1) M[i][j] = MAX + 1;
        }
    }
    printf("%d\n", buildT());
}

C++: #include<iostream>
using namespace std;
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;
static const int MAXN = 100;
static const int INTFY = (1 << 21);

int n, M[MAXN][MAXN];

int prim()
{
	int u, minv;
	int d[MAXN], p[MAXN], color[MAXN];
	
	for(int i = 0; i < n; i++)
	{
		d[i] = INTFY;
		p[i] = -1;
		color[i] = WHITE;
	}
	
	d[0] = 0;
	
	while(true)
	{
		minv = INTFY;
		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] != INTFY)
			{
				if(M[u][v] < d[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)? INTFY : e; 
		}
	}
	cout << prim() << endl;
	
	return 0;
}

C++: //http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_12_A&lang=jp
#include<vector>
#include<iostream>
#include<queue>
#include<functional>
using namespace std;

constexpr int INF = 1e9;

struct edge
{
	int from, to, cost;
	bool operator>(const edge& other)
	const{
		return cost > other.cost;
	}
};

using edges = vector<edge>;//辺を集めた
using graph = vector<edges>;//i番目のvectorには頂点iから延びる辺全てが並んでる

void add_edge(graph& g, int from, int to, int cost)
{
	g[from].push_back(edge{ from,to,cost });
	g[to].push_back(edge{ to,from,cost });
}

edges prim(const graph& g)
{
	const int N = g.size();//頂点の数
	vector <bool> used(N);
	used[0] = true;
	priority_queue<edge, vector<edge>, greater<edge>>p_que;
	for (auto e : g[0])
	{
		p_que.push(e);
	}
	edges mst;//最小全域木
	while (!p_que.empty())
	{
		auto e=p_que.top();//重みが最小の辺にアクセス
		p_que.pop();//重みが最小の辺をpriority_queueから削除
		if (used[e.to])//重み最小の辺の行き先がすでに訪れたことがある
		{
			continue;
		}
		//重み最小の辺の行き先がすでに訪れたことがない
		mst.push_back(e);//最小全域木に辺を追加
		used[e.to] = true;
		for (auto new_e : g[e.to])//集合Tに新しく含まれた頂点につながっている辺をpriority_queueに追加
		{
			p_que.push(new_e);
		}
	}
	return mst;

}

int main()
{
	int V, E;//Vは頂点　Eは辺の数
	cin >> V >> E;
	graph g(V);//「各頂点とそこから延びる辺」を頂点の数だけ並べた
	for (int i = 0; i < E; i++)
	{
		int from, to, cost;
		cin >> from >> to >> cost;
		add_edge(g, from, to, cost);
	}
	const auto mst = prim(g);
	int ans = 0;
	for (auto& e : mst)
	{
		ans += e.cost;
	}
	cout << ans << endl;
	return 0;
}

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

int main(){
    int n;cin >> n;
    A.assign(n,vector<int>(n));
    for(auto &i:A)for(auto &j:i) cin >> j;
    vector<int> imat(n);//今行き終わった頂点
    bool flag = true;
    imat[0] = 1;
    int ans = 0;
    while(flag){
        pair<int,int> pa(2001,-1);
        for(int now=0;now<n;now++){
            if(!imat[now]) continue;
            for(int to=0;to<n;to++){
                if(imat[to]) continue;
                if(A[now][to]==-1) continue;
                if(pa.first>A[now][to]){
                    pa.first = A[now][to];
                    pa.second = to;
                }
            }
        }
        imat[pa.second] = 1;
        ans+=pa.first;
        bool e = true;
        for(int i=0;i<n;i++){
            if(imat[i]==0){
                e = false;
                break;
            }
        }
        if(e) flag = false;
    }
    cout << ans << endl;
}

C++: #include<iostream>
#include<climits>
using namespace std;
typedef struct{
  int a[2000];
  bool f;
}node;

node T[103];
int m;
int n;
int ans;
int cnt;
int num;
void route(){
  m=INT_MAX;
  if(cnt==n)return;
  for(int i=0;i<n;i++){
    if(T[i].f==true){
      for(int j=0;j<n;j++){
	if(m>T[i].a[j]){
	  m=T[i].a[j];
	  num=j;
	}
      }
    }
  }
  ans+=m;
  cnt++;
  T[num].f=true;
  route();
}
int main(){
  int key;
  cin>>n;
  for(int i=0;i<n;i++){
    for(int j=0;j<n;j++){
      cin>>key;
      if(key==-1)T[i].a[j]=INT_MAX;
      else T[i].a[j]=key;
    }
  }
  T[0].f=true;
  route();
  cout<<ans<<endl;
  return 0;
}