Zoj 1203 swordfish

Source: Internet
Author: User

Note segmentation fault for prim implementation

1 # include <cstdio> 2 # include <cmath> 3 4 double dis [105] [105]; 5 Double P [105] [2]; // enter the coordinate point 6 double d [105]; // The distance from one point to another is 7 double vis [105]; // remember whether the point has been accessed 8 double Len; 9 int N; 10 11 // initialize dis [105] [105] 12 Void Init () 13 {14 int I, j; 15 for (I = 1; I <= N; I ++) 16 {17 for (j = 1; j <= N; j ++) 18 {19 dis [I] [J] = dis [J] [I] = 100000000; 20} 21 vis [I] = 0; 22} 23 Len = 0.0; 24} 25 26 // calculate dis [105] [105] 27 void makedis () 28 {29 int I, J; 30 31 for (I = 1; I <= N; I ++) 32 for (j = I; j <= N; j ++) 33 {34 dis [I] [J] = dis [J] [I] = SQRT (P [I] [0]-P [J] [0]) * (p [I] [0]-P [J] [0]) + (P [I] [1]-P [J] [1]) * (p [I] [1]-P [J] [1]); 35 36} 37} 38 39 40 // use the prim algorithm to obtain the shortest distance 41 void prim () 42 {43 int I, j, k; 44 double min; 45 for (I = 1; I <= N; I ++) 46 d [I] = dis [1] [I]; 47 vis [1] = 1; 48 49 for (I = 1; I <n; I ++) 50 {51 min = 0 xfffffff; // maximum value 52 for (j = 1; j <= N; j ++) 53 {5 4 If (d [J] <min &&! Vis [J]) 55 {56 min = d [J]; 57 K = J; 58} 59} 60 vis [k] = 1; 61 Len + = d [k]; 62 for (j = 1; j <= N; j ++) 63 {64 if (! Vis [J] & D [J]> dis [k] [J]) 65 d [J] = dis [k] [J]; 66} 67 68} 69} 70 71 // main function 72 int main () 73 {74 int step; 75 int I; 76 step = 0; 77 while (scanf ("% d", & N) // end with 0 78 {79 step ++; // record case80 81 If (step! = 1) 82 printf ("\ n"); 83 Init (); 84 for (I = 1; I <= N; I ++) 85 {86 scanf ("% lf", & P [I] [0], & P [I] [1]); 87} 88 makedis (); 89 prim (); 90 printf ("case # % d: \ n", step); 91 printf ("the minimal distance is: %. 2lf \ n ", Len); 92} 93 return 0; 94}
Related Article

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.