Go: package main

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

type color int

const (
	white color = iota // 未訪問
	mst                // MSTとして確定
)

// 頂点
type node struct {
	matrix   []int // 隣接行列
	minToMst int   // MSTへの重み最小値
	parent   int   // 最小値の相手
	color    color
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	n := eGetInt(scanner)

	nodes := make([]node, n)
	for i := 0; i < n; i++ {
		nodes[i].matrix = eAtoi((strings.Fields(eGetline(scanner))))
		nodes[i].parent = -1
		nodes[i].minToMst = math.MaxInt16
		nodes[i].color = white
	}

	// 初回はnode[0]がMSTに入るように,minToMstを設定
	nodes[0].minToMst = 0
	for { // 全部チェックするまで

		// どの頂点を新たにMSTに取り込むか
		min := math.MaxInt16
		u := -1
		for i := 0; i < n; i++ {
			if nodes[i].color == mst {
				continue
			}
			if nodes[i].minToMst < min {
				u = i
				min = nodes[i].minToMst
			}
		}

		if u == -1 {
			break
		}
		// MSTに追加する頂点決定
		nodes[u].color = mst

		// MSTに属していない頂点それぞれについて,MSTとの最小の重みを更新
		for i := 0; i < n; i++ {
			if nodes[i].color == mst {
				continue
			}
			if nodes[i].matrix[u] == -1 {
				// 接してない
				continue
			}
			if nodes[i].matrix[u] < nodes[i].minToMst {
				// node[u]との重みが最小
				nodes[i].minToMst = nodes[i].matrix[u]
				nodes[i].parent = u
			}
		}
	}

	sum := 0
	for _, v := range nodes {
		// node[0]はparentが入っていない
		if v.parent != -1 {
			sum += v.matrix[v.parent]
		}
	}
	fmt.Println(sum)
}

func eGetInt(scanner *bufio.Scanner) int {
	t := eAtoi(strings.Fields(eGetline(scanner)))
	return t[0]
}

func eGetline(p *bufio.Scanner) string {
	if !p.Scan() {
		if err := p.Err(); err != nil {
			log.Fatal(err.Error())
		}
		return ""
	}
	return p.Text()
}

func eAtoi(s []string) []int {
	ints := make([]int, len(s))
	var err error
	for i, e := range s {
		ints[i], err = strconv.Atoi(e)
		if err != nil {
			log.Fatal(err.Error())
		}
	}
	return ints
}

C++: #include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0; i < (n) ; i++)
constexpr auto INF = 2147483647;
typedef long long ll;
static const int MAX = 100;
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;

int n, Adjacency_Matrix[MAX][MAX];

int prim(){
  int u, minv;
  int d[MAX], p[MAX], color[MAX];
  // init
  for(int i = 0;i < n; i++){
    d[i] = INF;
    p[i]= -1;
    color[i] = WHITE;
  }

  d[0] = 0;
  while(1){
    minv = INF;
    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 && Adjacency_Matrix[u][v] != INF){
        if(d[v] > Adjacency_Matrix[u][v]){
          d[v] = Adjacency_Matrix[u][v];
          p[v] = u;
          color[v] = GRAY;
        }
      }
    }
  }
    int sum = 0;
    for(int i = 0;i<n; i++){
      if(p[i] != -1) sum += Adjacency_Matrix[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;
      Adjacency_Matrix[i][j] = (e== -1) ? INF : e;
    }
  }

  cout << prim() << endl;

  return 0;
}

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

bool self(vector<vector<int> >& G, int s, int t, vector<pair<int, int> >& stock_idx, vector<int>& searched){
    if (searched[s] == 1) return false;
    searched[s] = 1;
    if (G[s][t] != -1 && stock_idx.size() > 1) {
        stock_idx.push_back(make_pair(s, t));
        return true;
    }
    for (int i = 0; i < G[s].size(); i++) {
        if (G[s][i] != -1) {
            stock_idx.push_back(make_pair(s, i));
            if (self(G, i, t, stock_idx, searched)) return true;
            stock_idx.pop_back();
        }
    }
    return false;
}

bool DFS(vector<vector<int> >& G, int s, int t, vector<pair<int, int> >& stock_idx){
    vector<int> searched(G.size(), 0);
    return self(G, s, t, stock_idx, searched);
}

void cut(vector<vector<int> >& G, vector<pair<int, int> >& stock_idx){
    pair<int, int> max_idx = stock_idx[0];
    for (int i = 0; i < stock_idx.size(); i++) {
        if (G[stock_idx[i].first][stock_idx[i].second] > G[max_idx.first][max_idx.second]) {
            max_idx = stock_idx[i];
        }
    }
    G[max_idx.first ][max_idx.second] = -1;
    G[max_idx.second][max_idx.first ] = -1;
    stock_idx.clear();
}

int main(){
    int n;
    cin >> n;
    vector<vector<int> > G(n, vector<int>(n));
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            cin >> G[i][j];
        }
    }
    for (int i = 0; i < n; i++) {
        vector<pair<int, int> > stock_idx;
        while (DFS(G, i, i, stock_idx)) cut(G, stock_idx);
    }
    int sum = 0;
    for (int i = 0; i < n; i++) {
        for (int j = i + 1; j < n; j++) {
            if (G[i][j] != -1) sum += G[i][j];
        }
    }
    cout << sum << endl;

    return 0;
}

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

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;
  else {
    P[a] = root(P[a]);
    return 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;
  cin >> N;
  pair <int, int> edge[10010];
  for (int i=0; i<N; i++){
    for (int j=0; j<N; j++){
      cin >> edge[i*N+j].first;
      edge[i*N+j].second = i*N+j;
    }
  }
  sort(edge, edge+N*N);
  init(N);
  int ans = 0;
  for (int k=0; k<N*N; k++){
    while(edge[k].first < 0) k++;
    int i = edge[k].second / N;
    int j = edge[k].second % N;
    if (is_same_set(0,i) && is_same_set(0,j)){} //グラフが連結だと仮定
    else{
      unite(0,i);
      unite(0,j);
      ans += edge[k].first;
    }
  }
  cout << ans << endl;
}

C++: #include<iostream>

int prim();

using namespace std;
static const int MAX=100;
static const int INFTY=(1<<21);
static const int WHI=0;
static const int GRA=1;
static const int BLA=2;

int n,M[MAX][MAX];

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

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]=WHI;
    }

  d[0]=0;

  while(1)
    {
      minv=INFTY;
      u=-1;
      for(int i=0;i<n;i++)
	{
	  if(minv>d[i]&&color[i]!=BLA)
	    {
	      u=i;
	      minv=d[i];
	    }
	}
      if(u==-1)
	break;

      color[u]=BLA;
      for(int v=0;v<n;v++)
	{
	  if(color[v]!=BLA&&M[u][v]!=INFTY)
	    {
	      if(d[v]>M[u][v])
		{
		  d[v]=M[u][v];
		  p[v]=u;
		  color[v]=GRA;
		}
	    }
	}
    }

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

  return sum;
}

C++: #include <bits/stdc++.h>
using namespace std;
 
#define INF (1 << 30)
 
struct edge{
 
  int cost, to;
 
};
 
struct state{
 
  int node, cost;
  bool operator <(const state &b)const{
    return cost > b.cost;
  }
};
 
int n;
int d[10001];
vector<edge> glaph[10001];
priority_queue<state> qu;
 
void sss(){
 
  fill(d,d+10001,INF);
  qu.push((state){0,0});
  d[0] = 0;
 
  while(!qu.empty()){
 
    state info;
    info = qu.top();
    qu.pop();
 
    for(int i = 0; i < (double)glaph[info.node].size(); i++){
      if(d[glaph[info.node][i].to] > glaph[info.node][i].cost + d[info.node]){
      d[glaph[info.node][i].to] = glaph[info.node][i].cost + d[info.node];
      qu.push((state){glaph[info.node][i].to,d[info.node]}); 
      }
    }
  }
 
  for(int i = 0; i < n; i++){
    cout << i << " " << d[i] << endl;
  }
   
 
 
 
}
 
int main(){
 
  cin >> n;
 
  for(int i = 0; i < n; i++){
    int k, v, c, u;
    cin >> u >> k;
    for(int j = 0; j < k; j++){
      cin >> v >> c;
      glaph[u].push_back((edge){c,v});
    }
  }
 
  sss();
 
 
}