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)