Go: package main

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

var scanner = bufio.NewScanner(os.Stdin)

func nextString() string {
	scanner.Scan()
	return scanner.Text()
}

func nextInt() int {
	n, err := strconv.Atoi(nextString())
	if err != nil {
		fmt.Printf("strconv.Atoi failed: %v\n", err)
	}
	return n
}

type ElementKey int
type Node struct {
	key    ElementKey
	parent *Node
	rank   int
}

func newNode(key ElementKey) *Node {
	node := Node{}
	node.key = key
	node.parent = &node
	node.rank = 0

	return &node
}

func union(x, y *Node) {
	xSet := findSet(x)
	ySet := findSet(y)
	link(xSet, ySet)
}

func link(x, y *Node) {
	if x.rank > y.rank {
		y.parent = x
	} else {
		x.parent = y

		if x.rank == y.rank {
			y.rank++
		}
	}
}

func findSet(x *Node) *Node {
	if x != x.parent {
		x.parent = findSet(x.parent)
	}
	return x.parent
}

func newDisjointSets(nSets int) *DisjointSets {
	sets := DisjointSets{}
	sets.keyToNode = map[ElementKey]*Node{}
	for i := 0; i < nSets; i++ {
		key := ElementKey(i)
		sets.keyToNode[key] = newNode(key)
	}
	return &sets
}

type DisjointSets struct {
	keyToNode map[ElementKey]*Node
}

func (sets *DisjointSets) union(x, y ElementKey) {
	xNode := sets.keyToNode[x]
	yNode := sets.keyToNode[y]
	union(xNode, yNode)
}

func (sets DisjointSets) findSet(x ElementKey) *Node {
	xNode := sets.keyToNode[x]
	return findSet(xNode)
}

type Edge struct {
	i, j   int
	weight int
}

func main() {
	scanner.Split(bufio.ScanWords)

	n := nextInt()
	sets := newDisjointSets(n)
	edges := []Edge{}
	a := make([][]int, n)
	for i := 0; i < n; i++ {
		a[i] = make([]int, n)
		for j := 0; j < n; j++ {
			a[i][j] = nextInt()
			if a[i][j] >= 0 {
				e := Edge{
					i:      i,
					j:      j,
					weight: a[i][j],
				}
				edges = append(edges, e)
			}
		}
	}
	sort.Slice(edges, func(i, j int) bool { return edges[i].weight < edges[j].weight })
	totalWeight := 0
	for _, edge := range edges {
		i := ElementKey(edge.i)
		j := ElementKey(edge.j)
		if sets.findSet(i) != sets.findSet(j) {
			totalWeight += edge.weight
			sets.union(i, j)
		}
	}
	fmt.Println(totalWeight)
}

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

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

typedef int Weight;
struct Edge{
	int src,dst;
	Weight weight;
};

bool asc(const Edge &e,const Edge &f){
	return e.weight < f.weight;
}

vector<Edge> Edges;
vector<int> L(100005,-1);

int check(Edge &e,int n){
	int j;
	if (L[e.src]!=L[e.dst]){
		int t = L[e.dst];
		for (j=0;j<n;j++){
			if (L[j]==t){
				L[j]=L[e.src];
			}
		}
		return 1;
	}else{
		return 0;
	}
}

int main(){
	int n,m,i,j,w;
	cin >> n ;
	
	for (i=0;i<n;i++){
		for(j=0;j<n;j++){
			cin >> w;
			if ((w!=-1)&&(i>j)){
				Edge edge={i,j,w};
				Edges.push_back(edge);
			}
		}
	}
	
	sort(Edges.begin(),Edges.end(),asc);
	
	m = Edges.size();
	
	for (j=0;j<n;j++){
		L[j]=j;
	}
	
	int TotalWeight=0;
	int count=0;
	int k;
	for (i=0;i<m;i++){
		if (count==n-1){break;}
		if (check(Edges[i],n)){
			TotalWeight+=Edges[i].weight;
			count++;
		}
	}
	cout << TotalWeight<<endl;
	return 0;
}