Minimum spanning tree
Time limit: 1 Sec memory limit: up to MB
Submit: One solution: 2
Submitted State [Discussion Version]
Title Description
A cosmic Empire has n planets, and since the space of the universe is three-dimensional, the position of each planet can be represented by three-dimensional coordinates (x, y, z). Any two different planets I and J have one side connected, the distance of the edge is calculated as follows: Dis[i,j]=min (| xi-xj|,| yi-yj|,| zi-zj|), where | | The symbol represents the absolute value. Now you're going to pick N-1 edges, and let the n planets connect to a minimal spanning tree, outputting the sum of the lengths of the N-1 bars that make up the smallest spanning tree.
Input
Line 1th, an integer N (1≤n≤100000).
Next there are n lines, three integers per line, x, Y, Z, representing the coordinates of a planet, -1000000000≤x,y,z≤1000000000. There are no two planets with a completely overlapping position.
Output
1 rows, the sum of the lengths of the N-1 bars that make up the smallest spanning tree.
Sample input
511-15-1514 -5-15-1 -1-510-4-119-4 19
Sample output
4
"Analysis" and a minimum spanning tree, or Kruskal algorithm, this thought can be written quickly, but the beginning of the construction of the edge of me stumped, pay attention to n≤100000, if you use a double loop to build edge specified timeout.
So we can sort the x, Y and Z separately, then put the structure into the priority queue, and then Kruskal on the line. The following is the AC code.
#include <iostream>#include<cstdio>#include<cstdlib>#include<cmath>#include<algorithm>#include<climits>#include<cstring>#include<string>#include<Set>#include<map>#include<queue>#include<stack>#include<vector>#include<list>#include<functional>#defineMoD 1000000007#defineINF 0x3f3f3f3f#definePi ACOs (-1.0)using namespaceStd;typedefLong Longll;Const intn=100005;intn,m,k,cnt=-1;intd[4][2]= {0,1,1,0,0,-1,-1,0};intparent[n+ -];BOOLVis[n];structMan {intu,v,w; BOOL operator< (ConstMan&a)Const{ returnw>a.w;//Minimum value First}} Edg[n];//the weights between the nodes of the structure and the nodes, and X, Y, Z are saved in one copy.priority_queue<man>q;//Priority Queuestructmann{intX,y,z,id;} Mp[n];BOOLCMP1 (Mann G,mann h) {returng.x<h.x;}BOOLCMP2 (Mann G,mann h) {returng.y<h.y;}BOOLCmp3 (Mann G,mann h) {returnG.z//sort from small to large for x, Y and Z respectivelyvoidinit () { for(intI=0; i<=n; i++) parent[i]=i;}//InitializeintFind (intx) {if(parent[x]! = x) Parent[x] =Find (parent[x]); returnparent[x];}//finds and returns the root node of the collection to which node x belongsvoidUnion (intXinty) {x=Find (x); Y=Find (y); if(x = = y)return; Parent[y]=x;}//merging elements from two different collectionsvoidBuild ()//Convert the spatial map to a tree, and then save the distance in the x, Y, z direction between two points to the priority queue, respectively .{sort (mp,mp+N,CMP1); for(intI=1; i<n;i++) {man S; S.U=mp[i-1].id;s.v=mp[i].id;s.w=abs (mp[i-1].x-mp[i].x); Q.push (s); }//from small to large, the difference between the two adjacent numbers is minimalSort (mp,mp+N,CMP2); for(intI=1; i<n;i++) {man S; S.U=mp[i-1].id;s.v=mp[i].id;s.w=abs (mp[i-1].y-mp[i].y); Q.push (s); } sort (Mp,mp+N,cmp3); for(intI=1; i<n;i++) {man S; S.U=mp[i-1].id;s.v=mp[i].id;s.w=abs (mp[i-1].z-mp[i].z); Q.push (s); }}voidKruskal () {ll sumweight=0;//total weight of spanning tree intnum=0;//the number of edges already selected intU,v;//Vertexinit (); while(1) {man T=q.top (); Q.pop (); U=t.u; V=T.V; if(Find (u)! =Find (v)) {Sumweight+=T.W; Num++; Union (U,V); } if(num>=n-1) Break;//all the sides have been built.} printf ("%lld\n", sumweight);}intMain () {memset (Vis,false,sizeof(VIS)); intu,v,w; CIN>>N; for(intI=0; i<n; i++) {cin>>mp[i].x>>mp[i].y>>mp[i].z;mp[i].id=i; } Build (); Kruskal (); return 0;}View Code
Minimum spanning Tree (Kruskal) (and set)