Hdu 1233 is still a smooth Project (query set + quick release)

Source: Internet
Author: User

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;
}

 


 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.