Go: package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 500001), 500000)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func main() {
	fp := os.Stdin
	if len(os.Args) > 1 {
		fp, _ = os.Open(os.Args[1])
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(os.Stdout)

	n := getNextInt(scanner)

	g := make([][]int, n)
	ans := make([][]int, n)

	inf := 1 << 30
	for i := 0; i < n; i++ {
		g[i] = make([]int, n)
		ans[i] = make([]int, n)
		for ii := 0; ii < n; ii++ {
			g[i][ii] = getNextInt(scanner)
			if g[i][ii] == -1 {
				g[i][ii] = inf
			}
			ans[i][ii] = -1
		}
	}

	im := make(IntMap, 1)

	im.increment(1)

	for len(im) < n {
		min := inf
		u := -1
		v := -1
		for i := range im {
			for ii := 0; ii < n; ii++ {
				if im.get(ii) != 0 {
					continue
				}
				if min > g[i][ii] {
					min = g[i][ii]
					u = i
					v = ii
				}
			}
		}
		im.increment(v)
		ans[u][v] = min
		ans[v][u] = min
	}

	sum := 0
	for i := 0; i < n-1; i++ {
		for ii := i + 1; ii < n; ii++ {
			if ans[i][ii] != -1 {
				sum += ans[i][ii]
			}
		}
	}

	fmt.Fprintln(writer, sum)
	writer.Flush()
}

// IntMap ...
type IntMap map[int]int

func (m IntMap) get(key int) int {
	if _, ok := m[key]; ok {
		return m[key]
	}
	return 0
}
func (m IntMap) increment(key int) {
	if _, ok := m[key]; ok == false {
		m[key] = 0
	}
	m[key]++
}

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>
using namespace std;
int map[101][101];
int dp[101];
bool decided[101];
int sum,n;
int node,nextnode,mincost;
void check(){
	node=1;
	decided[node]=true;
	dp[node]=0;
	while(1){
		nextnode=-1;
		mincost=20000;
		dp[node]=0;
		for(int i=1;i<=n;i++){
			if(map[node][i]==-1||decided[i])continue;

			if(dp[i]==-1){
				dp[i]=map[node][i]+dp[node];
			}else{
				dp[i]=min(dp[i],map[node][i]+dp[node]);
			}
			if(mincost>dp[i])nextnode=i;
		}
		if(nextnode==-1)break;
		sum+=map[node][nextnode];
		decided[nextnode]=true;
		node=nextnode;
	}
}

int main(){
	cin >>n;
	for(int i=1;i<=n;i++){
		dp[i]=-1;
	}

	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			cin >> map[i][j];
		}
	}

	check();
	cout << sum<<endl;
}

C++: #include<iostream>
#define MAX 100
#define INF (2<<29)
using namespace std;
int main(){
  int n;
  cin>>n;
  int graph[MAX][MAX];
  
  for(int i=1;i<n+1;i++){
    for(int j=1;j<n+1;j++){
      cin>>graph[i][j];
      if(graph[i][j]==-1)graph[i][j]=INF;
    }
  }
  
  int visited[MAX];
  int min[MAX];
  for(int i=1;i<n+1;i++){
    visited[i]=0;
    min[i]=INF;
  }
  
  min[1]=0;
  int ans=0;
  while(true){
    int a=-1;
    for(int i=1;i<n+1;i++){
      if(visited[i]==0&&(a==-1||min[i]<min[a]))a=i;
    }

    if(a==-1)break;
    visited[a]=1;
    ans +=min[a];

    for(int i=1;i<n+1;i++){
      min[i]=min[i]<graph[a][i] ? min[i]:graph[a][i];
    }
  }
  cout<<ans<<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<bits/stdc++.h>
#define REP(i,n) for(int i=0;i<n;i++)
#define LOOP(i,x,n) for(int i=x;i<n;i++)
#define ALL(v) (v).begin(),(v).end()
#define PB push_back
#define MP make_pair
#define FIR first
#define SEC second
#define int long long
using namespace std;
const int MOD=1000000007;
const int INF=1000000009;

typedef int Weight;
struct Edge{
  int src;int dst;
  Weight wei;
  Edge(int s=0,int d=0,Weight w=0):
    src(s),dst(d),wei(w){}
};
using Graph = vector<vector<Edge>>;
int prim(const Graph &g,const int r=0){
  struct comp{
    bool operator()(const Edge &e,const Edge &f)const {return e.wei>f.wei;}
  };
  Weight sum=0;
  vector<int> used(g.size());
  priority_queue<Edge,vector<Edge>,comp> q;
  q.emplace(-1,r,0);
  while(q.size()){
    Edge e=q.top();
    q.pop();
    if(used[e.dst])continue;
    used[e.dst]=1;
    sum+=e.wei;
    for(auto &f : g[e.dst]){
      if(!used[f.dst])q.emplace(f);
    }
  }
  return sum;
}

signed main(){
  int n;
  cin>>n;
  Graph g(n);
  REP(i,n)REP(j,n){
    int a;
    cin>>a;
    if(a!=-1){
      g[i].emplace_back(i,j,a);
    }
  }
  cout<<prim(g,0)<<endl;
  return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<vector>
#include<queue>
#include<map>
#include<string>
#include<algorithm>
#include<functional>
#define ll long long
#define inf  999999999
#define pa pair<int,int>

using namespace std;

int n,a[200][200]={0};
void prim(){
	int d[200]={0};
	priority_queue<pair<int,pa> >  PQ; 
	pair<int,pa> q;
	pa r;
	int wei=0;
	int w=6;
	int u=w,z=0;
	d[w]=1;
	while(z!=n-1){
		for(int j=n-1;j>-1;j--){
			if(a[u][j]<=0 && d[j]==0 && u!=j) PQ.push(make_pair(a[u][j],make_pair(u,j)));
		}
	while(1){
		if(PQ.size()==0) {
			cout<<"dv"<<endl;
		break;
		}
		q=PQ.top();
		PQ.pop();
		r=q.second;
	//	cout<<"rsec"<<r.second<<endl;
		if(d[r.second]==0) break;
		
		}
	u=r.second;
//	cout<<"a" <<u<<endl;
	d[u]=1;
	wei=wei+q.first;
	z++;
//	cout<<z<<" "<<-q.first<<" "<<u<<" "<<r.first<<endl;
	
	}
	cout<<-wei<<endl;
}



int main(){
	// your code goes here
	int m;
	cin>>n;
	
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
		cin>>m;
		a[i][j]=-m;
		}
	}
	prim();

	return 0;
}