gym-100203h Highways minimum Spanning tree

Source: Internet
Author: User

Test instructions: The plane on the N-Point Road, has repaired the M bar, and then repair a number of points to connect, to find the minimum cost of the scheme.

Idea: basically bare the smallest spanning tree, I here to save the edge directly exist Multyset, take the time is also more convenient, I was so considered, teammates played a simple sort of timed out.

1#include <iostream>2#include <cstdio>3#include <fstream>4#include <algorithm>5#include <cmath>6#include <deque>7#include <vector>8#include <queue>9#include <string>Ten#include <cstring> One#include <map> A#include <stack> -#include <Set> - #defineLL Long Long the #defineEPS 1e-8 - #defineINF 0x3f3f3f3f - #defineMAXN 755 - using namespacestd; + structedge{ -     int  from, to; + LL dis; AEdge (int  from,intTo, LL dis): from( from), to, dis (dis) {}; at     BOOL operator< (ConstEdge &b)Const{ -         returnDis <B.dis; -     } - }; -Multiset<edge>s; - structpoint{ in      intx, y; - }P[MAXN]; to LL DIS[MAXN][MAXN]; + BOOLVIS[MAXN][MAXN]; - intFATHER[MAXN]; the intScan () { *     intres =0, ch, flag =0; $ Panax Notoginseng     if(ch = getchar ()) = ='-') -Flag =1; the  +     Else if(Ch >='0'&& CH <='9') Ares = CH-'0'; the      while(ch = getchar ()) >='0'&& CH <='9' ) +res = res *Ten+ CH-'0'; -  $     returnFlag? -Res:res; $ } - LL Getdis (Point A, point B) { -LL x = ABS (a.x-b.x); theLL y = ABS (A.Y-b.y); -     returnX * x + y *y;Wuyi } the intFindintx) { -     if(Father[x] = = x)returnx; WuFATHER[X] =find (Father[x]); -     returnFather[x]; About } $ intMain () - { - #ifndef Online_judge -Freopen ("In.txt","R", stdin); A     //freopen ("OUT.txt", "w", stdout); + #endif //Open_file the     intn =Scan (); -      for(inti =1; I <= N; i++){ $Father[i] =i; thep[i].x =Scan (); theP[I].Y =Scan (); the     } the s.clear (); - LL dis; in      for(inti =1; I <= N; i++){ the          for(intj = i +1; J <= N; J + +){ thedis =Getdis (P[i], p[j]); About S.insert (Edge (i, J, Dis)); the         } the     } the     intm =Scan (); +     intx, y; -      for(inti =1; I <= m; i++){ thex =Scan ();Bayiy =Scan (); theVis[x][y] =true; thex =find (x); -y =find (y); -         if(x = = y)Continue; theFATHER[X] =y; the     } theMultiset<edge>::iterator it =S.begin (); the      while(It! =S.end ()) { -Edge u = *it; theit++; the         if(Vis[u. from][u.to] | | Vis[u.to][u. from])Continue; thex = Find (U. from);94y =find (u.to); the         if(x = = y)Continue; theFATHER[X] =y; theprintf"%d%d\n", U. from, u.to);98     } About  -}

gym-100203h Highways minimum Spanning tree

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.