Go: package main

import (
	"fmt"
	"sort"
)

type Edge struct {
	I int
	J int
	A int
}

func main() {
	var n int
	fmt.Scan(&n)

	g := make([][]int, n)
	edges := make([]Edge, 0, n*(n-1)/2)
	parents := make([]int, n)
	for i := 0; i < n; i++ {
		g[i] = make([]int, n)
		for j := 0; j < n; j++ {
			fmt.Scan(&g[i][j])
			if g[i][j] > 0 {
				edges = append(edges, Edge{i, j, g[i][j]})
			}
		}
		parents[i] = i
	}
	sort.SliceStable(edges, func(i, j int) bool { return edges[i].A > edges[j].A })

	sum := 0
	for len(edges) > 0 {
		edge := edges[len(edges)-1]
		edges = edges[:len(edges)-1]

		if same(edge.I, edge.J, &parents) {
			continue
		}
		union(edge.I, edge.J, &parents)
		sum += edge.A
	}
	fmt.Println(sum)
}



func root(id int, parents *[]int) int {
	if id == (*parents)[id] {
		return id
	}
	return root((*parents)[id], parents)
}

func union(i, j int, parents *[]int) {
	(*parents)[root(j, parents)] = (*parents)[root(i, parents)]
}

func same(i, j int, parents *[]int) bool {
	return root(i, parents) == root(j, parents)
}

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-1)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]){
	  if(T[j].f==false){
	    m=T[i].a[j];
	    num=j;
	  }
	}
      }
    }
  }
  ans+=m;
  cnt++;
  //  cout<<"m "<<m<<endl;
  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;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <algorithm>

using namespace std;
const int INF = 10000;

template<typename ISTREAM>
void readWeightedGraphMatrix(ISTREAM& cin, vector< vector<int> >& weightedGraphMatrix) {
	size_t n;
	cin >> n;
	int weight;
	weightedGraphMatrix = vector< vector<int> >(n, vector<int>(n));
	for (size_t i = 0; i != n; ++i) {
		for (size_t j = 0; j != n; ++j) {
			cin >> weight;
			weightedGraphMatrix.at(i).at(j) = weight != -1 ? weight : INF;
		}
	}
}

int MST(const vector< vector<int> >& weightedGraphMatrix) {
	enum Color {
		WHITE, GRAY, BLACK//白は未訪問でTと隣接していない、。グレーはTと隣接状態だが未訪問、黒は訪問済み。木は閉路を持たないグラフなので、全域木の計算では
		//各点は1度だけ訪問されるはず。
	};
	const size_t n = weightedGraphMatrix.size();
	vector<Color> color(n,Color::WHITE);
	vector<int> d(n, INF);
	vector<int> p(n, INF);
	color.front() = Color::GRAY;
	d.front() = 0;
	int currentNode = 0;
	while (true) {
		int min = INF;
		int u;
		//TからV-Tへ接続する辺の重みの中で最小のものを求める。
		//同時に、その辺に接続する辺のT内のノードを求める。
		for (size_t i = 0; i != n; ++i) {
			if (color.at(i) == Color::BLACK || color.at(i) == Color::WHITE) continue;//訪問済みはスキップ
			if (d.at(i) >= min)continue;
			min = d.at(i);//TからV-Tの中のノードiへ向かう辺の重みの最小値を更新
			u = i;
		}

		
		if (min == INF) {
			//ノード０からアクセス可能なすべての点を訪問済みになったらMST構築完了
			break;
		}

		color.at(u) = Color::BLACK;//訪問

		for (int v = 0; v != n; ++v) {
			if (color.at(v) == Color::BLACK) continue;//訪問済みはスキップ
			if (weightedGraphMatrix.at(u).at(v) == INF ) continue;
			if (weightedGraphMatrix.at(u).at(v) >= d.at(v)) continue;
			d.at(v) = weightedGraphMatrix.at(u).at(v);
			p.at(v) = u;
			color.at(v) = Color::GRAY;

		}
	
	}
	int sum = 0;
	for (int i = 0; i != n; ++i) {
		if (p.at(i) == INF)continue;
		sum += weightedGraphMatrix.at(i).at(p.at(i));
	}
	return sum;
}

template<typename T>
void execute(T& cin) {
	vector< vector<int> > graphList;
	readWeightedGraphMatrix(cin, graphList);
	cout << MST(graphList) << endl;
}


int main()
{


	execute(cin);



	return 0;
}

C++: #include <stdio.h>
static const int MAX=100;
static const int INFTY=(1<<30);
static const int WHITE=0;
static const int GRAY=1;
static const int BLACK=2;

int n,M[MAX][MAX];

int prim(){
    int i,u,minv,sum;
    int d[MAX],p[MAX],color[MAX];

    for(i=0;i<n;i++){
        d[i]=INFTY;
        p[i]=-1;
        color[i]=WHITE;
    }
    d[0]=0;
    while(1){
        minv=INFTY;
        u=-1;
        for(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(i=0;i<n;i++){
            if(color[i]!=BLACK && M[u][i]!=INFTY){
                if(d[i]>M[u][i]){
                    d[i]=M[u][i];
                    p[i]=u;
                    color[i]=GRAY;
                }
            }
        }
    }
        sum=0;
        for(i=0;i<n;i++)
            if(p[i]!=-1)
                sum+=M[i][p[i]];
    return sum;
}

int main(void){
    int i,o,e;
    scanf("%d",&n);
    for(i=0;i<n;i++)
        for(o=0;o<n;o++){
            scanf("%d",&e);
            M[i][o]=(e==-1)?INFTY:e;
        }
    printf("%d\n",prim());
    return 0;
}

C++: #include<iostream>
#include<utility>
#include<algorithm>
using namespace std;
int matrix[100][100];
pair<int,int> edges[100];
int cost[100];
int src[100];
int dst[100];
int p[10010];
void init(int n)
{
    for(int i=0;i<=n;i++)
        p[i]=i;
};
int root(int a)
{
    if(p[a]==a)
        return a;
    return (p[a]=root(p[a]));
};
bool is_same_set(int a,int b)
{
    return root(a)==root(b);
};
void unite(int a,int b)
{
    p[root(a)]=root(b);
};

int main()
{
    int n,amount=0,sum=0;
    
    cin>>n;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            cin>>matrix[i][j];
        }
    }
    
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<=i;j++)
        {
            if(matrix[i][j]!=-1)
            {
                cost[amount]=matrix[i][j];
                src[amount]=i;
                dst[amount]=j;
                amount++;
            }
        }
    }
    for(int i=0;i<amount;i++)
    {
        edges[i]=make_pair(cost[i],i);
    }

	init(n);
    
    sort(edges,edges+amount);
    
    for(int i=0;i<amount;i++)
    {
        if(is_same_set(src[edges[i].second],dst[edges[i].second]))
			continue;
		unite(src[edges[i].second],dst[edges[i].second]);
		sum+=edges[i].first;
    }
	cout<<sum<<endl;
}