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++: #include<bits/stdc++.h>
using namespace std;

static const int MAX = 100;
static const int INF = (1<<21);
static const int WHI = 0;
static const int GRA = 1;
static const int BLA = 2;

int n, m[MAX][MAX];

int prim(){
  int u,minv;
  int d[MAX], p[MAX], color[MAX];
  
  for ( int i = 0; i < n ; ++i) {
    d[i] = INF;
    p[i] = -1;
    color[i] = WHI;
  }
  d[0] = 0;
  
  while (1) {
    minv = INF;
    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] != INF) {
	if ( m[u][v] < d[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;
}

  
main()
{
  cin >> n;
  for ( int i = 0; i < n ; ++i) {
    for ( int j = 0; j < n ; ++j) {
      int e;
      cin >> e;
      if ( e == -1 ) {
	m[i][j] = INF;
      } else {
	m[i][j] = e;
      }
    }
  }

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

int n, M[MAX][MAX];

// プリムのアルゴリズム（厳密解法)
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] = WHITE;
    }
    d[0] = 0;

    while(1){
        minv = INFTY;
        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; // uに対しての辺がなかった場合
        color[u] = BLACK;
        
        // 次点の捜索
        for(int v = 0;v<n;v++){
            if(color[v] != BLACK && M[u][v] != INFTY){
                if(d[v] > M[u][v]){
                    d[v] = M[u][v];
                    p[v] = u;
                    color[v] = GRAY;
                }
            }
        }
    }
    int sum =0;
    for(int i=0;i<n;i++){
        if(p[i] != -1) sum +=M[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;
            M[i][j] = (e== -1) ? INFTY : e;
        }
    }
    cout << prim() << 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;
int n, M[MAX][MAX];


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

	d[0] = 0;

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

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<list>
#include<string>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<cmath>
using namespace std;

const int NIL = -1;
const int MAX = 20001;

int G[101][101];
int color[101], d[101], p[101];

int main(void)
{
	int n, m, mincost, u, min = 0;
	cin >> n;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			cin >> m;
			if (m == -1)
				G[i][j] = MAX;
			else
				G[i][j] = m;
		}
	}
	for (int i = 0; i < n; i++) {
		color[i] = 0;
		d[i] = MAX;
	}
	d[0] = 0;
	p[0] = -1;
	while (1) {
		mincost = MAX;
		for (int i = 0; i < n; i++) {
			if (color[i] != 2 && d[i] < mincost) {
				mincost = d[i];
				u = i;
			}
		}
		if (mincost == MAX)
			break;
		min += mincost;
		color[u] = 2;
		for (int v = 0; v < n; v++) {
			if (color[v] != 2) {
				if (G[u][v] < d[v]) {
					d[v] = G[u][v];
					p[v] = u;
					color[v] = 1;
				}
			}
		}
	}
	cout << min << endl;
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vs = vector<string>;
using msi = map<string, int>;
using mii = map<int, int>;
using psi = pair<string, int>;
using pii = pair<int, int>;
using vlai = valarray<int>;
#define rep(i,n) for(int i=0;i<n;i++)
#define range(i,s,n) for(int i=s;i<n;i++)
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define fs first
#define sc second
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define INF 1E9
#define EPS 1E-9
#define MOD (ll)(1E9+7)
#define PI 3.1415926535897932384

template <class T>ostream &operator<<(std::ostream &o, const vector<T> &v)
{
	rep(i, v.size()) {
		o << (i > 0 ? " " : "") << v[i];
	}
	return o;
}

int n;
vvi cost;
vi mincos;
vector<bool> used;

int prim() {
	rep(i, n) {
		mincos[i] = INF;
		used[i] = false;
	}
	mincos[0] = 0;
	int res = 0;
	while (1) {
		int v = -1;
		rep(u, n) {
			if(!used[u]&&(v==-1||mincos[u]<mincos[v])){
				v = u;
			}
		}
		if (v == -1) break;
		used[v] = true;
		res += mincos[v];

		rep(u, n) {
			mincos[u] = min(mincos[u], cost[v][u]);
		}
	}
	return res;
}

int main() {
	int a,b;
	cin >> n ;
	cost.resize(n);
	mincos.resize(n);
	used.resize(n);
	for (auto &i : cost) {
		i.resize(n);
	}
	rep(i, n) {
		rep(j, n) {
			cin >> cost[i][j];
			if (cost[i][j] == -1) cost[i][j] = INF;
		}
	}
	cout << prim() << endl;
}

C++: #include <bits/stdc++.h>
#define r(i,a,n) for(int i=a;i<n;i++)
using namespace std;
int main(){
	int n,p;
	while(cin>>n>>p,n){
		int q,a[10000][1261],ch[10000][1261],b,c,d,e;
		r(i,0,p)r(j,0,1261)ch[i][j]=a[i][j]=0;
		cin>>q;
		r(i,0,q){
			cin>>b>>c>>d>>e;
			if(e==1){a[d-1][b]++;ch[d-1][b]=c;}
			else{
				for(int j=b-1;;j--){
					if(ch[d-1][j]==c)break;
					a[d-1][j]++;
				}
			}
		}
		cin>>q;
		while(q--){
			int s=0;
			cin>>b>>c>>d;
			r(i,b,c)if(a[d-1][i])s++;
			cout<<s<<endl;
		}
	}
}