题目大意:

曼哈顿最小距离生成树

算法讨论:

同上。

这回的模板真的准了。

 #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 (曼哈顿距离最小生成树 模板)的更多相关文章

  1. 【POJ 3241】Object Clustering 曼哈顿距离最小生成树

    http://poj.org/problem?id=3241 曼哈顿距离最小生成树模板题. 核心思想是把坐标系转3次,以及以横坐标为第一关键字,纵坐标为第二关键字排序后,从后往前扫.扫完一个点就把它插 ...

  2. HDU 4408 Minimum Spanning Tree 最小生成树计数

    Minimum Spanning Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  3. 【HDU 4408】Minimum Spanning Tree(最小生成树计数)

    Problem Description XXX is very interested in algorithm. After learning the Prim algorithm and Krusk ...

  4. 数据结构与算法分析–Minimum Spanning Tree(最小生成树)

    给定一个无向图,如果他的某个子图中,任意两个顶点都能互相连通并且是一棵树,那么这棵树就叫做生成树(spanning tree). 如果边上有权值,那么使得边权和最小的生成树叫做最小生成树(MST,Mi ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. [Educational Round 3][Codeforces 609E. Minimum spanning tree for each edge]

    这题本来是想放在educational round 3的题解里的,但觉得很有意思就单独拿出来写了 题目链接:609E - Minimum spanning tree for each edge 题目大 ...

随机推荐

  1. HDU 5904 - LCIS (BestCoder Round #87)

    HDU 5904 - LCIS [ DP ]    BestCoder Round #87 题意: 给定两个序列,求它们的最长公共递增子序列的长度, 并且这个子序列的值是连续的 分析: 状态转移方程式 ...

  2. CODEVS 3279 奶牛的健美操

    3279 奶牛健美操 USACO  时间限制: 2 s  空间限制: 256000 KB  题目等级 : 钻石 Diamond 题目描述 Description Farmer John为了保持奶牛们的 ...

  3. c++ 编译期计算 (一)

    编译期就是编译器进行编译,产生.obj文件的所处的那一段时间(如果是广义的编译期,那么一般还包括了链接期,因为现在很多编译器都会自动调用链接器进行链接)执行期就是你执行某个已经链接好的程序的那段时间. ...

  4. Qt窗口句柄

    关键字: 透明效果,异形,子窗口,控件,浮窗,同级句柄

  5. 转载Eclipse中Maven WEB工程tomcat项目添加调试

    转载地址: http://blog.csdn.net/free4294/article/details/38260581 一.建立一个maven  WEB项目 1.file->new->o ...

  6. javascript 笔试题之删除数组重复元素

    笔试时紧张没写出来,静下心后发现简单的要死. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" & ...

  7. uva1368 DNA Consensus String

    <tex2html_verbatim_mark> Figure 1. DNA (Deoxyribonucleic Acid) is the molecule which contains ...

  8. cf C. Bombs

    http://codeforces.com/contest/350/problem/C 对n个点按曼哈顿距离排序. #include <cstdio> #include <cstri ...

  9. 自定义JsonResult处理JSON序列化DateTime类型数据(Ext4.2+ASP.NET MVC 4)

    最近项目中前台页面使用Extjs4.2 ,在后台ASP.NET MVC4返回的DateTime类型的数据错返回的DateTime类型的JsonResult的结果中的值是“\/Date(13784461 ...

  10. libeXosip2(1-1) -- How-To initialize libeXosip2.

    How-To initialize libeXosip2. The eXtented eXosip stack Initialize eXosip and prepare transport laye ...