//每次选择最短的边,同时保证边的两个端点并不相连(判断相连可用并查集),之前先排序
typedef struct s
{
int a;
int b;
int c;
}s;
s w[101];
int pre[101];
int find(int i)
{
int temp,j=i;
while (pre[i]!=i)
{
i=pre[i];
}
while (j!=i)
{
temp=pre[j];
pre[j]=i;
j=temp;
}
return i;
}
int merge(int c,int d)
{
int t1,t2;
t1=find(c);
t2=find(d);
if (t1!=t2)
{
pre[t2]=t1;
return 1;
}
return 0;
}
void quicksort(int left,int right)
{
int low=left;
int high=right;
s temp=w[left];
int key=w[left].c;
if (left<right)
{
while (low<high)
{
while ((low<high)&&(w[high].c>=key))
high--;
w[low]=w[high];
while ((low<high)&&(w[low].c<=key))
low++;
w[high]=w[low];
}
w[low]=temp;
quicksort(left,low-1);
quicksort(low+1,right);
}
}
#include <stdio.h>
int main()
{
int i,n,m,count=0,sum=0;
scanf("%d%d",&n,&m);
for (i=1;i<=m;i++)
scanf("%d%d%d",&w[i].a,&w[i].b,&w[i].c);
quicksort(1,m);
for (i=1;i<=n;i++)
pre[i]=i;
for (i=1;i<=m;i++)
{
if (merge(w[i].a,w[i].b))//如果不相连
{
count++;
sum+=w[i].c;
}
if (count==n-1)
break;
}
printf("%d\n",sum);
return 0;
}