LA 3662 Another Minimum Spanning Tree (曼哈顿距离最小生成树 模板)
题目大意:
曼哈顿最小距离生成树
算法讨论:
同上。
这回的模板真的准了。
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <cstdio>
using namespace std;
const int N = + ;
const int M = N * ;
typedef long long ll;
const ll oo = 100000000000000LL; int n, etot;
ll W = , c[N];
int fa[N], id[N];
int A[N], B[N]; struct Point{
int x, y, id;
bool operator < (const Point &a)const {
if(a.x == x) return y < a.y;
return x < a.x;
}
}p[N]; struct Edge{
int from, to;
ll dis;
bool operator < (const Edge &a)const {
return dis < a.dis;
}
}e[M]; int find(int x){return fa[x] == x ? x : fa[x] = find(fa[x]);}
void update(int x, ll val, int pos){
for(int i = x; i > ; i -= (i&(-i))){
if(val < c[i]){
c[i] = val;
id[i] = pos;
}
}
}
int query(int pos, int up){
int res = -;
ll val = oo;
for(int i = pos; i <= up; i += (i&(-i))){
if(c[i] < val){
val = c[i];
res = id[i];
}
}
return res;
}
void MST(){
int up;
for(int dir = ; dir <= ; ++ dir){
if(dir % == )
for(int i = ; i <= n; ++ i)
swap(p[i].x, p[i].y);
else if(dir == )
for(int i = ; i <= n; ++ i)
p[i].x = -p[i].x;
sort(p + , p + n + );
for(int i = ; i <= n; ++ i) A[i] = B[i] = (int) p[i].y - p[i].x;
sort(B + , B + n + );
up = unique(B + , B + n + ) - B - ; for(int i = ; i <= up; ++ i){
c[i] = oo; id[i] = -;
} for(int i = n; i >= ; -- i){
A[i] = lower_bound(B + , B + up + , A[i]) - B;
int np = query(A[i], up);
if(np != -){
++ etot;
e[etot].from = p[i].id;
e[etot].to = p[np].id;
e[etot].dis = abs(p[i].x - p[np].x) + abs(p[i].y - p[np].y);
}
update(A[i], p[i].x + p[i].y, i);
}
} int have = ;
sort(e + , e + etot + );
for(int i = ; i <= n; ++ i) fa[i] = i;
for(int i = ; i <= etot; ++ i){
int fx = find(e[i].from), fy = find(e[i].to);
if(fx != fy){
fa[fx] = fy;
++ have;
W += e[i].dis;
if(have == n-) break;
}
}
} int main(){
int cnt = ;
while(scanf("%d", &n) && n){
++ cnt; etot = ;
for(int i = ; i <= n; ++ i){
scanf("%d%d", &p[i].x, &p[i].y);
p[i].id = i;
}
W = ;
MST();
printf("Case %d: Total Weight = %lld\n", cnt, W);
}
return ;
}
LA 3662
LA 3662 Another Minimum Spanning Tree (曼哈顿距离最小生成树 模板)的更多相关文章
- 【POJ 3241】Object Clustering 曼哈顿距离最小生成树
http://poj.org/problem?id=3241 曼哈顿距离最小生成树模板题. 核心思想是把坐标系转3次,以及以横坐标为第一关键字,纵坐标为第二关键字排序后,从后往前扫.扫完一个点就把它插 ...
- HDU 4408 Minimum Spanning Tree 最小生成树计数
Minimum Spanning Tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- 【HDU 4408】Minimum Spanning Tree(最小生成树计数)
Problem Description XXX is very interested in algorithm. After learning the Prim algorithm and Krusk ...
- 数据结构与算法分析–Minimum Spanning Tree(最小生成树)
给定一个无向图,如果他的某个子图中,任意两个顶点都能互相连通并且是一棵树,那么这棵树就叫做生成树(spanning tree). 如果边上有权值,那么使得边权和最小的生成树叫做最小生成树(MST,Mi ...
- Educational Codeforces Round 3 E. Minimum spanning tree for each edge LCA/(树链剖分+数据结构) + MST
E. Minimum spanning tree for each edge Connected undirected weighted graph without self-loops and ...
- CF# Educational Codeforces Round 3 E. Minimum spanning tree for each edge
E. Minimum spanning tree for each edge time limit per test 2 seconds memory limit per test 256 megab ...
- Codeforces Educational Codeforces Round 3 E. Minimum spanning tree for each edge LCA链上最大值
E. Minimum spanning tree for each edge 题目连接: http://www.codeforces.com/contest/609/problem/E Descrip ...
- MST(Kruskal’s Minimum Spanning Tree Algorithm)
You may refer to the main idea of MST in graph theory. http://en.wikipedia.org/wiki/Minimum_spanning ...
- [Educational Round 3][Codeforces 609E. Minimum spanning tree for each edge]
这题本来是想放在educational round 3的题解里的,但觉得很有意思就单独拿出来写了 题目链接:609E - Minimum spanning tree for each edge 题目大 ...
随机推荐
- mongodb内嵌文档的查询
本文转自:http://blog.163.com/wm_at163/blog/static/1321734902012526103825481/ 1 > db.blog.findOne() { ...
- 慕课linux学习笔记(四)常用命令(1)
Root 表示当前登录用户 Localhost 主机名 ~ 当前所在位置(~表示/root) # 超级用户 $ 普通用户 命令 1.pwd 显示当前所在位置 2.ls 查询目录中的内容 -a 显示所有 ...
- 列表:一个打了激素的数组3 - 零基础入门学习Python012
列表:一个打了激素的数组3 让编程改变世界 Change the world by program 列表的一些常用操作符 比较操作符 逻辑操作符 连接操作符 重复操作符 成员关系操作符 ...... ...
- NSThread 、NSRunLoop 和 Dispatch Queue
iOS多线程编程中,NSOperation和NSOperationQueue无疑是最常用的,它们能满足绝大部分情况下的线程操作.但在完成一些特殊的任务时,我们还是要使用的NSThread和NSRun ...
- cf C. Magic Formulas
http://codeforces.com/contest/424/problem/C #include <cstdio> #include <cstring> #includ ...
- shell 脚本FTP自动上传文件
下面的脚本 会把本地的文本文件压缩后, 上传到FTP服务器上. 里面有一点小逻辑, 就是上传的文本文件 是 日期时间.txt 形式的, 一天写一个日志文件, 今天的文件不上传, 只上传 老的日志文件. ...
- [Android]通过setImageURI设置网络上面的图片
设置imageView显示网络上的图片 picUrl = new URL(getIntent().getExtras().getString("map_url")); Bitmap ...
- android批量文件上传(android批量图片上传)
项目中多处用到文件批量上传功能,今天正好解决了此问题,在此写出来,以便日后借鉴. 首先,以下架构下的批量文件上传可能会失败或者不会成功: 1.android客户端+springMVC服务端:服务端 ...
- mybatis和hibernate对比
Hibernate是一个数据库表和java对象之间完全映射的框架,java开发人员直接对java对象操作,而不对数据库表进行操作: Mybatis是对SQL语句和java对象进行映射,仍需要开发人员编 ...
- Java宝典(二)
--String s = "a" + "b" + "c" + "d"; 一共创建了多少个对象? --对于如下代码: St ...