Go: package main

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

const (
	WHITE int = iota
	GRAY
	BLACK
)

type graph struct {
	G [][]int
	C []int
	D []int
	P []int
}

var INFTY = 1 << 32

func constructGraph() *graph {
	sc := bufio.NewScanner(os.Stdin)
	sc.Scan()
	n, _ := strconv.Atoi(sc.Text())

	g := make([][]int, n)
	c := make([]int, n)
	d := make([]int, n)
	p := make([]int, n)

	for i := 0; i < n; i++ {
		sc.Scan()
		fields := strings.Fields(sc.Text())
		g[i] = toIntSlice(fields)
	}

	return &graph{g, c, d, p}
}

func (g *graph) initMst() {
	for i := 0; i < len(g.G); i++ {
		g.C[i] = WHITE
		g.D[i] = INFTY
	}
	g.D[0] = 0
	g.P[0] = -1
}

func (g *graph) primMst() int {
	g.initMst()

	var u int
	for {
		mincost := INFTY
		for i := 0; i < len(g.G); i++ {
			if g.C[i] != BLACK && g.D[i] < mincost {
				mincost = g.D[i]
				u = i
			}
		}

		if mincost == INFTY {
			break
		}

		g.C[u] = BLACK

		for i := 0; i < len(g.G); i++ {
			if g.C[i] != BLACK && g.existEdge(u, i) {
				if g.G[u][i] < g.D[i] {
					g.D[i] = g.G[u][i]
					g.P[i] = u
				}
			}
		}
	}

	totalCost := 0
	for _, cost := range g.D {
		totalCost += cost
	}
	return totalCost
}

func (g *graph) existEdge(u, v int) bool {
	return g.G[u][v] != INFTY && g.G[v][u] != INFTY
}

func toIntSlice(s []string) []int {
	a := make([]int, len(s))
	for i, v := range s {
		num, _ := strconv.Atoi(v)
		if num == -1 {
			num = INFTY
		}
		a[i] = num
	}
	return a
}

func main() {
	g := constructGraph()
	fmt.Println(g.primMst())
}

C++: #include <map>
#include <set>
#include <list>
#include <cmath>
#include <deque>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <vector>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <numeric>
#include <utility>
#include <iostream>
#include <algorithm>
#include <functional>
#include <unordered_map>
#include <unordered_set>

using namespace std;

#define ll long long
#define ull unsigned long long
#define INF (1 << 29)
#define LLINF (1LL << 61)
#define MOD7 1000000007
#define MOD9 1000000009
#define eps 1e-9
#define P pair<int, int>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pul pair<ull, ull>
#define all(a) (a).begin(), (a).end()
#define FOR(i,a,b) for (int i=(a);i<(b);++i)
#define RFOR(i,a,b) for (int i=(b)-1;i>=(a);--i)
#define REP(i,n) for (int i=0;i<(n);++i)
#define RREP(i,n) for (int i=(n)-1;i>=0;--i)

void YES() { cout << "YES" << endl; }
void NO() { cout << "NO" << endl; }
void Yes() { cout << "Yes" << endl; }
void No() { cout << "No" << endl; }
void yes() { cout << "yes" << endl; }
void no() { cout << "no" << endl; }

template<class T>
class Graph {
public:
	class Edge {
	public:
		int to;
		T cost;

		Edge(int t, T c) : to(t), cost(c) {}
	};

	vector<vector<Edge>> G;
	int V;

	Graph(int n = 0) {
		resize(n);
	}

	void resize(int n) {
		V = n;
		G.resize(n);
	}

	void addEdge(int from, int to, T cost = 0) {
		int v = max(from, to);
		if (v >= V)
			resize(v + 1);
		G[from].push_back(Edge(to, cost));
	}

	void dfs(int s) {
		vector<int> visit(V);
		calcDfs(visit, s);
	}

	void calcDfs(vector<int>& visit, int s) {
		if (visit[s])
			return;

		visit[s] = 1;
		for (int i = 0; i < G[s].size(); ++i) {
			calcDfs(visit, G[s][i].to);
		}
	}

	vector<T> bfs(int s) {
		vector<T> dist(V, -1);
		queue<int> que;

		dist[s] = 0;
		que.push(s);
		while (!que.empty()) {
			int from = que.front();
			que.pop();
			for (int i = 0; i < G[from].size(); ++i) {
				int to = G[from][i].to;
				if (dist[to] != -1)
					continue;
				dist[to] = dist[from] + G[from][i].cost;
				que.push(to);
			}
		}

		return dist;
	}

	vector<T> dijkstra(int s) {
		vector<T> dist(V, INF);
		priority_queue<pair<T, int>, vector<pair<T, int>>, greater<pair<T, int>>> que;

		dist[s] = 0;
		que.push(make_pair(0, s));
		while (!que.empty()) {
			auto f = que.top();
			que.pop();

			T cost = f.first;
			int from = f.second;

			for (int i = 0; i < G[from].size(); ++i) {
				int to = G[from][i].to;
				if (dist[to] > cost + G[from][i].cost) {
					dist[to] = cost + G[from][i].cost;
					que.push(make_pair(dist[to], to));
				}
			}
		}

		return dist;
	}

	T dijkstra(int s, int e) {
		vector<T> dist(V, INF);
		priority_queue<pair<T, int>, vector<pair<T, int>>, greater<pair<T, int>>> que;

		dist[s] = 0;
		que.push(make_pair(0, s));
		while (!que.empty()) {
			auto f = que.top();
			que.pop();

			T cost = f.first;
			int from = f.second;

			if (from == e) {
				return cost;
			}

			for (int i = 0; i < G[from].size(); ++i) {
				int to = G[from][i].to;
				if (dist[to] > cost + G[from][i].cost) {
					dist[to] = cost + G[from][i].cost;
					que.push(make_pair(dist[to], to));
				}
			}
		}

		return -1;
	}

	vector<vector<T>> warshallFloyd() {
		vector<vector<T>> dist(V, vector<T>(V, INF));
		for (int i = 0; i < V; ++i) {
			dist[i][i] = 0;
			for (int j = 0; j < G[i].size(); ++j) {
				dist[i][G[i][j].to] = G[i][j].cost;
			}
		}

		for (int k = 0; k < V; ++k)
			for (int i = 0; i < V; ++i)
				for (int j = 0; j < V; ++j)
					dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);

		return dist;
	}

	/*T prim(int s) {
		T dst = 0;
		vector<int> visit(V);
		priority_queue<pair<T, int>, vector<pair<T, int>>, greater<pair<T, int>>> que;
		que.push(make_pair(0, s));
		while (!que.empty()) {
			auto from = que.top();
			que.pop();

			if (visit[from.second] != 2) {
				dst += from.first;
				visit[from.second] = 2;
			}

			for (int i = 0; i < G[from.second].size(); ++i) {
				auto tmp = G[from.second][i];
				if (visit[tmp.to] != 2) {
					visit[tmp.to] = 1;
					que.push(make_pair(tmp.cost, tmp.to));
				}
			}
		}

		return dst;
	}*/

	T prim(int s) {
		T dst = 0;
		vector<int> visit(V);
		priority_queue<pair<T, int>, vector<pair<T, int>>, greater<pair<T, int>>> que;
		que.push(make_pair(0, s));
		while (!que.empty()) {
			auto from = que.top();
			que.pop();

			if (visit[from.second])
				continue;

			dst += from.first;
			visit[from.second] = 1;

			for (int i = 0; i < G[from.second].size(); ++i) {
				auto tmp = G[from.second][i];
				if (!visit[tmp.to]) {
					que.push(make_pair(tmp.cost, tmp.to));
				}
			}
		}

		return dst;
	}
};

int main() {
	int n, c;
	Graph<int> g;
	cin >> n;
	for (int i = 1; i <= n; ++i) {
		for (int j = 1; j <= n; ++j) {
			cin >> c;
			if (c != -1) {
				g.addEdge(i, j, c);
			}
		}
	}

	cout << g.prim(1) << endl;

	return 0;
}

C++: #include <iostream>
#include <vector>
#include <queue>
#include <tuple>

int main(int argc, char const *argv[]) {
  int n;
  std::vector<std::vector<int> > a;
  std::cin >> n; // .. 100
  a = std::vector<std::vector<int> >(n, std::vector<int>(n));
  // a_ij <= 2000, a_ij = a_ji, this graph is connected;

  for (size_t i = 0; i < n; i++) {
    for (size_t j = 0; j < n; j++) {
      std::cin >> a[i][j];
    }
  }

  //ALGORITHM
  std::vector<bool> found_vtx(n, false);
  // std::tuple<int,int,int> x(1,2,3);
  std::priority_queue<
    std::tuple<int,int,int>,
    std::vector<std::tuple<int, int, int> >,
    std::greater<std::tuple<int, int, int> >
  > q;

  int new_elm = 0;
  found_vtx[new_elm] = true;
  for (size_t i = 0; i < n; i++) {
    q.push(std::make_tuple(a[new_elm][i], new_elm, i));
  }
  int mst_weight = 0;
  while(!q.empty()){
    auto x = q.top(); q.pop();
    // std::cout << "e: "
    //   << std::get<0>(x) << " "
    //   << std::get<1>(x) << " "
    //   << std::get<2>(x) << std::endl;
    if(found_vtx[std::get<2>(x)] || std::get<0>(x) < 0) continue;
    else{
      mst_weight += std::get<0>(x);
      // std::cout << std::get<0>(x) << std::endl;
      new_elm = std::get<2>(x);
      found_vtx[new_elm] = true;
      for (size_t i = 0; i < n; i++) {
        q.push(std::make_tuple(a[new_elm][i], new_elm, i));
      }
    }
  }

  std::cout << mst_weight << std::endl;
  return 0;
}