题目大意:

曼哈顿最小距离生成树

算法讨论:

同上。

这回的模板真的准了。

 #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. asp.net mvc 删除栏目、栏目下又有子栏目的处理方式

  2. Linux知识扫盲

    1.发现linux中好多软件以d结尾,d代表什么? d 代表 deamon 守护进程守护进程是运行在Linux服务器后台的一种服务程序.现在比较常用 是 service 这个词.它周期性地执行某种任务 ...

  3. HDU 3966 dfs序+LCA+树状数组

    题目意思很明白: 给你一棵有n个节点的树,对树有下列操作: I c1 c2 k 意思是把从c1节点到c2节点路径上的点权值加上k D c1 c2 k 意思是把从c1节点到c2节点路径上的点权值减去k ...

  4. JS对象排序

    function createComparisonFunction(propertyName) {return function(object1, object2){var value1 = obje ...

  5. 从类的继承看socketserver源码

    当我们拿到一份python源代码,我们要怎么去看呢? 下面我们以socketserver为例,看下面的一段代码: #!/usr/bin/env python # -*- coding: UTF-8 - ...

  6. Labeling Balls--poj3687

    Labeling Balls Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12273   Accepted: 3516 D ...

  7. AngularJS中的控制器示例

    <!doctype html> <html ng-app="myApp"> <head> <script src="C:\\Us ...

  8. 百度地图api实例

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm11.aspx ...

  9. goldengate介绍

    Oracle Golden Gate软件是一种基于日志的结构化数据复制备份软件,它通过解析源数据库在线日志或归档日志获得数据的增量变化,再将这些变化应用到目标数据库,从而实现源数据库与目标数据库同步. ...

  10. 《Programming WPF》翻译 第7章 6.视频和3-D

    原文:<Programming WPF>翻译 第7章 6.视频和3-D 虽然详细地讨论视频和3-D超越了这本书的范围,但是获得这些特征的支持是值得的. 视频由MediaElement类型支 ...