HDU 1162 Eddy ' s picture (minimum spanning tree) (Java edition)

Source: Internet
Author: User
Tags gety pow

Eddy ' s picture

Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=1162

--Daily Online, welcome message to talk about.

Main topic:

Give you n points, the shortest total distance to connect the n points together.

Ideas:

Assume that there is a path between every 22 points to find the smallest spanning tree.

AC Code: (Java)
1 ImportJava.util.Scanner;2 Importjava.math.*;3  Public classMain {4      Public Static Final intMAXN = 110;5      Public Static DoubleMp[][] =New Double[MAXN][MAXN];6      Public StaticPoint p[] =NewPOINT[MAXN];7      Public Static intN;8      Public StaticString answer =NewString ();9      Public StaticScanner SCN =NewScanner (system.in);Ten      Public Static voidMain (string[] args) { One          for(inti = 0; i < MAXN; i++){ AP[i] =NewPoint (); -         } -          while(Scn.hasnext ()) { then =scn.nextint (); - Foundmap (); -Answer = String.Format ("%.2f", Prim ()); - System.out.println (answer); +         } -System.exit (0); +     } A      Public Static BooleanFoundmap () { at         Doublex, y; -          for(inti = 0; I < n; i++) { -x =scn.nextdouble (); -y =scn.nextdouble (); - p[i].setpoint (x, y); -}//Found points in          for(inti = 0; I < n; i++) -              for(intj = 0; J < N; J + +) to                 if(I! =j) +MP[I][J] =Point.getdistencs (P[i], p[j]); -         //Found Map the         return true; *     } $      Public Static DoublePrim () {Panax Notoginseng         inttempi; -         DoubleTempclos; the         Double[] Closedge =New Double[MAXN]; +         Doublesum = 0.0; A         //Init Closeedge theClosedge[0] = 0.0; +          for(inti = 1; I < n; i++) { -Closedge[i] = mp[0][i]; $         } $         //Find n-1 Edge -          for(inti = 1; I < n; i++) { -Tempi =-1; theTempclos =-1.0; -              for(intj = 0; J < N; J + +) {Wuyi                 if(Closedge[j]! = 0.0 && (Tempclos = = -1.0 | | closedge[j] <Tempclos)) { theTempclos =Closedge[j]; -Tempi =J; Wu                 } -             } AboutSum + =Tempclos; $Closedge[tempi] = 0; -              for(intj = 0; J < N; J + +) { -                 if(J! = Tempi && Closedge[j] >Mp[tempi][j]) { -CLOSEDGE[J] =Mp[tempi][j]; A                 } +             } the         } -         returnsum; $     } the } the classPoint { //has been down as a dot class can not look at.  the     Private Doublex; the     Private Doubley; -      PublicPoint (DoubleXDoubley) { in          This. x =x; the          This. y =y; the     } About      PublicPoint () { thex = 0.0; they = 0.0; the     } +      Public voidSetPoint (DoubleXDoubleY) { -          This. x =X; the          This. y =Y;Bayi     } the      Public DoubleGetX () { the         returnx; -     } -      Public DoubleGetY () { the         returny; the     } the      Public Static DoubleGetdistencs (Point p1,point p2) { the         returnMath.sqrt (Math.pow (P1.getx ()-P2.getx (), 2.0) + Math.pow (p1.gety ()-p2.gety (), 2.0)); -     } the}

2017-07-23 13:07:39

HDU 1162 Eddy ' s picture (minimum spanning tree) (Java edition)

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.