Go: package main

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

const (
	white int = iota
	gray
	black
)
const INFTY int = 2001

var G [][]int
var color []int
var d []int
var p []int
var N int

func prim() int {
	u := 0
	d[u] = 0
	for {
		minCost := INFTY
		for i:=0; i<N; i++ {
			if color[i] != black && d[i] < minCost {
				minCost = d[i]
				u = i
			}
		}

		if minCost == INFTY {
			break
		}

		color[u] = black

		for v:=0; v<N; v++ {
			if color[v] != black && G[u][v] != -1 {
				if G[u][v] < d[v] {
					d[v] = G[u][v]
					p[v] = u
					color[v] = gray
				}
			}
		}
	}
	// sum
	sum := 0
	for i:=0; i<N; i++ {
		if p[i] != -1 {
			sum += G[i][p[i]]
		}
	}
	return sum
}




func initVariable(N int) {
	G = make([][]int, N)
	for i:=0; i<N; i++ {
		G[i] = make([]int, N)
	}
	color = make([]int, N)
	d = make([]int, N)
	p = make([]int, N)
	for i:=0; i<N; i++ {
		d[i] = INFTY
		p[i] = -1
	}
}

func nextInt(scanner *bufio.Scanner) int {
	scanner.Scan()
	num, _ := strconv.Atoi(scanner.Text())
	return num
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	N = nextInt(scanner)
	initVariable(N)
	for i:=0; i<N; i++ {
		for j:=0; j<N; j++ {
			G[i][j] = nextInt(scanner)
		}
	}
	fmt.Printf("%d\n", prim())
}

C++: 
#include <iostream>
using namespace std;
const int MAX = 100;
const int INFTY = 2000000;
const int WHITE = 0;
const int GRAY = 1;
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>
using namespace std;

#define MAX 101
#define BLACK 0
#define GRAY 1
#define WHITE 2
#define INF (1<<30)

int graph[MAX][MAX];
int n;

// プロトタイプ宣言
int prim();
// ここまで

int main(){
	cin>>n;

	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			cin>>graph[i][j];
		}
	}
	cout<<prim()<<endl;


	return 0;
}




//---------------------- 
int prim(){
	int from;
	int color[MAX];
	int d[MAX];
	int p[MAX];

	for(int i=0;i<MAX;i++){
		color[i]=WHITE;
		d[i]=INF;		
		p[i]=-1;
	}
	d[0]=0;

	while(1){
		int minCost=INF;
		from=-1;

		for(int i=0;i<n;i++){
			if(color[i]!=BLACK && d[i]<minCost){
				minCost=d[i];
				from=i;
			}
		}
		if(from == -1) break;

		color[from]=BLACK;
		for(int to=0;to<n;to++){
			if(color[to]!=BLACK && graph[from][to]!=-1){
				if(graph[from][to]<d[to]){
					d[to]=graph[from][to];
					p[to]=from;
					color[to]=GRAY;
				}
			}
		}
	}

	int sum=0;
	for(int i=0;i<n;i++){
		// if(p[i]!=-1){
		// 	sum += graph[i][ p[i] ];
		// }
		if(d[i]!=INF){
			sum+=d[i];
		}
	}

	return sum;
}

C++: #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <stack>
#include <vector>
#include <algorithm>
#include <cmath>
#include <climits>
#include <cstring>
#include <queue>
#include <string>

using namespace std;

const double PI = 3.14159265358979;

int G[101][101];
int visited[101];
int n;

int houxuan[101];
int father[101];

int prim() {

	for (int i = 0; i < n; i++)	{
		houxuan[i] = INT_MAX;
		father[i] = -1;
	}
	houxuan[0] = 0;

	int sum = 0;
	while (true) {

		int minx = INT_MAX;
		int u = -1;
		for (int i = 0; i < n; i++) {

			if (!visited[i] && houxuan[i] < minx) {
				minx = houxuan[i];
				u = i;
			}
		}
		if (u == -1) break;

		visited[u] = 1;
		sum += minx;

		for (int i = 0; i < n; i++) {
			if (!visited[i] && G[u][i] != -1) {
				if (houxuan[i] > G[u][i]) {
					houxuan[i] = G[u][i];
					father[i] = u;
				}
			}
		}
	}	

	return sum;
}

int main() {
	
	
	cin >> n;

	for (int i = 0; i < n; i++)	{
		for (int j = 0; j < n; j++)	{
			cin >> G[i][j];
		}
	}
	
	cout << prim() << endl;

	return 0;
}

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

const int MAX = 100;
const int INF = 1 << 29;

int n;
int grid[MAX][MAX];

int prim()
{
	int ans[MAX];
	int visited[MAX];

	for (int i = 0; i < n; i++) {
		ans[i] = INF;
		visited[i] = false;
	}

	ans[0] = 0;

	while (true) {
		int min = INF;
		int min_index;

		for (int i = 0; i < n; i++) {
			if (min > ans[i] && visited[i] == false) {
				min = ans[i];
				min_index = i;
			}
		}

		if (min == INF) {
			break;
		}

		visited[min_index] = true;

		for (int i = 0; i < n; i++) {
			if (ans[i] > grid[min_index][i] && visited[i] == false) {
				ans[i] = grid[min_index][i];
			}
		}
	}

	int sum = 0;

	for (int i = 0; i < n; i++) {
		sum += ans[i];
	}

	return sum;
}

int main()
{
	cin >> n;

	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			int temp;
			cin >> temp;

			if (temp == -1) {
				grid[i][j] = INF;
			}
			else {
				grid[i][j] = temp;
			}
		}
	}

	cout << prim() << endl;

	return 0;
}

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

#define mp       make_pair
#define pb       push_back
#define all(x)   (x).begin(),(x).end()
#define rep(i,n) for(int i=0;i<(n);i++)
#define repi(i,a,b) for(int i = (a); i < (b); i++)

using namespace std;

typedef    long long          ll;
typedef    unsigned long long ull;
typedef    vector<bool>       vb;
typedef    vector<int>        vi;
typedef    vector<vb>         vvb;
typedef    vector<vi>         vvi;
typedef    pair<int,int>      pii;

const int INF=1<<29;
const double EPS=1e-9;

const int dx[]={1,0,-1,0},dy[]={0,-1,0,1};
const int MAX_N =200;
int n;
int cost[MAX_N][MAX_N];
bool visit[MAX_N];
int mincost[MAX_N]={INF};
int prime(){
	//memset(mincost, INF, sizeof(mincost));
	memset(visit, false, sizeof(visit));
	int res = 0;
	mincost[0]=0;
	/*rep(i, n){
		cout <<mincost[i];
		cout <<endl;
	}
	cout <<endl;*/
	while(1){
		int v = -1;
		for(int u =0; u <n;u++){
			if(!visit[u]&&(v==-1||mincost[u] < mincost[v])){
				v =u;
			}
		}
		if(v==-1)break;
		//cout <<"v = "<<v<<endl;
		visit[v]=true;
		res +=mincost[v];
		//cout <<res<<endl;
		for(int u =0;u <n ;u++){
			mincost[u]=min(mincost[u],cost[v][u]);
		}
	}
	//cout <<res<<endl;
	return res;
}

int main(){
	cin>>n;
	for(int i =0;i < n;i++){
		mincost[i]=INF;
		for(int j =0;j < n;j++){
			int x;
			cin>>x;
			if(x==-1){
				cost[i][j]=INF;
			}else{
				cost[i][j]=x;
			}
		}
	}
	/*rep(i, n){
		rep(j, n){
			printf("%10d",cost[i][j]);
		}
		cout <<endl;
	}*/

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