#include <iostream>
using namespace std;
int f[101]; //存放每个点所在的树的根节点
struct h //线段
{
int x; //点1
int y; //点2
int z; //长度
}a[101];
void quicksort(h a[], int low, int high) //快排 从小到大排出边的长度
{
if (low < high)
{
int i = low;
int j = high;
while (i < j)
{
if (i < j&&a[j].z >= a[low].z) j--;
if (i < j&&a[i].z <= a[low].z) i++;
swap(a[i], a[j]);
}
if (i == j) swap(a[i], a[low]);
quicksort(a, low, i - 1);
quicksort(a, i + 1, high);
}
}
void inti(int f[], int m) //初始化 每个点都是独立的树
{
for (int i = 1; i <= m; i++) f[i] = i;
}
int getf(int f[], int x) //获取根节点
{
if (f[x] == x) return x;
else
{
f[x] = getf(f, f[x]);
return f[x];
}
}
int merge(int f[], int v, int u) //并查集
{
int t1 = getf(f, v);
int t2 = getf(f, u);
if (t1 != t2) //两者的根节点不同 则通过靠左原则合并
{
f[t2] = t1;
return 1;
}
else return 0;
}
int main()
{
int m;
int n;
while(cin>>m>>n)
{
if (n < m - 1) cout << "不符合要求!!!" << endl; //情况一:有的点没有其他任何点有连线 不符合
else
{
int sum = 0;
for (int i = 1; i <= n; i++)
{
cin >> a[i].x >> a[i].y >> a[i].z;
sum += a[i].z;
}
if (m - 1 == n) cout << "最短长度是:" << sum << endl; //恰好
else //核心情况
{
int leng = 0;
int count = 0;
quicksort(a, 1, n);
inti(f, n);
for (int i = 1; i <= n; i++)
{
if (merge(f, a[i].x, a[i].y))
{
count++;
leng += a[i].z;
}
if (count == n - 1) break;
}
cout << "最短长度是:" << leng << endl;
cout << endl;
}
}
}
return 0;
}
using namespace std;
int f[101]; //存放每个点所在的树的根节点
struct h //线段
{
int x; //点1
int y; //点2
int z; //长度
}a[101];
void quicksort(h a[], int low, int high) //快排 从小到大排出边的长度
{
if (low < high)
{
int i = low;
int j = high;
while (i < j)
{
if (i < j&&a[j].z >= a[low].z) j--;
if (i < j&&a[i].z <= a[low].z) i++;
swap(a[i], a[j]);
}
if (i == j) swap(a[i], a[low]);
quicksort(a, low, i - 1);
quicksort(a, i + 1, high);
}
}
void inti(int f[], int m) //初始化 每个点都是独立的树
{
for (int i = 1; i <= m; i++) f[i] = i;
}
int getf(int f[], int x) //获取根节点
{
if (f[x] == x) return x;
else
{
f[x] = getf(f, f[x]);
return f[x];
}
}
int merge(int f[], int v, int u) //并查集
{
int t1 = getf(f, v);
int t2 = getf(f, u);
if (t1 != t2) //两者的根节点不同 则通过靠左原则合并
{
f[t2] = t1;
return 1;
}
else return 0;
}
int main()
{
int m;
int n;
while(cin>>m>>n)
{
if (n < m - 1) cout << "不符合要求!!!" << endl; //情况一:有的点没有其他任何点有连线 不符合
else
{
int sum = 0;
for (int i = 1; i <= n; i++)
{
cin >> a[i].x >> a[i].y >> a[i].z;
sum += a[i].z;
}
if (m - 1 == n) cout << "最短长度是:" << sum << endl; //恰好
else //核心情况
{
int leng = 0;
int count = 0;
quicksort(a, 1, n);
inti(f, n);
for (int i = 1; i <= n; i++)
{
if (merge(f, a[i].x, a[i].y))
{
count++;
leng += a[i].z;
}
if (count == n - 1) break;
}
cout << "最短长度是:" << leng << endl;
cout << endl;
}
}
}
return 0;
}