atcoder 2643 切比雪夫最小生成树
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?
- 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;
}
atcoder 2643 切比雪夫最小生成树的更多相关文章
- Atcoder CODE FESTIVAL 2016 Final G - Zigzag MST[最小生成树]
题意:$n$个点,$q$次建边,每次建边选定$x,y$,权值$c$,然后接着$(y,x+1,c+1),(x+1,y+1,c+2),(y+1,x+2,c+3),(x+2,y+2,c+4)\dots$(画 ...
- AtCoder Regular Contest 076
在湖蓝跟衡水大佬们打的第二场atcoder,不知不觉一星期都过去了. 任意门 C - Reconciled? 题意:n只猫,m只狗排队,猫与猫之间,狗与狗之间是不同的,同种动物不能相邻排,问有多少种方 ...
- 【AtCoder3611】Tree MST(点分治,最小生成树)
[AtCoder3611]Tree MST(点分治,最小生成树) 题面 AtCoder 洛谷 给定一棵\(n\)个节点的树,现有有一张完全图,两点\(x,y\)之间的边长为\(w[x]+w[y]+di ...
- 【AtCoder2134】ZigZag MST(最小生成树)
[AtCoder2134]ZigZag MST(最小生成树) 题面 洛谷 AtCoder 题解 这题就很鬼畜.. 既然每次连边,连出来的边的权值是递增的,所以拿个线段树xjb维护一下就可以做了.那么意 ...
- 【Atcoder】CODE FESTIVAL 2017 qual A D - Four Coloring
[题意]给定h,w,d,要求构造矩阵h*w满足任意两个曼哈顿距离为d的点都不同色,染四色. [算法]结论+矩阵变换 [题解] 曼哈顿距离是一个立着的正方形,不方便处理.d=|xi-xj|+|yi-yj ...
- AtCoder Regular Contest 093
AtCoder Regular Contest 093 C - Traveling Plan 题意: 给定n个点,求出删去i号点时,按顺序从起点到一号点走到n号点最后回到起点所走的路程是多少. \(n ...
- AtCoder ARC 076D - Built?
传送门:http://arc076.contest.atcoder.jp/tasks/arc076_b 本题是一个图论问题——Manhattan距离最小生成树(MST). 在一个平面网格上有n个格点, ...
- AtCoder,Codeforces做题记录
AGC024(5.20) 总结:猜结论,“可行即最优” B: 给定一个n的排列,每次可以将一个数移到开头或结尾,求变成1,2,...,n所需的最小步数. 找到一个最长的i,i+1,...,j满足在排列 ...
- [AtCoder] NIKKEI Programming Contest 2019 (暂缺F)
[AtCoder] NIKKEI Programming Contest 2019 本来看见这一场的排名的画风比较正常就来补一下题,但是完全没有发现后两题的AC人数远少于我补的上一份AtCoder ...
随机推荐
- X509 文件扩展名
编码 (也用于扩展名) .DER = 扩展名DER用于二进制DER编码的证书.这些证书也可以用CER或者CRT作为扩展名.比较合适的说法是“我有一个DER编码的证书”,而不是“我有一个DER证书”. ...
- Python函数(七)-匿名函数
函数就是变量,定义一个函数就是把一个函数体赋值给一个函数名,函数和变量的回收机制也是一样的 匿名函数不需要指定函数名,只需要有函数体,然后把这个函数体赋给一个变量 Python中使用lambda来创建 ...
- 简单叙述一下MYSQL的优化
一个面试题.每次没能完全答对.各位补充一下.或者发表自己的答案:cry: 现在大概列出如下:(忘各位补充)1.数据库的设计尽量把数据库设计的更小的占磁盘空间.1).尽可能使用更小的整数类型.(medi ...
- 如何解决SSH登录Solaris主机速度慢的问题
SSH登录速度慢可能有多种原因. 1. 与DNS有关 缺省情况下,当客户端用SSH登录solaris服务器时,服务器会试图反向解析客户端的IP 地址(即把IP地址解析成机器名).如果Solaris系统 ...
- 【转】TinyMCE(富文本编辑器)
效果预览:http://www.tinymce.com/tryit/full.php [转]TinyMCE(富文本编辑器)在Asp.Net中的使用方法 转自:http://www.cnblogs.co ...
- 数据存储的两种方式:Cookie 和Web Storage(转)
数据存储的两种方式:Cookie 和Web Storage 数据存储的两种方式:Cookie 和Web Storage 1.Cookie Cookie的作用就像你去超市购物时,第一次给你办张购物卡 ...
- javascript 函数,事件
1.函数字符串函数 var s=new string(); var ss="hello world"; var sss=""HELLO, WORLD" ...
- html 5 data-* (dataset) 属性和 jquery data方法比较
一些文章在介绍html 5 data-* (dataset)属性时,会提到jquery的data方法,认为data方法能够很好的利用html 5的这个特性.但实际上,二者的兼容性是很差的.下面给出一段 ...
- 树莓派研究笔记(1)-- 安装Mono
职业病啊,原谅我,第一步就是要安装Mono搞DOTNET 1. 更新系统 sudo apt-get update 2. 安装 Mono sudo apt-get install mono-comple ...
- Spring第六篇---AOP
接着Spring第五篇讲 我们今天将叙述以下几个知识点 1 什么是AOP AOP 是一种思想 横向重复 纵向抽取 在软件业,AOP为Aspect Oriented Programming的缩写,意 ...