1 条题解

  • 1
    @ 2022-10-30 14:31:49
    备注:
    ******************************************/
    #include <queue>
    #include <math.h>
    #include <stack>
    #include <stdio.h>
    #include <iostream>
    #include <vector>
    #include <iomanip>
    #include <string.h>
    #include <algorithm>
    #include <map>
    using namespace std;
    #define int long long
    const int N = 1e6 + 10;
    const int INF = 0x3f3f3f3f;
    struct node{
    	int x, y, v;
    }b[1000100];
    int ans, n, pre[1000100], root1, root2, t;
    struct gjl{
    	int x, y, z, v;
    }jb[2000100];
    bool cmp(node aa, node bb)
    {
    	return aa.v < bb.v;
    }
    bool cmpx(gjl xx, gjl yy)
    {
    	return xx.x < yy.x;
    }
    bool cmpy(gjl xx, gjl yy)
    {
    	return xx.y < yy.y;
    }
    bool cmpz(gjl xx, gjl yy)
    {
    	return xx.z < yy.z;
    }
    int unionsearch(int root)
    {
    	int son, tmp;
    	son = root;
    	while(root != pre[root])
    		root = pre[root];
    	while(son != root)
    	{
    		tmp = pre[son];
    		pre[son] = root;
    		son = tmp;
    	}
    	return root;
    }
    signed main()
    {
    	//freopen(".in", "r", stdin);
    	//freopen(".out", "w", stdout);
    	cin>>n;
    	for(int i = 1; i <= n; i++)
    	{
    		cin>>jb[i].x>>jb[i].y>>jb[i].z;
    		jb[i].v = i;
    	}
    	sort(jb+1, jb+1+n, cmpx);
    	for(int i = 2; i <= n; i++)
    	{
    		int k = jb[i].x - jb[i-1].x;
    		int u = jb[i-1].v;
    		int v = jb[i].v;
    		b[++t].x = v;
    		b[t].y = u;
    		b[t].v = k;
    	}
    	sort(jb+1, jb+1+n, cmpy);
    	for(int i = 2; i <= n; i++)
    	{
    		int k = jb[i].y - jb[i-1].y;
    		int u = jb[i-1].v;
    		int v = jb[i].v;
    		b[++t].x = v;
    		b[t].y = u;
    		b[t].v = k;
    	}
    	sort(jb+1, jb+1+n, cmpz);
    	for(int i = 2; i <= n; i++)
    	{
    		int k = jb[i].z - jb[i-1].z;
    		int u = jb[i-1].v;
    		int v = jb[i].v;
    		b[++t].x = v;
    		b[t].y = u;
    		b[t].v = k;
    	}
    	sort(b+1, b+1+t, cmp);
    	for(int i = 1; i <= n; i++)
    	{
    		pre[i] = i;
    	}
    	for(int i = 1; i <= t; i++)
    	{
    		int root1 = unionsearch(b[i].x);
    		int root2 = unionsearch(b[i].y);
    		if(root1 != root2)
    		{
    			ans += b[i].v;
    			pre[root2] = root1;
    		}
    	}
    	cout<<ans;
    	return 0;
    }
    
    
    • 1

    信息

    ID
    1336
    时间
    2000ms
    内存
    128MiB
    难度
    9
    标签
    递交数
    174
    已通过
    19
    上传者