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

两点之间的距离定义min( |a-b|,|c-d| ),就是切比雪夫距离;我们要求这样的定义下的最小生成树;考虑Kruskal算法:每次选取距离最小的边加入其中且保证不能出现环;那么我们分别按照x,y进行排序;最后再统一排序即可;此时进行普通的Kruskal即可;
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
//#include<cctype>
//#pragma GCC optimize(2)
using namespace std;
#define maxn 2000005
#define inf 0x7fffffff
//#define INF 1e18
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
#define rdult(x) scanf("%lu",&x)
#define rdlf(x) scanf("%lf",&x)
#define rdstr(x) scanf("%s",x)
typedef long long  ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const long long int mod = 1e9 + 7;
#define Mod 1000000000
#define sq(x) (x)*(x)
#define eps 1e-4
typedef pair<int, int> pii;
#define pi acos(-1.0)
//const int N = 1005;
#define REP(i,n) for(int i=0;i<(n);i++)
typedef pair<int, int> pii;
inline ll rd() {ll x = 0;char c = getchar();bool f = false;while (!isdigit(c)) {if (c == '-') f = true;c = getchar();}while (isdigit(c)) {x = (x << 1) + (x << 3) + (c ^ 48);c = getchar();}return f ? -x : x;
}ll gcd(ll a, ll b) {return b == 0 ? a : gcd(b, a%b);
}
int sqr(int x) { return x * x; }/*ll ans;
ll exgcd(ll a, ll b, ll &x, ll &y) {if (!b) {x = 1; y = 0; return a;}ans = exgcd(b, a%b, x, y);ll t = x; x = y; y = t - a / b * y;return ans;
}
*/int n;
int tot;
struct node {int x, y;int d;node(){}node(int x,int y,int d):x(x),y(y),d(d){}}nd[maxn],nd2[maxn];bool cmp(node a, node b) {return a.x < b.x;
}
bool cmp2(node a, node b) {return a.y < b.y;
}bool cmp3(node a, node b) {return a.d < b.d;
}int fa[maxn];
void init() {for (int i = 0; i <= n; i++)fa[i] = i;
}
int findfa(int x) {if (x == fa[x])return x;return fa[x] = findfa(fa[x]);
}void merge(int u, int v) {int p = findfa(u);int q = findfa(v);if (p != q) {fa[p] = q;}
}bool chk(int x, int y) {if (findfa(x) == findfa(y))return true;else return false;
}int kruskal() {int sum = 0;init();for (int i = 0; i < tot; i++) {node tmp = nd2[i];if (tmp.d == 0)merge(tmp.x, tmp.y);if (!chk(tmp.x, tmp.y)) {merge(tmp.x, tmp.y); sum += tmp.d;}}return sum;
}int main() {
//  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);cin >> n;for (int i = 1; i <= n; i++) {rdint(nd[i].x); rdint(nd[i].y);nd[i].d = i;}tot = 0;sort(nd + 1, nd + 1 + n, cmp);for (int i = 2; i <= n; i++) {nd2[tot++] = node(nd[i].d, nd[i - 1].d, nd[i].x - nd[i - 1].x);}sort(nd+1, nd + n+1, cmp2);for (int i = 2; i <= n; i++) {nd2[tot++] = node(nd[i].d, nd[i - 1].d, nd[i].y - nd[i - 1].y);}sort(nd2, nd2 + tot, cmp3);int sum = kruskal();cout << sum << endl;return 0;
}

转载于:https://www.cnblogs.com/zxyqzy/p/10319151.html

atcoder 2643 切比雪夫最小生成树相关推荐

  1. 最小生成树(kruskal、prim、最小生成森林问题、严格次小生成树)

    整理的算法模板合集: ACM模板 目录 一.kruskal算法 二.prim算法 三.Boruvka算法 四.生成森林问题(K颗树) 五.最小生成树的唯一性 六.严格次小生成树 LCA优化的次小生成树 ...

  2. AtCoder Regular Contest 065

    AtCoder Regular Contest 065 C - Daydream Score : 300300300 points 倒着来就行了,正着来会产生歧义匹配,dreamer,dreamdre ...

  3. Atcoder abc 233 题解

    Atcoder abc 233 题解 A 10yen Stamp 答案就是两个数字的差/10之后上取整 记得判断res=0的情况就可以了 c++代码 #include<iostream> ...

  4. 数据结构与算法(7-3)最小生成树(普里姆(Prim)算法和克鲁斯卡尔(Kruskal)算法)

    目录 一.最小生成树简介 二.普里姆算法(Prim) 1.原理 2.存储 2-1.图顶点和权: 2-3. 最小生成树: 3.Prim()函数 3-1.新顶点入树 3-2.保留最小权 3-3. 找到最小 ...

  5. [kuangbin带你飞]专题六 最小生成树 L - 还是畅通工程 (简单最小生成树)

    L - 还是畅通工程 题目链接:https://vjudge.net/contest/66965#problem/L 题目: 某省调查乡村交通状况,得到的统计表中列出了任意两村庄间的距离.省政府&qu ...

  6. 图的算法专题——最小生成树

    概要: Prim算法 Kruskal算法 1.Prim算法 算法流程: (1)对图G(V,E)设置集合S来存放已被并入的顶点,然后执行n次(2)(3) (2)每次从未并入顶点集合中选择与集合S最近的一 ...

  7. 【BZOJ1016】【Luogu P4208】 [JSOI2008]最小生成树计数 最小生成树,矩阵树定理

    蛮不错的一道题,遗憾就遗憾在数据范围会导致暴力轻松跑过. 最小生成树的两个性质: 不同的最小生成树,相同权值使用的边数一定相同. 不同的最小生成树,将其都去掉同一个权值的所有边,其连通性一致. 这样我 ...

  8. Educational Codeforces Round 9 F. Magic Matrix 最小生成树

    F. Magic Matrix 题目连接: http://www.codeforces.com/contest/632/problem/F Description You're given a mat ...

  9. [vijos1234]口袋的天空最小生成树

    题目链接:https://vijos.org/p/1234 白天刚刚写完prim的算法,晚上就心血来潮的打了一道最小生成树的题 虽然有题解说可以用prim做,但是这道题明显是加最小的边,感觉krusk ...

最新文章

  1. 好书 《古代的中医》 《麦肯锡卓越工作方法》
  2. vivado VIO (virtual input output)虚拟IO的使用
  3. mongodb数据库磁盘碎片整理。
  4. Linux下基于socket多线程并发通信的实现
  5. python游戏最简单代码-如何利用Python开发一个简单的猜数字游戏
  6. java自定义日志级别_自定义log4j日志级别
  7. OpenCV4.0+VS2017完整安装配置过程(详细!)
  8. html中样式表的三种形式,CSS样式表有几种存在方式
  9. linux内存管理(十)-页表管理
  10. 聊聊 rel=noopener
  11. 剑指offer(C++)-JZ28:对称的二叉树(数据结构-树)
  12. Bailian2702 密码翻译【密码】
  13. tf.shape()和tf.reshape()
  14. JavaScript 事件-事件流,事件冒泡,事件捕获,事件绑定与解绑,事件委托、阻止冒泡、阻止默认行为详细篇
  15. 解决redisson超时org.redisson.client.RedisResponseTimeoutException: Redis server response timeout
  16. WWW 2017精选论文
  17. 如何在visio中画立体图形
  18. 第四章 闪烁探测器----闪烁体、选择原则、光收集系统、PMT、替代产品
  19. java浪漫代码_程序员表白代码,用过的人都找到了对象...
  20. 快手治理低质量直播内容,运营者需要注意什么?

热门文章

  1. windows下使用net-snmp实现agent扩展(三)
  2. msf:Known bug in WMI query, try migrating to another process
  3. Wireshark个人实战总结
  4. Qt:Windows编程—代码注入
  5. G - Hard problem CodeForces - 706C DP
  6. 转发-[原创]ASR1K 在Rommon导入IOS-XE启动
  7. 关于Office 中的墨迹功能(可作word电子签名)
  8. Party Lamps chapter 2.2
  9. Tomcat的目录结构详解
  10. 6本书,读懂2022年最火的边缘计算