It's a little harder than the last question!
(Thunder in the sky seems to rain! It's time to rain. These days are all hot. The water that I just went to WC (toilet) was hot );
The shortest path for this question is to first use quick sorting and then judge one by one. If you are in the same camp, you will not handle it. If you are not in the same camp, you need to be in the same camp. Add the path!
# Include "algorithm"
Using namespace std;
Struct node
{
Int x, y, z;
} A [10000];
Int cmp (node a, node B)
{
Return a. z <B. z;
}
Int pre [100000];
Int find (int k)
{
If (k = pre [k])
Return k;
Pre [k] = find (pre [k]);
Return pre [k];
}
Int main ()
{
Int m, I, ans, f1, f2, n;
While (scanf ("% d", & n), n)
{
For (I = 1; I <= n; I ++)
Pre [I] = I;
M = (n-1) * n/2;
For (I = 0; I <m; I ++)
Scanf ("% d", & a [I]. x, & a [I]. y, & a [I]. z );
Sort (a, a + m, cmp );
Ans = 0;
For (I = 0; I <m; I ++)
{
F1 = find (a [I]. x );
F2 = find (a [I]. y );
If (f1! = F2)
{
Pre [f1] = f2;
Ans + = a [I]. z;
}
}
Printf ("% d \ n", ans );
}
Return 0;
}