UVAlive3662 Another Minimum Spanning Tree 莫队算法
就是莫队的模板题
/*
Memory: 0 KB Time: 1663 MS
Language: C++11 4.8.2 Result: Accepted
*/ #include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cmath>
using namespace std;
typedef long long LL;
const int INF=0x3f3f3f3f;
const int maxn=;
struct Point
{
int x,y,id;
bool operator<(const Point &e)const
{
if(x==e.x)return y<e.y;
return x<e.x;
}
} point[maxn];
struct Edge
{
int u,v,w;
bool operator<(const Edge &e)const
{
return w<e.w;
}
} edge[maxn*];
int tot;
void addedge(int u,int v,int w)
{
++tot;
edge[tot].u=u;
edge[tot].v=v;
edge[tot].w=w;
}
struct Node
{
int minval,pos;
void init()
{
minval=INF;
pos=-;
}
} node[maxn];
int lowbit(int x)
{
return x&(-x);
}
void update(int i,int val,int pos)
{
while(i>)
{
if(val<node[i].minval)
{
node[i].minval=val;
node[i].pos=pos;
}
i-=lowbit(i);
}
}
int query(int i,int m)
{
int minval=INF,pos=-;
while(i<=m)
{
if(node[i].minval<minval)
{
minval=node[i].minval;
pos=node[i].pos;
}
i+=lowbit(i);
}
return pos;
}
int dis(Point a,Point b)
{
return abs(a.x-b.x)+abs(a.y-b.y);
}
int c[maxn],b[maxn],n;
void build()
{
sort(point+,point+n+);
for(int i=; i<=n; ++i)
b[i]=c[i]=point[i].y-point[i].x;
sort(c+,c++n);
int m=unique(c+,c++n)-(c+);
for(int i=; i<=m; ++i)
node[i].init();
for(int i=n; i>; --i)
{
int pos=lower_bound(c+,c++m,b[i])-c;
int tt=query(pos,m);
if(tt!=-)addedge(point[i].id,point[tt].id,dis(point[i],point[tt]));
update(pos,point[i].x+point[i].y,i);
}
}
int fa[maxn];
int find(int x)
{
if(x==fa[x])return x;
return fa[x]=find(fa[x]);
}
LL solve()
{
sort(edge+,edge++tot);
for(int i=; i<=n; ++i)
fa[i]=i;
int cnt=;
LL mst=;
for(int i=; i<=tot; ++i)
{
int fx=find(edge[i].u);
int fy=find(edge[i].v);
if(fx==fy)continue;
fa[fy]=fx;
mst+=edge[i].w;
cnt++;
if(cnt>=n)break;
}
return mst;
}
int main()
{ int cas=;
while(~scanf("%d",&n),n)
{
tot=;
for(int i=; i<=n; ++i)
scanf("%d%d",&point[i].x,&point[i].y),point[i].id=i;
build();
for(int i=; i<=n; ++i)
point[i].y=-point[i].y;
build();
for(int i=; i<=n; ++i)
point[i].y=-point[i].y,swap(point[i].x,point[i].y);
build();
for(int i=; i<=n; ++i)
point[i].y=-point[i].y;
build();
printf("Case %d: Total Weight = %lld\n",++cas,solve());
}
return ;
}
UVAlive3662 Another Minimum Spanning Tree 莫队算法的更多相关文章
- HDU 3333 Turing Tree 莫队算法
题意: 给出一个序列和若干次询问,每次询问一个子序列去重后的所有元素之和. 分析: 先将序列离散化,然后离线处理所有询问. 用莫队算法维护每个数出现的次数,就可以一边移动区间一边维护不同元素之和. # ...
- 最小生成树 (Minimum Spanning Tree,MST) --- Prim算法
本文链接:http://www.cnblogs.com/Ash-ly/p/5409904.html 普瑞姆(Prim)算法: 假设N = (V, {E})是连通网,TE是N上最小生成树边的集合,U是是 ...
- 最小生成树 (Minimum Spanning Tree,MST) --- Kruskal算法
本文链接:http://www.cnblogs.com/Ash-ly/p/5409265.html 引导问题: 假设要在N个城市之间建立通信联络网,则连通N个城市只需要N - 1条线路.这时,自然会考 ...
- 【算法】关于图论中的最小生成树(Minimum Spanning Tree)详解
本节纲要 什么是图(network) 什么是最小生成树 (minimum spanning tree) 最小生成树的算法 什么是图(network)? 这里的图当然不是我们日常说的图片或者地图.通常情 ...
- 最小生成树(Minimum Spanning Tree)——Prim算法与Kruskal算法+并查集
最小生成树——Minimum Spanning Tree,是图论中比较重要的模型,通常用于解决实际生活中的路径代价最小一类的问题.我们首先用通俗的语言解释它的定义: 对于有n个节点的有权无向连通图,寻 ...
- 「日常训练&知识学习」莫队算法(二):树上莫队(Count on a tree II,SPOJ COT2)
题意与分析 题意是这样的,给定一颗节点有权值的树,然后给若干个询问,每次询问让你找出一条链上有多少个不同权值. 写这题之前要参看我的三个blog:Codeforces Round #326 Div. ...
- [Codeforces375D]Tree and Queries(莫队算法)
题意:给定一棵树,每个节点有颜色,对于每个询问(u,k)询问以u为根节点的子树下有多少种颜色出现次数>=k 因为是子树,跟dfs序有关,转化为一段区间,可以用莫队算法求解 直接用一个数组统计出现 ...
- SPOJ COT2 Count on a tree II 树上莫队算法
题意: 给出一棵\(n(n \leq 4 \times 10^4)\)个节点的树,每个节点上有个权值,和\(m(m \leq 10^5)\)个询问. 每次询问路径\(u \to v\)上有多少个权值不 ...
- 说说最小生成树(Minimum Spanning Tree)
minimum spanning tree(MST) 最小生成树是连通无向带权图的一个子图,要求 能够连接图中的所有顶点.无环.路径的权重和为所有路径中最小的. graph-cut 对图的一个切割或者 ...
随机推荐
- oc 中四种实例变量的范围类型@private@protected@public@package
To enforce the ability of an object to hide its data, the compiler limits the scope of instance vari ...
- Mysql异常:MySQLNonTransientConnectionException: No operations allowed after statement closed
Mysql异常:MySQLNonTransientConnectionException: No operations allowed after statement closed MySQLNonT ...
- [转载]test后跟je
今天俺也用OD(OllyDbg)反汇编了个小软件,其中里面有下面两条指令: 没太明白什么意思,google一下,在看雪论坛上发现了一个大虾的解释很详细,记录一下: 1.test a,b 是a与b相与的 ...
- HDU1412
大水题.. 求集合的并 /* */ #include<algorithm> #include<iostream> #include<string.h> #inclu ...
- MyEclipse server窗口 Could not create the view: An unexpected exception was thrown 错误解决
MyEclipse 打开后有时候莫名的在server窗口里抛出“Could not create the view: An unexpected exception was thrown”错误,解决办 ...
- [博弈]ZOJ3591 Nim
题意: 给了一串数,个数不超过$10^5$,这串数是通过题目给的一段代码来生成的 int g = S; ; i<N; i++) { a[i] = g; ) { a[i] = g = W; } = ...
- xcode 把cocos2d-x 以源码的形式包含进自己的项目适合, 性能分析问题的错误
性能分析:出现如下错误: xcode profile Variable has incomplete type class “CC_DLL” 解决办法:在 xcode的Build Setting ...
- [codility]Min-abs-sum
https://codility.com/demo/take-sample-test/delta2011/ 0-1背包问题的应用.我自己一开始没想出来.“首先对数组做处理,负数转换成对应正数,零去掉, ...
- Android:常见错误提示
记录开发中常出现的错误 1.遇到这样的错误时,应该立马想到是书写错误或语法错误,常见为android:name写成了name Attribute is missing the Android name ...
- A过的题目
1.TreeMap和TreeSet类:A - Language of FatMouse ZOJ1109B - For Fans of Statistics URAL 1613 C - Hardwood ...