Go: package main

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

const (
	MAX_N      = 200
	MAX_WEIGHT = 2000
)

type Edge struct {
	start  int
	end    int
	weight int
}

type ShortestInfo struct {
	edges    []Edge
	shortest int
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	n, err := nextInt(sc)
	if err != nil {
		panic(err)
	}

	shortestList := make([]ShortestInfo, n)
	for i := 1; i < n; i++ {
		shortestList[i] = ShortestInfo{edges: []Edge{}, shortest: MAX_N*MAX_WEIGHT + 1}
	}

	adjacentMatrix := make([][]int, n)
	for i := 0; i < n; i++ {
		adjacentMatrix[i] = make([]int, n)
		for j := 0; j < n; j++ {
			v, err := nextInt(sc)
			if err != nil {
				panic(err)
			}
			adjacentMatrix[i][j] = v
		}
	}

	mst([]int{1}, shortestList, adjacentMatrix)
	fmt.Println(shortestList)
	fmt.Println(calculateMinWeight(shortestList))
}

func calculateMinWeight(shortestList []ShortestInfo) int {
	visitedList := make([]bool, len(shortestList))
	visitedList[0] = true
	sumWeight := 0
	for !visitedAll(visitedList) {
		shortestInfo := shortestList[0]
		shortestList = shortestList[1:]
		edges := shortestInfo.edges
		for _, edge := range edges {
			if !visitedList[edge.end-1] {
				visitedList[edge.end-1] = true
				sumWeight += edge.weight
			}
		}
	}
	return sumWeight
}

func visitedAll(visitedList []bool) bool {
	for _, v := range visitedList {
		if !v {
			return false
		}
	}
	return true
}

func mst(nodeStack []int, shortestList []ShortestInfo, adjacentMatrix [][]int) {
	for len(nodeStack) > 0 {
		currentNode := nodeStack[0]
		nodeStack = nodeStack[1:]
		currentShortest := getShortest(currentNode, shortestList)
		adjacents := findAdjacents(currentNode, adjacentMatrix)
		for i := 0; i < len(adjacents); i++ {
			adj := adjacents[i]
			s := getShortest(adj.end, shortestList)
			if currentShortest.shortest+adj.weight < s.shortest || (currentShortest.shortest+adj.weight == s.shortest && len(currentShortest.edges)+1 > len(s.edges)) {
				setShortest(adj.end, adj, currentShortest, shortestList)
				nodeStack = append(nodeStack, adj.end)
			}
		}
	}
}

func getShortest(node int, shortestList []ShortestInfo) ShortestInfo {
	return shortestList[node-1]
}

func setShortest(node int, edge Edge, currentShortest ShortestInfo, shortestList []ShortestInfo) {
	newEdges := make([]Edge, len(currentShortest.edges)+1)
	for i := 0; i < len(newEdges)-1; i++ {
		newEdges[i] = currentShortest.edges[i]
	}
	newEdges[len(newEdges)-1] = edge
	shortestInfo := ShortestInfo{edges: newEdges, shortest: currentShortest.shortest + edge.weight}
	shortestList[node-1] = shortestInfo
}

func findAdjacents(currentNode int, adjacentMatrix [][]int) []Edge {
	adjacents := adjacentMatrix[currentNode-1]
	edges := []Edge{}
	for i, v := range adjacents {
		if v > 0 {
			edges = append(edges, Edge{start: currentNode, end: i + 1, weight: v})
		}
	}
	return edges
}

func nextInt(sc *bufio.Scanner) (int, error) {
	sc.Scan()
	return strconv.Atoi(sc.Text())
}

C++: #include<iostream>
#include<algorithm>
#include<string>
#include<cstdlib>
#include<map>
#include<iomanip>
#include<sstream>
#include<vector>
#include<stack>
#include<math.h>
#include<queue>
#include<complex>
#include<random>
#include<ctime>
using namespace std;
long long int INF=99999999999999;

//INFを宣言すること。Edgeに辺の情報を入れよう。
class Graph{
//メンバ変数の説明
//Edge:一つ目の番号が辺が出る方向
//MaxV,MaxE:頂点数と辺の数
//Result系:各メソッドの答え
//InfinteUpdate:BellmanFordで各番号の頂点までの最短経路が更新されるか
//
public:
	vector<pair<long long int,long long int> > *Edge;
	vector<pair<pair<long long int,long long int>,long long int > >PrimEdge;
	long long int MaxV,MaxE;
	long long int *BellmanFordResult,*DijkstraResult,**WarshallFloydResult;
	bool *InfiniteUpdate;
	bool NegativeLoop=false;
	long long int PrimResult=0;
	bool NotConected=false;


	Graph(long long int InputV,long long int InputE){
		MaxV=InputV;
		MaxE=InputE;
		Edge=new vector<pair<long long int,long long int> >[InputV+1];

	}
	
	void BellmanFord(long long int Start) {
		BellmanFordResult=new long long int[MaxV];
		InfiniteUpdate=new bool[MaxV];
		for (int i = 0; i < MaxV; i++) { BellmanFordResult[i] = INF;InfiniteUpdate[i]=false; }
		BellmanFordResult[Start] = 0;
		long long int turn=0;
		while (true) {
			turn++;
			bool update = false;
			for (int i = 0; i < MaxV; i++) {
				for(int j=0;j<Edge[i].size();j++){
					pair<long long int,long long int> e = Edge[i][j];
					if(turn>=MaxV&&InfiniteUpdate[i])
					{
						InfiniteUpdate[e.first]=true;
					}
					if (BellmanFordResult[i] != INF && BellmanFordResult[e.first] > BellmanFordResult[i] + e.second) {
						BellmanFordResult[e.first] = BellmanFordResult[i] + e.second;
						update = true;
						if(turn>=MaxV){
							InfiniteUpdate[e.first]=true;
						}
					}
				}
			}
			if (!update) { 
				NegativeLoop=false;
				break; 
			}
			if(turn>=2*MaxV-1){
				NegativeLoop=true;
				break;
			}
		}
	}

	void Dijkstra(long long int Start) {
		DijkstraResult=new long long int[MaxV];
		priority_queue<pair<long long int,long long int>, vector<pair<long long int,long long int> >, greater<pair<long long int,long long int> > >que;
		fill(DijkstraResult, DijkstraResult + MaxV, INF);
		DijkstraResult[Start] = 0;
		que.push(pair<long long int,long long int>(0, Start));

		while (!que.empty()) {
			pair<long long int,long long int> p = que.top();
			que.pop();
			long long int v = p.second;
			if (DijkstraResult[v] < p.first)continue;
			for (int i = 0; i < Edge[v].size(); i++) {
				pair<long long int,long long int> e = Edge[v][i];
				if (DijkstraResult[e.first] > DijkstraResult[v] + e.second) {
					DijkstraResult[e.first] = DijkstraResult[v] + e.second;
					que.push(pair<long long int,long long int>(DijkstraResult[e.first], e.first));
				}
			}
		}
	}

	void WarshallFloyd() {
		WarshallFloydResult=new long long int*[MaxV];
		for(int i=0;i<MaxV;i++){
			WarshallFloydResult[i]=new long long int[MaxV];
		}
		for(int i=0;i<MaxV;i++){
			for(int j=0;j<Edge[i].size();j++){
				WarshallFloydResult[i][Edge[i][j].first]=Edge[i][j].second;
			}
		}

		for (int k = 0; k < MaxV; k++) {
			for (int i = 0; i < MaxV; i++) {
				for (int j = 0; j < MaxV; j++) {
					WarshallFloydResult[i][j] = min(WarshallFloydResult[i][j], WarshallFloydResult[i][k] + WarshallFloydResult[k][j]);
				}
			}
		}
	}
	
	 void Prim(){
	 	NotConected=false;
	 	priority_queue<pair<long long int,pair<long long int,long long int> >,vector<pair<long long int,pair<long long int,long long int> > > ,greater<pair<long long int,pair<long long int,long long int> > > >PriorityEdge;
	 	bool *IncludedVertex=new bool[MaxV];
	 	fill(IncludedVertex,IncludedVertex+MaxV,false);
	 	PrimEdge.clear();
	 	for(int i=0;i<Edge[0].size();i++)
	 	{
	 		PriorityEdge.push({Edge[0][i].second,{0,Edge[0][i].first}});
	 	}
	 	IncludedVertex[0]=true;
	 	for(int i=0;i<MaxV-1;i++)
	 	{
	 		while(1)
	 		{
	 			if(PriorityEdge.empty())
	 			{
	 				NotConected=true;
	 				break;
	 			}
	 			pair<long long int,pair<long long int,long long int> > pe = PriorityEdge.top();
	 			PriorityEdge.pop();
	 			//cout<<pe.first<<" "<<pe.second.first<<" "<<pe.second.second<<endl;
	 			if(!IncludedVertex[pe.second.second])
	 			{
	 				IncludedVertex[pe.second.second]=true;
	 				PrimResult+=pe.first;
	 				PrimEdge.push_back({pe.second,pe.first});
	 				//cout<<"size"<<pe.second.second<<" "<<Edge[pe.second.second].size()<<endl;
	 				for(int j=0;j<Edge[pe.second.second].size();j++)
	 				{
	 					PriorityEdge.push({Edge[pe.second.second][j].second,{pe.second.second,Edge[pe.second.second][j].first}});
	 				}
	 				break;
	 			}
	 			
	 			
	 		}
	 	}
	 }
};

int main(){
	
	long long int N,M=0,P,a[101][101];
	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)
			{
				M++;
			}
		}	
	}
	
	Graph graph(N,M);
	for(int i=0;i<N;i++)
	{
		for(int j=0;j<N;j++)
		{
			if(a[i][j]!=-1)
			{
				graph.Edge[i].push_back({j,a[i][j]});
			}
		}	
	}
	graph.Prim();
	cout<<graph.PrimResult<<endl;
	
}