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;

typedef pair< int , int > P;
struct Edge {
  int to,cost;
  Edge(){}
  Edge(int to,int cost):to(to),cost(cost){};
};
vector< vector< Edge > > Graph;
int V,E;
int prim(){
  int total = 0;
  vector< bool > used(Graph.size(),false);
  priority_queue< P,vector< P >,greater< P > > que;
  que.push(P(0,0));
  while(!que.empty()){
    int cost = que.top().first;
    int now = que.top().second;
    que.pop();
    if(used[now]) continue;
    used[now] = true;
    total += cost;
    for(int i=0;i<Graph[now].size();i++){
      que.push(P(Graph[now][i].cost,Graph[now][i].to));
    }
  }
  return total;
}

int main(){
  cin >> V;
  Graph.resize(V+1);
  /*
  for(int i=0;i<E;i++){
    int s,t,d;
    cin >> s >> t >> d;
    Graph[s].push_back(Edge(t,d));
    Graph[t].push_back(Edge(s,d));
    }*/
  for(int i=0;i<V;i++){
    for(int j=0;j<V;j++){
      int d; cin >> d;
      if(d != -1){
	Graph[i].push_back(Edge(j,d));
	Graph[j].push_back(Edge(i,d));
      }
    }
  }
  cout << prim() << endl;
}

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

int main(){
  vector<vector<int> > graph;
  vector<int> master_node;
  vector<int> distance;
  vector<int> to_neighbor;
  int n;

  cin >> n;
  n++;
  graph.resize(n);
  master_node.resize(n);
  distance.resize(n);
  to_neighbor.resize(n);
  for(int i=1;i<n;i++){
    graph[i].resize(n);
    master_node[i]=0;
    distance[i]=INT_MAX;
    to_neighbor[i]=INT_MAX;
  }

  for(int i=1;i<n;i++){
    for(int j=1;j<n;j++){
      int input;

      cin >> input;
      graph[i][j]=(input!=-1?input:INT_MAX);
    }
  }


  int from=1;

  master_node[1]=1;
  distance[1]=0;
  for(int i=1;i<n;i++){
    to_neighbor[i]=(graph[1][i]<to_neighbor[i]?graph[1][i]:to_neighbor[i]);
  }
  while(true){
    int min_distance=INT_MAX,to;

    for(int i=1;i<n;i++){
      if((to_neighbor[i]<min_distance)&&(master_node[i]==0)){
        min_distance=to_neighbor[i];
	to=i;
      }
    }
    if(min_distance==INT_MAX){
      break;
    }else{
      master_node[to]=from;
      distance[to]=min_distance;
      from=to;
      to_neighbor[from]=INT_MAX;
      for(int i=1;i<n;i++){
        to_neighbor[i]=(graph[from][i]<to_neighbor[i]?graph[from][i]:to_neighbor[i]);
      }
    }
  }


  int sum=0;
  
  for(int i=1;i<n;i++){
    sum+=distance[i];
  }
  cout << sum << endl;
}

C++: #include<iostream>
#include<iomanip>
#include<algorithm>
#include<bitset>
#include<cctype>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<functional>
#include<limits>
#include<list>
#include<map>
#include<set>
#include<stack>
#include<string>
#include<sstream>
#include<queue>
#include<vector>
using namespace std;

#define MOD 1000000007
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL

#define MAX 100
#define WHITE 0
#define GRAY 1
#define BLACK 2

int n, M[MAX][MAX];

int prim() {
	int d[MAX],
		p[MAX] = { -1 },
		color[MAX] = {};//WHITE
	fill(d, d + MAX, INF);
	d[0] = 0;

	while (true) {
		int u, minv = INF;
		for (int i = 0; i < n; i++) {
			if (color[i] != BLACK&&d[i] < minv) {
				minv = d[i];
				u = i;
			}
		}

		if (minv == INF) { break; }

		color[u] = BLACK;

		for (int v = 0; v < n; v++) {
			if (color[v] != BLACK&&M[u][v] != INF) {
				if (M[u][v] < d[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 a; cin >> a;
			M[i][j] = (a == -1) ? INF : a;
		}
	}

	cout << prim() << endl;

	return 0;
}

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

using namespace std;
static const int MAX=100;
static const int WHITE=1;
static const int GRAY=2;
static const int BLACK=3;

int n,M[MAX+10][MAX+10];

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

    for(int i=0;i<n;i++)
    {
      d[i]=INT_MAX;
      p[i]=-1;
      color[i]=BLACK;
    }

    d[0]=0;

    while(1)
    {
        minv=INT_MAX;
        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]!=INT_MAX)
        {
            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++)
      for(int j=0;i<n;j++)
    {
        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)? INT_MAX:e;
    }

    cout<<prim()<<endl;
    return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i, n) for(int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;

static const int INFTY = 1 << 21;
static const int MAX = 100;
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];
    rep(i, n) {
        d[i] = INFTY;
        p[i] = -1;
        color[i] = WHITE;
    }
    d[0] = 0;
    while(1) {
        minv = INFTY;
        u = -1;

        // Tに加える頂点:rを選ぶ
        // 最初はd[0]=0以外はINFTYなので、d[0]が選ばれる
        // 探索を１度でもしている頂点の中から、最小の頂点を選ぶ
        // 探索が行われていない頂点は d[v] にINFTYが入っているため判別可能
        rep(i, n) {
            if(minv > d[i] && color[i] != BLACK) {
                u = i;
                minv = d[i];
            }
        }

        if(u == -1) {
            break;
        }
        color[u] = BLACK;

        // rの辺を調べる
        // ここでは、「コストの更新」を目的にしている　d[v]には頂点:vの辺の中から探索時の最小の値が入っている。
        // ここで計算した各辺のコストを使って、次のループで最小のものをrとして選ぶ
        rep(v, n) {
            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;
    rep(i, n) {
        if(p[i] != -1)
            // 計算した最小の辺を選んで足していく作業
            // どの辺からその頂点にアクセスするのが一番コストが低いのかは
            // p が保持している。
            sum += M[i][p[i]];
    }
    return sum;
}
int main() {
    // 制約見て！！！デカかったらlong longにすること！
    cin >> n;
    rep(i, n) {
        rep(j, n) {
            scanf("%d", &M[i][j]);
            if(M[i][j] == -1) {
                M[i][j] = INFTY;
            }
        }
    }

    cout << prim() << endl;
}