#include <string>
#include <vector>
#include <queue>
#include <limits.h>
using namespace std;
#define PII pair<int,int>
priority_queue<PII, vector<PII>, greater<PII>> vertex;
vector<PII> *node;
vector<bool> visited;
int addEdge(int node);
int prim(int n);
int solution(int n, vector<vector<int>> costs) {
int answer = 0;
node = new vector<PII>[n];
for(int i=0; i<costs.size(); i++){
node[costs[i][0]].push_back(PII(costs[i][2],costs[i][1]));
node[costs[i][1]].push_back(PII(costs[i][2],costs[i][0]));
}
return answer = prim(n);
}
/*void addEdge(int n){
for(int i=0; i<node[n].size(); i++){
vertex.push(node[n][i]);
}
}*/
int addEdge(int n) {
for (auto edge : node[n]) vertex.push(edge);
}
int prim(int n){
int res = 0;
visited.resize(n, false);
vertex.push(PII(0,0));
for(int i=0; i<n; i++){
int now = -1, minVal = INT_MAX;
while(!vertex.empty()){
now = vertex.top().second;
if(!visited[now]){
minVal = vertex.top().first;
break;
}
vertex.pop();
}
if(minVal == INT_MAX) return INT_MAX;
res += minVal;
visited[now] = true;
addEdge(now);
}
return res;
}