Built(最小生成树+构图离散化)

针对大规模数据集,本文分享了一种高效求解最小生成树问题的方法。通过将点按坐标排序并仅考虑相邻点间的连接,大幅减少了所需比较的距离数量,有效避免了时间和空间复杂度过高的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

个人心得:看了题目很明确,最小生成树,但是但是周赛卡住了,因为10W的点若一个一个找出距离很明显内存和时间都炸了,

静下心来,画了下图,仔细一想,任意一个点都只会在她左右俩边选择建立联系,那么我们只要对做表X,Y分别排序然后再构建

距离,然后Kruaskal就OK了。

希望以后思维能够更活跃点,才能不失初望。

题目:

There are N towns on a plane. The i-th town is located at the coordinates (xi,yi). There may be more than one town at the same coordinates.

You can build a road between two towns at coordinates (a,b) and (c,d) for a cost of min(|ac|,|bd|) yen (the currency of Japan). It is not possible to build other types of roads.

Your objective is to build roads so that it will be possible to travel between every pair of towns by traversing roads. At least how much money is necessary to achieve this?

Constraints

 

  • 2≤N≤105
  • 0≤xi,yi≤109
  • All input values are integers.

Input

 

Input is given from Standard Input in the following format:

N
x1 y1
x2 y2
:
xN yN

Output

 

Print the minimum necessary amount of money in order to build roads so that it will be possible to travel between every pair of towns by traversing roads.

Sample Input 1

 

3
1 5
3 9
7 8

Sample Output 1

 

3

Build a road between Towns 1 and 2, and another between Towns 2 and 3. The total cost is 2+1=3 yen.

Sample Input 2

 

6
8 3
4 9
12 19
18 1
13 5
7 6

Sample Output 2

 

8
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<iomanip>
#include<algorithm>
using namespace std;
#define inf 1<<29
#define nu 1000005
#define maxnum 100005
#define num 30
int n;
struct Md
{
    int v,u,flag;

}M[maxnum],C[maxnum];
struct Node
{
    int x,y,z;

}dis[nu];
bool cmp(Md a,Md b){
   return a.v<b.v;
}
bool cmp0(Md a,Md b){
   return a.u<b.u;
}
bool cmp1(Node a,Node b){
   return a.z<b.z;
}
int book[maxnum];
void init()
{
    for(int i=1;i<=n;i++)
        book[i]=i;

}
int getx(int x)
{
    if(book[x]!=x)
        book[x]=getx(book[x]);
    return book[x];
}
void mergexy(int x,int y)
{
     book[y]=x;
}
void change(){
    for(int i=1;i<=n;i++)
        {
            C[i].v=M[i].v;
            C[i].u=M[i].u;
            C[i].flag=M[i].flag;
        }
}
int main()
{
    scanf("%d",&n);

    for(int i=1;i<=n;i++){
         scanf("%d%d",&M[i].v,&M[i].u);
         M[i].flag=i;
    }
  /*  int t=unique(M+1,M+n)-M;

      n=t;*/
      change();
      int fl=0;
      sort(C+1,C+n+1,cmp);
      for(int i=1;i<n;i++)
      {
          dis[++fl].x=C[i].flag,dis[fl].y=C[i+1].flag,dis[fl].z=(min(fabs(C[i].v-C[i+1].v),fabs(C[i].u-C[i+1].u)));
      }
      change();
      sort(C+1,C+n+1,cmp0);
      for(int i=1;i<n;i++)
      {
          dis[++fl].x=C[i].flag,dis[fl].y=C[i+1].flag,dis[fl].z=(min(fabs(C[i].v-C[i+1].v),fabs(C[i].u-C[i+1].u)));
      }
    sort(dis+1,dis+fl+1,cmp1);
   /*  cout<<flag<<endl;
    for(int i=1;i<=flag;i++)
        cout<<dis[i].z<<endl;*/
        init();
         int number=0;
        int sum=0;
       for(int i=1;i<=fl;i++)
        {
            int p=getx(dis[i].x),q=getx(dis[i].y);
            if(p!=q){
                mergexy(p,q);
                number++;
                sum+=dis[i].z;
        }
        if(number==n)  break;
    }
   cout<<sum<<endl;
    return 0;
}

  



转载于:https://www.cnblogs.com/blvt/p/7978718.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值