Go: package main

import (
	"bufio"
	"container/heap"
	"fmt"
	"os"
	"strconv"
)

type Node struct {
	id     int
	weight int
}

func init() {
	sc.Split(bufio.ScanWords)
}

type NodeHeap []Node

func (h NodeHeap) Len() int           { return len(h) }
func (h NodeHeap) Less(i, j int) bool { return h[i].weight < h[j].weight }
func (h NodeHeap) Swap(i, j int)      { h[i], h[j] = h[j], h[i] }

func (h *NodeHeap) Push(x interface{}) {
	*h = append(*h, x.(Node))
}

func (h *NodeHeap) Pop() interface{} {
	old := *h
	n := len(old)
	x := old[n-1]
	*h = old[0 : n-1]
	return x
}

var sc = bufio.NewScanner(os.Stdin)

func scanInt() int {
	num, _ := strconv.Atoi(scanString())
	return num
}

func scanString() string {
	sc.Scan()
	return sc.Text()
}

func init() {
	sc.Split(bufio.ScanWords)
}

func main() {
	n := scanInt()

	list := make([][]int, n)
	seen := make([]bool, n)
	for i := range list {
		list[i] = make([]int, n)
		for j := range list[i] {
			list[i][j] = scanInt()
		}
	}

	start := Node{id: 0, weight: 0}
	h := &NodeHeap{start}
	heap.Init(h)

	sum := 0
	for h.Len() > 0 {
		node := heap.Pop(h).(Node)
		if seen[node.id] {
			continue
		}
		seen[node.id] = true
		sum += node.weight

		for i, w := range list[node.id] {
			if w < 0 {
				continue
			}
			heap.Push(h, Node{id: i, weight: w})
		}
	}

	fmt.Println(sum)
}

C++: //MST
#if 0

#endif // 0
#include<iostream>
#include<vector>
#include<algorithm>
#define MAX 10010
using namespace std;
vector<int> s;
int root(int x)
{
    if(s[x]==x)return x;
    return root(s[x]);
}

int main()
{
    int cost[MAX],src[MAX],dst[MAX];
    pair<int,int> pos[MAX];
    int n,sum=0;
    cin>>n;
    s.resize(n);
    for(int i=0;i<n;i++)
    {
        s[i]=i;
    }
    int m=0;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            int a;
            cin>>a;
            if(a>=0&&j>i)
            {
                cost[m]=a;
                src[m]=i;
                dst[m]=j;
                pos[m].first=cost[m];
                pos[m].second=m;
                m++;
            }
        }
    }
    sort(pos,pos+m);
    int c=0;
    for(int i=0;i<m;i++)
    {
        int d=pos[i].second;
        if(root(src[d])!=root(dst[d]))
        {
            sum+=cost[d];
            s[root(src[d])]=root(dst[d]);
            c++;
        }
        if(c>=n-1)
            break;
    }

    cout<<sum<<endl;
    return 0;
}

C++: #include <iostream>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm>	// require sort next_permutation count __gcd reverse etc.
#include <cstdlib>	// require abs exit atof atoi 
#include <cstdio>		// require scanf printf
#include <functional>
#include <numeric>	// require accumulate
#include <cmath>		// require fabs
#include <climits>
#include <limits>
#include <cfloat>
#include <iomanip>	// require setw
#include <sstream>	// require stringstream 
#include <cstring>	// require memset
#include <cctype>		// require tolower, toupper
#include <fstream>	// require freopen
#include <ctime>		// require srand
#define rep(i,n) for(int i=0;i<(n);i++)
#define ALL(A) A.begin(), A.end()
#define INF 1<<20

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

int cost[105][105];
int mincost[105];
bool used[105];

int prim (int n )
{
	memset (used, false, sizeof (used ) );
	rep (i, n ) mincost[i] = INF;

	mincost[0] = 0;
	int res = 0;

	while (true ){
		int v = -1;
		// X  に属さない頂点のうち X からの辺のコストが最小になる頂点を探す
		rep (u, n )
			if (!used[u] && (v == -1 || mincost[u] < mincost[v] ) ) v = u;

		if (v == -1 ) break;

		used[v] = true;
		res += mincost[v];

		rep (u, n ) mincost[u] = min (mincost[u], cost[v][u] );
	} // end while

	return res;
}

int main()
{
	memset (cost, -1, sizeof (cost ) );
	ios_base::sync_with_stdio(0);
	int n; cin >> n;
	rep (i, n ) rep (j, n ) cost[i][j] = INF;

	rep (i, n ) rep (j, n ){
		int in; cin >> in;
		if (in == -1 ) in = INF;
		cost[i][j] = in;
	} // end rep

	int res = prim(n );
	cout << res << endl;
		
	return 0;
}

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

const int WHITE = 0;
const int GRAY = 1;
const int BLACK = 2;

int n;
vector<vector<int>> G;
vector<int> d;
vector<int> colors;

int mst(){
    int left = n;
    int res = 0;
    d[0] = 0;
    //int u = 0;
    while(left--){
        int min_id = -1;
        int cur_min = INT_MAX;
        for(int i = 0;i<n;i++){
            if(colors[i] !=BLACK && d[i] < cur_min){
                min_id = i;
                cur_min = d[i];
            }
        }
        if(min_id==-1) break;
        int u = min_id;
        //printf("%d\n",min_id);
        res += d[u];
        colors[u] = BLACK;
        for(int v =0;v<n;v++){
            if(colors[v]!=BLACK && G[u][v] < d[v]){
                d[v] = G[u][v];
            }
        }
    }
    return res;
}

int main()
{
    //freopen("data.txt","r",stdin);
    cin >> n;
    G.resize(n,vector<int>(n,INT_MAX));
    d.resize(n,INT_MAX);
    colors.resize(n,WHITE);
    int tmp;
    for(int i =0;i<n;i++){
        for(int j=0;j<n;j++){
            cin >> tmp;
            if(tmp!=-1) G[i][j] = tmp;
        }
    }
    cout << mst() <<endl;
    return 0;
}

C++: #include <iostream>
#include <vector>
#include <queue>
#define USED 1
using namespace std;
typedef pair<int, int> pi;
int graph[200][200];
int used[200];
// first:weight, second:key
priority_queue<pi, vector<pi>, greater<pi>> pqueue;

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      cin >> graph[i][j];   
    }
  }
  
 
  int ans = 0, key;
  pqueue.push(make_pair(0,0));
  for (int i = 0; i < n; i++) {
    pi p = pqueue.top();
    pqueue.pop();
    key = p.second;
    if (used[key] == USED) {
      i--;
      continue;
    }
    used[key] = USED;
    ans += p.first;
    //cout << "Distance: " << p.first << ", Key: " << p.second << endl;
    for (int j = 0; j < n; j++) {
      if (graph[key][j] != -1 && used[j] != USED) {
        pqueue.push(make_pair(graph[key][j],j));
      }
    }
  }
  cout << ans << endl;
}

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

int main(){
  long n;
  long G[100][100],d[100];
  long minc,mi,i;
  bool flg[i];
  long sum=0;
  
  cin >> n;
  for (i=0;i<n;i++){
    for (int j=0;j<n;j++){
      cin >> G[i][j];
      if (G[i][j]==-1) G[i][j]=INF;
    }
    d[i]=INF;
    flg[i]=false;
  }

  d[0]=0;
  while (true){
    minc=INF;
    for (i=0;i<n;i++){
      if (!flg[i]&&d[i]<minc){
	minc=d[i];
	mi=i;
      }
    }
    if (minc==INF) break;
    flg[mi]=true;
    for (i=0;i<n;i++){
      if (G[mi][i]==INF)continue;
      if (!flg[i]&&d[i]>G[mi][i]){
	d[i]=G[mi][i];
	//cout << d[i];
      }
    }
  }
  
  for (i=0;i<n;i++){
    sum+=d[i];
  }
  cout << sum<<endl;
  
  return 0;
}