World islands -- prim

Source: Internet
Author: User

Link address:

Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 3405

 

The question is to find an MST, but not n vertices. It is n-1 vertices, And the other vertices are isolated.

 

I isolated n vertices and each vertex, figured out the shortest path of all MST, and finally found the minimum value.

 

Code# Include <cstdlib> <br/> # include <cmath> <br/> # include <algorithm> <br/> # include <vector> <br/> # include <iostream> <br/> # include <cstdio> <br/> using namespace STD; <br/> # include <cstring> <br/> const int INF = 20000000; <br/> typedef struct <br/>{< br/> int X, Y; <br/>} node; <br/> node T [51]; <br/> int map [51] [51]; <br/> int shst [51]; <br/> bool used [51]; <br/> int N; <br/> inline int dist (int I, Int J) <br/> {<Br/> int tt; <br/> TT = (T [I]. x-T [J]. x) * (T [I]. x-T [J]. x) + (T [I]. y-T [J]. y) * (T [I]. y-T [J]. y); <br/> return tt; <br/>}< br/> void input () <br/>{< br/> for (INT I = 1; I <= N; I ++) <br/> {<br/> scanf ("% d", & T [I]. x, & T [I]. y); <br/>}< br/> for (INT I = 1; I <= N; I ++) <br/> for (Int J = 1; j <= I; j ++) <br/>{< br/> if (I! = J) <br/>{< br/> map [J] [I] = map [I] [J] = dist (I, j ); <br/>}< br/> else <br/> map [I] [J] = 0; <br/>}</P> <p> double solve (int u) /// U is an isolated island <br/>{< br/> int start; <br/> int I, J, K, KK; <br/> for (I = 1; I <= N; I ++) <br/> used [I] = false; <br/> used [u] = true; <br/> If (u! = 1) Start = 1; <br/> else start = 2; <br/> for (I = 1; I <= N; I ++) <br/> shst [I] = map [start] [I]; <br/> used [start] = true; <br/> shst [start] = shst [u] = 0; <br/> //// there are already two vertices: "accessed ", also find N-2 Points <br/> for (I = 1; I <= n-2; I ++) <br/> {<br/> KK = inf; <br/> for (j = 1; j <= N; j ++) <br/>{< br/> If (! Used [J] & shst [J] <KK) <br/> KK = shst [J], K = J; <br/>}< br/> used [k] = true; <br/> for (j = 1; j <= N; j ++) <br/>{< br/> If (! Used [J] & shst [J]> map [k] [J]) <br/> shst [J] = map [k] [J]; <br/>}</P> <p> double Total = 0; <br/> for (I = 1; I <= N; I ++) <br/>{< br/> total + = SQRT (double) shst [I]); <br/>}< br/> return total; <br/>}< br/> int main () <br/> {<br/> // freopen ("1.in"," r ", stdin ); <br/> int t; <br/> double ans; <br/> CIN> T; <br/> while (t --) <br/>{< br/> scanf ("% d", & N); <br/> input (); <br/> double gyc = 20000000; <br/> for (INT I = 1; I <= N; I ++) <br/> {<br/> ans = solve (I ); <br/> If (gyc> ans) <br/> gyc = ans; <br/>}< br/> printf ("%. 2lf/N ", gyc); <br/>}< br/> return 1; <br/>}< br/>:

 

Prim is used.

 

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.