Go: package main

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

const (
	WHITE int = iota
	GRAY
	BLACK
)

type graph struct {
	G [][]int
	C []int
	D []int
	P []int
}

var INFTY = 1 << 32

func constructGraph() *graph {
	sc := bufio.NewScanner(os.Stdin)
	sc.Scan()
	n, _ := strconv.Atoi(sc.Text())

	g := make([][]int, n)
	c := make([]int, n)
	d := make([]int, n)
	p := make([]int, n)

	for i := 0; i < n; i++ {
		sc.Scan()
		fields := strings.Fields(sc.Text())
		g[i] = toIntSlice(fields)
	}

	return &graph{g, c, d, p}
}

func (g *graph) initMst() {
	for i := 0; i < len(g.G); i++ {
		g.C[i] = WHITE
		g.D[i] = INFTY
	}
	g.D[0] = 0
	g.P[0] = -1
}

func (g *graph) primMst() int {
	g.initMst()

	var u int
	for {
		mincost := INFTY
		for i := 0; i < len(g.G); i++ {
			if g.C[i] != BLACK && g.D[i] < mincost {
				mincost = g.D[i]
				u = i
			}
		}

		if mincost == INFTY {
			break
		}

		g.C[u] = BLACK

		for i := 0; i < len(g.G); i++ {
			if g.C[i] != BLACK && g.existEdge(u, i) {
				if g.G[u][i] < g.D[i] {
					g.D[i] = g.G[u][i]
					g.P[i] = u
				}
			}
		}
	}

	totalCost := 0
	for _, cost := range g.D {
		totalCost += cost
	}
	return totalCost
}

func (g *graph) existEdge(u, v int) bool {
	return g.G[u][v] != INFTY && g.G[v][u] != INFTY
}

func toIntSlice(s []string) []int {
	a := make([]int, len(s))
	for i, v := range s {
		num, _ := strconv.Atoi(v)
		if num == -1 {
			num = INFTY
		}
		a[i] = num
	}
	return a
}

func main() {
	g := constructGraph()
	fmt.Println(g.primMst())
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
#define PII pair<int,int>
#define MPII(a,b) make_pair(a,b)
using namespace std;
class NODE
{
public:
	vector<PII> v;
	int index = 0;
};
class Tree
{
public:
	vector<int> V;
	int cost = 0;
};

int main()
{
	NODE* nodes;
	nodes = new NODE[101];
	Tree T;
	int n;
	cin >> n;
	for (int i = 1; i <= n; i++)
	{
		for (int j = 1; j <= n; j++)
		{
			int t;
			cin >> t;
			if (t != -1)
			{
				PII p = MPII(t, j);
				nodes[i].v.push_back(p);
			}
		}
		sort(nodes[i].v.begin(), nodes[i].v.end());
	}
	T.V.push_back(1);
	while (1)
	{
		if (T.V.size() == n+1)break;
		int mincost=10000;
		int addv=-1;
		int index;
		for (auto itr : T.V)
		{
			if (mincost >= nodes[itr].v[nodes[itr].index].first)
			{
				mincost = nodes[itr].v[nodes[itr].index].first;
				addv = nodes[itr].v[nodes[itr].index].second;
				index = itr;
			}
		}
		nodes[index].index++;
		T.V.push_back(addv);
		T.cost += mincost;
	}
	cout << T.cost << endl;
	delete[] nodes;
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
const int MAX=101;
const int INF=123456789;


class union_find{
public:
  vector<int> rank,pare;
  union_find(){}
  union_find(int size){
    rank.resize(size,0);
    pare.resize(size,0);
    for(int i=0;i<size;i++){
      init(i);
    }
  }

  void init(int x){
    pare[x]=x;
    rank[x]=0;
  }

  int findpare(int x){
    if(pare[x]==x){
      return x;
    }
    else{
      return pare[x]=findpare(pare[x]);
    }
  }

  bool issame(int x,int y){
    return findpare(x)==findpare(y);
  }

  void unite(int x,int y){
    x=findpare(x);
    y=findpare(y);

    if(x==y) return;
    if(rank[x]<rank[y]){
      pare[x]=y;
    }
    else{
      pare[y]=x;
      if(rank[x]==rank[y]) rank[x]++;
    }
  }


};

int n,E;
struct edge{
  int u,v,cost;
};

edge es[MAX];


bool comp(const edge& e1,const edge& e2){
  return e1.cost<e2.cost;
}


int kruskal(){
  sort(es,es+E,comp);//E???????????°
  int res=0;
  union_find uf=union_find(n+1);
  for(int i=0;i<E;i++){
    edge e=es[i];
    if(!uf.issame(e.u,e.v)){
      uf.unite(e.u,e.v);
      res+=e.cost;
    }
  }
  return res;
}

int main(){
  cin>>n>>E;
  for(int i=0;i<E;i++){
    int s,t,w;
    cin>>s>>t>>w;
    es[i].u=s;
    es[i].v=t;
    es[i].cost=w;
  }

  cout<<kruskal()<<endl;
  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;

struct Node{
  int color, parent;
};

int n, M[MAX][MAX];
Node node[MAX];

void Printnodes(){
  string col[3] = {"WHITE", "GRAY ", "BLACK"};
  for(int i = 0; i < n; i++){
    cout << "nodeID : " << i ;
    cout << " color : " << col[node[i].color];
    cout << " parent : " << node[i].parent << endl;
  }
  cout << endl;
}

int main(){
  cin >> n;

  for(int i = 0; i < n; i++){
    for(int j = 0; j < n; j++){
      cin >> M[i][j];
    }
    node[i].color = WHITE;
    node[i].parent = -1;
  }

  node[0].color = BLACK;
  int totaldist = 0;

  while(true){
 //   Printnodes();

    int count = 0, min_d = INFTY, min_id, dist;
    for(int p = 0; p < n; p++){
      if(node[p].color == BLACK){
        for(int i = 0; i < n; i++){
          if(M[p][i] > 0 && node[i].color != BLACK){
            count++;
            node[i].color = GRAY;
            dist = M[p][i];
            if(dist < min_d){
              min_d = dist;
              min_id = i;
              node[i].parent = p;
            }
          }
        }
      }
    }

    if(count == 0)
      break;
    node[min_id].color = BLACK;
    totaldist += min_d;
  }

  cout << totaldist << endl;
  return 0;
}

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

using namespace std;
using MatrixGraph = vector<vector<int>>;

const int no_edege = -1;
const int started_label = 1;
const int no_label = -1;
const int no_cost = -1;
const int max_weight = 2'000;

int total_weight(MatrixGraph matrix_graph, int started_label) {
  vector<int> min_weights(matrix_graph.size(), max_weight + 1);
  vector<int> visited(matrix_graph.size(), false);
  min_weights[started_label - 1] = 0;
  int weight_sum = 0;

  while(true) {
    int min_weight = max_weight + 1;
    int min_label = no_label;
    for(int i = 0; i < matrix_graph.size(); i++) {
      if(visited[i]) {
        continue;
      }
      if(min_weights[i] < min_weight) {
        min_weight = min_weights[i];
        min_label = i + 1;
      }
    }
    if(min_label == no_label) {
      break;
    }

    visited[min_label - 1] = true;
    weight_sum += min_weight;

    for(int i = 0; i < matrix_graph.size(); i++) {
      if(matrix_graph[min_label - 1][i] < min_weights[i]) {
        min_weights[i] = matrix_graph[min_label - 1][i];
      }
    }
  }

  return weight_sum;
}

int main() {
  int vertex_num;
  cin >> vertex_num;
  MatrixGraph matrix_graph(vertex_num, vector<int>(vertex_num, no_edege));

  // Init
  for(int i = 0; i < vertex_num; i++) {
    for(int j = 0 ;j < vertex_num; j++) {
      int weight;
      cin >> weight;
      if(weight == no_edege) {
        matrix_graph[i][j] = max_weight + 1;
        continue;
      }
      matrix_graph[i][j] = weight;
    }
  }

  cout << total_weight(matrix_graph, started_label) << endl;
}

C++: #include <stdio.h>

#define inf 100000
#define WHITE 1
#define BLACK 0
#define MAX 100

int d[MAX],pi[MAX],color[MAX],A[MAX][MAX];
int n,i,j,v,u;
int min,count=0;

int main(){

  int i,j;

  scanf("%d",&n);
  for(i=0;i<n;i++){
    for(j=0;j<n;j++){
      scanf("%d",&A[i][j]);
    }
  }

  for(i=0;i<n;i++){
    d[i]=inf;
    color[i]=WHITE;
  }
  d[0]=0;

  while(1){
    min=inf;
    for(i=0;i<n;i++){
      if(color[i]==WHITE && d[i]<min){
	min=d[i];
	u=i;
      }
    }

    if(min==inf)break;
    color[u]=BLACK;
    count+=min;

    for(v=0;v<n;v++){
      if(A[u][v]==-1)continue;
      if(d[v]>A[u][v])d[v]=A[u][v];
    }
  }

  printf("%d\n",count);

  return 0;
  
}