UPC 6569 Built?(最小生成树)

题目描述

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(|a−c|,|b−d|) 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 is given from Standard Input in the following format:

N
x1 y1
x2 y2
:
xN yN

输出

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.

样例输入
3
1 5
3 9
7 8

样例输出
3

提示

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

题意:

给n个点,可以在任意两点之间修建道路,花费为min( |x1-x2| , |y1-y2| ),问最少花费多少可以连通任意两点。

分析:
用kruscal求最小生成,不用求C(n,2)条边的距离,只需求2*(n-1)条边的距离——将n个点按x升序排序,得到n-1条相邻点构成边、将n个点按y升序排序,得到n-1条相邻点构成边;则在这2*(n-1)条边的边集上用kruscal一定能找到其中n-1条边构成的最小生成树

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
const long long int mod=1e9+7;
 int a[N],parent[N];
//int n;
int cnt=0,n;
struct node {

long long int x,y,id;
}road[N];

struct Node{

long long int from,to,cost;

}edge[N];


void UFset( )  //初始化
{
     for(int i=1;i<=n;i++){

         parent[i]=i;
    // printf("777777%d\n",parent[i]);
     }
 }

int cmp1(node a,node b){

return a.x<b.x;
}

int cmp2(node a,node b){

return a.y<b.y;
}

int cmp3(Node a,Node b){

return a.cost<b.cost;
}

void builtedgex(){

for(int i=2;i<=n;i++){
    edge[++cnt].from=road[i-1].id;
    //printf("%lld\n",edge[cnt].from);
    edge[cnt].to=road[i].id;
    edge[cnt].cost=road[i].x-road[i-1].x;

}

}
void builtedgey(){

for(int i=2;i<=n;i++){
    edge[++cnt].from=road[i-1].id;
    edge[cnt].to=road[i].id;
    edge[cnt].cost=road[i].y-road[i-1].y;

}

}

int find(int x)
{
    if(parent[x]==x) { //代表x无上级了,即队长
          //  printf("eeee%d\n",x);
        return x;}
    return parent[x]=find(parent[x]);
}


int main()
{  scanf("%d",&n);
   UFset( );//初始化,用于并查集
   for(int i=1;i<=n;i++){
    scanf("%lld%lld",&road[i].x,&road[i].y);
    road[i].id=i;
   }
   //建边
   sort(road+1,road+n+1,cmp1);
   builtedgex();
   sort(road+1,road+n+1,cmp2);
   builtedgey();
   // Kruskal,最小生成树
   sort(edge+1,edge+1+cnt,cmp3);//对边的大小进行排序
  /* for(int i=1;i<=cnt;i++){
    printf("%lld,",edge[i].cost);
   }*/
    long long len=0;int num=0;
    for(int i=1;i<=cnt;i++){
        // printf("%lld\n",edge[i].from);
        int father1=find(edge[i].from);//看父亲是否一样
        int father2=find(edge[i].to);
   //     printf("hhhh%d,%d,%d\n",i,father1,father2);
        if(father1!=father2){

            parent[father1]=father2;
            num++;
            len+=(edge[i].cost);
            //printf("hahah%d\n",i);
        }
        if(num==n-1) break;
    }
    printf("%lld",len);



    return 0;
}

猜你喜欢

转载自blog.csdn.net/eternityZZing/article/details/81474406
今日推荐