Hdu 1875 smooth engineering recontinued

Source: Internet
Author: User

/* The Minimum Spanning Tree only needs to set the edge of the graph smaller than 10 or greater than 1000 as INF,

*/

# Include <cstdio>

# Include <cstring>
# Include <cmath>
# Define INF 1 <30
Int n, m, vis [110];
Double low [110];
Double map [110] [110];
Struct Node
{
Double x, y;
} A [110];
Int init ()
{
For (int I = 1; I <= n; I ++)
For (int j = 1; j <= n; j ++)
Map [I] [j] = INF;
Memset (vis, 0, sizeof (vis ));
}
Double dis (int I, int j)
{
Return sqrt (a [I]. x-a [j]. x) * (a [I]. x-a [j]. x) + (a [I]. y-a [j]. y) * (a [I]. y-a [j]. y ));
}
Double prim ()
{
Double ans = 0;
For (int I = 0; I <= n; I ++)
Low [I] = map [1] [I];
Vis [1] = 1;
For (int I = 1; I <n; I ++)
{
Int temp = INF, pos =-1;
For (int j = 1; j <= n; j ++)
If (temp> low [j] &! Vis [j])
{
Temp = low [j];
Pos = j;
}
If (pos =-1) return-1;
Vis [pos] = 1;
Ans + = low [pos];
For (int j = 1; j <= n; j ++)
If (! Vis [j] & low [j]> map [pos] [j])
Low [j] = map [pos] [j];
}
Return ans;
}
Int main ()
{
Int t;
Scanf ("% d", & t );
While (t --)
{
Scanf ("% d", & n );
Init ();
For (int I = 1; I <= n; I ++)
{
Scanf ("% lf", & a [I]. x, & a [I]. y );
}
For (int I = 1; I <= n; I ++)
For (int j = 1; j <= n; j ++)
{
If (I = j) map [I] [j] = 0;
Else
{
Double temp = dis (I, j );
If (temp> 1000 | temp <10)
Map [I] [j] = INF;
Else map [I] [j] = temp * 100;
}
}
Double res = prim ();
If (res =-1) puts ("oh! ");
Else printf ("%. 1lf \ n", res );
}
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.