题目大意:

曼哈顿最小距离生成树

算法讨论:

同上。

这回的模板真的准了。

 #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. php解析json数组(循环输出数据)的实例

    以快递100接口为例 返回的JSON数据 {"message":"ok","nu":"350116805826",&qu ...

  2. 【转】linux之自建yum仓库

    原链接:http://www.live-in.org/archives/1410.html 平时使用yum方式安装更新软件,可以自建一个yum源,同步官方更新源,这样如果本地有机器要升级的话就可以直接 ...

  3. Python学习(一) Python安装配置

    我本身是Java程序猿,听说Python很强大,所以准备学习一下Python,虽说语言都是相同的,但java跟python肯定还是有区别的.希望在此记录一下自己的学习过程. 目前,Python分2.X ...

  4. 为什么说 Git 比 SVN 更好

    为什么说 Git 比 SVN 更好 在版本控制系统的选型上,是选择Git还是SVN? 对于开源项目来说这不算问题.使用Git极大地提高了开发效率.扩大了开源项目的参与度. 增强了版本控制系统的安全性, ...

  5. CRC校验源码分析

    这两天做项目,需要用到 CRC 校验.以前没搞过这东东,以为挺简单的.结果看看别人提供的汇编源程序,居然看不懂.花了两天时间研究了一下 CRC 校验,希望我写的这点东西能够帮助和我有同样困惑的朋友节省 ...

  6. PagedList.MVC 应用

    1. NuGet 下载 PagedList.MVC 2. View Page @model PagedList.IPagedList<Libaray.Models.Entities.BookMo ...

  7. windows下wchar_t* 转char*

    这个在windows下很常见,常用,留个档. 一般用这个函数: size_t wcstombs( char *mbstr, const wchar_t *wcstr, size_t count ); ...

  8. 通过Excel来集中管理资源文件

     在支持双语或多语种项目中,常常需要编辑多个文件来添加资源项,感觉比较繁琐,所以想做一个可以集中管理资源文件的工具.借助Excel,使用Excel来记录,并且通过Excel可以进行分页分模块来规划 ...

  9. 修改和获取web.config或app.config文件appSettings配置节中的Add里的value属性 函数

    1: /// <summary> 2: /// 修改web.config或app.config文件appSettings配置节中的Add里的value属性 3: /// </summ ...

  10. Android_Studio常用插件

    Android studio常用插件,可极大简化开发,增强开发效率. 不懂安装studio插件,看参考博文:android stuido插件安装:http://blog.csdn.net/liang5 ...