Go: package main

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

// I/O
type Scanner struct {
	sc *bufio.Scanner
}

func NewScanner() *Scanner {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 1024), int(1e+9))
	return &Scanner{sc}
}

func (s *Scanner) nextStr() string {
	s.sc.Scan()
	return s.sc.Text()
}

func (s *Scanner) nextInt() int {
	i, e := strconv.Atoi(s.nextStr())
	if e != nil {
		panic(e)
	}
	return i
}

func (s *Scanner) nextFloat() float64 {
	f, e := strconv.ParseFloat(s.nextStr(), 64)
	if e != nil {
		panic(e)
	}
	return f
}

func (s *Scanner) nextRuneSlice() []rune {
	return []rune(s.nextStr())
}

func (s *Scanner) nextIntSlice(n int) []int {
	res := make([]int, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextInt()
	}
	return res
}

func (s *Scanner) nextFloatSlice(n int) []float64 {
	res := make([]float64, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextFloat()
	}
	return res
}

// Arithmetic
func max(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m < i {
			m = i
		}
	}
	return m
}

func min(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m > i {
			m = i
		}
	}
	return m
}

func abs(x int) int {
	if x > 0 {
		return x
	}
	return -x
}

func pow(x, y int) int {
	res := 1
	for i := 0; i < y; i++ {
		res *= x
	}
	return res
}

func ceil(a, b int) int {
	if a%b == 0 {
		return a / b
	} else {
		return a/b + 1
	}
}

// Sort
type RuneSlice []rune

func (a RuneSlice) Len() int           { return len(a) }
func (a RuneSlice) Less(i, j int) bool { return a[i] < a[j] }
func (a RuneSlice) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

const INF = 1 << 60

// Main
func main() {
	sc := NewScanner()
	wtr := bufio.NewWriter(os.Stdout)
	N := sc.nextInt()
	G := make([][]int, N)
	for i := 0; i < N; i++ {
		G[i] = make([]int, N)
		for j := 0; j < N; j++ {
			G[i][j] = sc.nextInt()
		}
	}
	d := make([]int, N)
	for i := 0; i < N; i++ {
		d[i] = INF
	}
	seen := make([]bool, N)
	d[0] = 0
	ans := 0
	for {
		cur := 0
		minCost := INF
		for i := 0; i < N; i++ {
			if !seen[i] && d[i] < minCost {
				minCost = d[i]
				cur = i
			}
		}
		if minCost == INF {
			break
		}
		seen[cur] = true
		ans += d[cur]
		for i := 0; i < N; i++ {
			cost := G[cur][i]
			if cost != -1 && cost < d[i] {
				d[i] = cost
			}
		}
	}

	fmt.Fprintln(wtr, ans)
	wtr.Flush()
}

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

static const int MAX = 10000;
static const int INFTY = (1<<21);
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;

int n;
vector<pair<int,int> > adj[MAX];

void dijkstra(){
    priority_queue<pair<int,int> > PQ;
    int color[MAX];
    int d[MAX];
    for(int i=0; i<n; i++){
        d[i] = INFTY;
        color[i] = WHITE;
    }
    
    d[0] = 0;
    PQ.push(make_pair(0,0));
    color[0] = GRAY;
    
    while(!PQ.empty()){
        pair<int,int> f = PQ.top(); PQ.pop();
        int u = f.second;
        color[u] = BLACK;
        
        if(d[u] < f.first*(-1)) continue;
        
        for(int j=0; j<adj[u].size(); j++){
            int v = adj[u][j].first;
            if(color[v]==BLACK) continue;
            if(d[v]>d[u]+adj[u][j].second){
                d[v] = d[u]+adj[u][j].second;
                PQ.push(make_pair(d[v]*(-1),v));
                color[v] = GRAY;
            }
        }
    }
    
    for(int i=0; i<n; i++){
        cout << i << " " << (d[i]==INFTY ? -1 : d[i]) << endl;
    }
}

int main(){
    int k,u,v,c;
    
    cin >> n;
    for(int i=0; i<n; i++){
        cin >> u >> k;
        for(int j=0; j<k; j++){
            cin >> v >> c;
            adj[u].push_back(make_pair(v,c));
        }
    }
    
    dijkstra();
    
    return 0;
}

C++: #include<iostream>

using namespace std;

int main()
{
    int n, A[100][100], B[100] = { 0 };
    int min;
    cin >> n;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            cin >> A[i][j];
            if(A[i][j]!='-1')
            B[i] += A[i][j];
        }
    }
    min = B[0];
    for (int i = 1; i < n; i++) {
        if (min > B[i])min = B[i];
    }
    cout << min << endl;
    return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cassert>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)
#define rep(i,n) FOR(i,0,n)
#define DEBUG(x) cout<<#x<<": "<<x<<endl
#define vint vector<int>
#define vdouble vector<double>
#define vstring vector<string>
using namespace std;
#include <functional>

#include<map>
#include<set>
#include<queue>

typedef long long ll;
typedef unsigned long long ull;

const int MAX_N = 100;

int N;

class Edge{
public:
	int from, to, cost;
	Edge(int from = 0, int to = 0, int cost = 0) :
		from(from), to(to), cost(cost){}
	
	bool operator < (const Edge &e) const{
		return cost < e.cost;
	}
	bool operator > (const Edge &e) const{
		return cost > e.cost;
	}

};

class Node{
public:
	vector<Edge> edges;
	int color;
	Node(){
		color = 0;
	}
};

class Graph{
public:
	vector<Node> nodes;
	Graph(int n) {
		rep(i, n) { Node n;  nodes.push_back(n); }
	}
};

int main() {

	cin >> N;
	Graph g(N);

	rep(i, N)
	   rep(j, N){
		int cost; cin >> cost;
		if (cost != -1){
			Edge e(i, j, cost);
			g.nodes[i].edges.push_back(e);
		}
	}

	rep(i, N) { g.nodes[i].color = 0; }

	int i = 0;
	g.nodes[i].color = 1;

	priority_queue<Edge, vector<Edge>, greater<Edge> > pq;
	for (auto e : g.nodes[i].edges){
		pq.push(e);
	}

	int ret = 0;
	while (!pq.empty()){
		Edge e = pq.top(); pq.pop();
		Node *n = &g.nodes[e.to];
		if (n->color != 0){ continue; }
		
		n->color = 1;
		ret += e.cost;

		for (auto e : n->edges){
			pq.push(e);
		}
	}

	cout << ret << endl;
}

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

const int MAX = numeric_limits<int>::max();

class Node{
public:
  int _id;
  bool _isFound;
  vector<int> _adj_index; // 隣接点のid
  vector<int> _adj_vertices;  // 隣接点との距離(重み)

  void init(int id, int n){
    _id = id;
    _isFound = false;
    for(auto i = 0; i < n; i++){
      _adj_vertices.push_back(MAX);
    }
  }
};

void inputGraph(vector<Node>& graph){
  for(auto i = 0; i < graph.size(); i++){
    graph[i].init(i, graph.size());
    for(auto j = 0; j < graph.size(); j++){
      int tmp;
      cin >> tmp;
      if(tmp != -1){
        graph[i]._adj_index.push_back(j);
        graph[i]._adj_vertices[j] = tmp;
      }
    }
  }
}

int mst(vector<Node>& graph, int search){
  auto sum = 0;
  vector<int> searched; // 木に追加済みのNodeのid
  searched.push_back(search);
  graph[searched[0]]._isFound = true;
  int minNodeIndex;

  while(searched.size() < graph.size()){
    auto compare = MAX;

    for(auto i = 0; i < searched.size(); i++){
      for(auto j = 0; j < graph[searched[i]]._adj_index.size(); j++){
//  cout << "call in for " << j << endl;
//  cout << compare << endl;
        Node searching = graph[searched[i]];
        if(!graph[searching._adj_index[j]]._isFound &&
           searching._adj_vertices[searching._adj_index[j]] < compare){
          // 木の隣接点で最短
          compare = searching._adj_vertices[searching._adj_index[j]];
          minNodeIndex = searching._adj_index[j];
 //         cout << "added" << endl;
        }
      }
    }
    if(compare != MAX){
      sum += compare;
      graph[minNodeIndex]._isFound = true;
      searched.push_back(minNodeIndex);
//      cout << endl << "minNodeIndex:" << minNodeIndex << endl;
    }

  }
  return sum;
}

int main(){
  int n;
  cin >> n;
  vector<Node> graph(n);

  inputGraph(graph);
  cout << mst(graph, 0) << endl;

  return 0;
}