1 #include <map>
2 #include <set>
3 #include <stack>
4 #include <queue>
5 #include <cmath>
6 #include <vector>
7 #include <cstdio>
8 #include <cstring>
9 #include <algorithm>
10 using namespace std;
11 #define maxn 101
12 #define mod 1000000007
13 #define INF 0x7fffffff
14 #define ll long long
15 //#define ll __int64
16 struct Node{
17 int u, cost;
18 bool operator<(const Node& cmp)const{
19 return cost>cmp.cost;
20 }
21 };
22 struct Edge{int from, to, cost;};
23 priority_queue<Node>q;
24 vector<int>G[maxn];
25 vector<Edge>edges;
26 int vis[maxn];
27 int n, m;
28 void init(){
29 memset(vis, 0, sizeof vis);
30 while (!q.empty())q.pop();
31 for (int i = 0; i < n; i++)G[i].clear();
32 edges.clear();
33 }
34 void AddEdge(int from, int to, int cost){
35 edges.push_back((Edge){ from, to, cost });
36 edges.push_back((Edge){ to, from, cost });
37 int m = edges.size();
38 G[from].push_back(m - 2);
39 G[to].push_back(m - 1);
40 }
41 int Prim(int s){
42 int cost = 0, t = 0;
43 Node x;
44 x.cost = 0;
45 x.u = s;
46 q.push(x);
47 while (!q.empty()&&t<n){
48 x = q.top(); q.pop();
49 t++;
50 cost += x.cost;
51 vis[x.u] = 1;
52 for (int i = 0; i < G[x.u].size(); i++){
53 Edge e = edges[G[x.u][i]];
54 if (!vis[x.u])q.push((Node){ e.to, e.cost });
55 }
56 }
57 return cost;
58 }
59 int main(){
60 int cas = 1;
61 while (~scanf("%d", &n)){
62 for (int i = 0; i <= n;i++)
63 for (int j = 0; j <= n; j++)AddEdge(i, j, INF);
64 for (int i = 0; i <= n; i++)AddEdge(0, i, 0);
65 for (int i = 1; i <= n; i++){
66 int a, b, v;
67 scanf("%d", &a,&b,&v);
68 AddEdge(a, b, v);
69 }
70 int cost=Prim(0);
71 printf("%d\n", cost);
72 }
73 return 0;
74 }