HDU 3371 (city unicom minimum spanning tree-kruskal)

Source: Internet
Author: User

Test instructions is the minimum amount to be spent on all points, such as not fully connected, output-1

Direct Kruskal, the point of the lesson is that rank is the algorithm header file, directly do the variable name will lead to compilation errors. Did not find the specific use of rank ...

1#include <cstdio>2#include <iostream>3#include <algorithm>/*rank is in algorithm.*/4 using namespacestd;5 intfather[100005],r[100005];6 intN,m,k,ans;7 structEDG8 {9     int  from, To,money;Ten}e[100005]; One intHdfintx) A { -     if(X==father[x])returnx; -     returnFD (father[x]); the } - voidMakeSet (intx) - { -      for(inti =1; I <= x;i++) +     { -Father[i] =i; +R[i] =1; A     } at } - voidUoN (intXinty) - { -x =FD (x); -y =fd (y); -     if(x!=y) in     { -         if(R[x] >=R[y]) to         { +Father[y] =x; -R[X] + =R[y]; theR[y] =0; *         } $         ElsePanax Notoginseng         { -FATHER[X] =y; theR[y] + =R[x]; +R[X] =0; A         } the     } + } - BOOLcmp (edg x,edg y) $ { $     returnX.money <Y.money; - } - intMain () the { -     intT,p,len;Wuyiscanf"%d",&t); the      while(t--) -     { Wuscanf"%d%d%d",&n,&m,&k); - MakeSet (n); AboutAns =0; $Len =0; -          for(inti =1; I <= m; i++) -scanf"%d%d%d", &e[i]. from,&e[i].to,&E[i].money); -          for(inti =1; I <= K; i++) A         { +scanf"%d",&p); the             intA, B (0); -              for(intU =0; U < p; u++) $             { thescanf"%d",&a); the                 if(u&&b) UoN (a); theb =A; the             } -         } inSort (e+1, e+1+m,cmp); the          for(inti =0; I < m; i++) the             if(FD (E[i]. from)!=FD (e[i].to)) About             { theUoN (E[i]. from, e[i].to); theAns + =E[i].money; the             } +          for(inti =1; I <= N; i++) -             if(R[i] >0) len++;//Check if there is only one root, that is, if there are no points attached to the tree the         if(len = =1) printf ("%d\n", ans);Bayi         Elseprintf"-1\n"); the     } the     return 0; -}
View Code

HDU 3371 (city unicom minimum spanning tree-kruskal)

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.