ZOJ Design the city LCA转RMQ
Design the city
Time Limit: 1 Second Memory Limit: 32768 KB
Cerror is the mayor of city HangZhou. As you may know, the traffic system of this city is so terrible, that there are traffic jams everywhere. Now, Cerror finds out that the main reason of them is the poor design of the roads distribution, and he want to change this situation.
In order to achieve this project, he divide the city up to N regions which can be viewed as separate points. He thinks that the best design is the one that connect all region with shortest road, and he is asking you to check some of his designs.
Now, he gives you an acyclic graph representing his road design, you need to find out the shortest path to connect some group of three regions.
Input
The input contains multiple test cases! In each case, the first line contian a interger N (1 < N < 50000), indicating the number of regions, which are indexed from 0 to N-1. In each of the following N-1 lines, there are three interger Ai, Bi, Li (1 < Li < 100) indicating there's a road with length Li between region Ai and region Bi. Then an interger Q (1 < Q < 70000), the number of group of regions you need to check. Then in each of the following Q lines, there are three interger Xi, Yi, Zi, indicating the indices of the three regions to be checked.
Process to the end of file.
Output
Q lines for each test case. In each line output an interger indicating the minimum length of path to connect the three regions.
Output a blank line between each test cases.
Sample Input
4
0 1 1
0 2 1
0 3 1
2
1 2 3
0 1 2
5
0 1 1
0 2 1
1 3 1
1 4 1
2
0 1 2
1 0 3
Sample Output
3
2 2
2
Author: HE, Zhuobin
Source: ZOJ Monthly, May 2009
题意: 给你一个无向图,然后M个询问,每次询问给你三个点,问你链接这三个点的最短距离
题解:ST算法,分别求出aa=LCA(a,b),bb=LCA(a,c),cc=LCA(c,b);最短距离就是dist[a]+dist[b]+dist[c]-(dist[aa]+dist[bb]+dist[cc]);
//
#include <iostream>
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <string>
#include <stack>
#include <math.h>
#include <vector>
#include <string.h>
using namespace std;
#define maxn 55000
typedef long long ll;
const int inf = (int)1E9+;
inline ll read()
{
ll x=,f=;
char ch=getchar();
while(ch<''||ch>'')
{
if(ch=='-')f=-;
ch=getchar();
}
while(ch>=''&&ch<='')
{
x=x*+ch-'';
ch=getchar();
}
return x*f;
} //*******************************
int first[maxn],ver[maxn*];
int R[maxn*],head[maxn];
int dist[maxn],t,vis[maxn],tot;
struct ss
{
int from,to,value,next;
} e[maxn*];
int dp[maxn*][];
void init()
{
t=;
memset(head,,sizeof(head));
}
void add(int u,int v,int w)
{
ss kk= {u,v,w,head[u]};
e[t]=kk;
head[u]=t++;
}
void dfs(int u,int dep)
{
vis[u]=;
ver[++tot]=u;
first[u]=tot;
R[tot]=dep;
for(int i=head[u]; i; i=e[i].next)
{
if(!vis[e[i].to])
{
int vv=e[i].to;
dist[vv]=dist[u]+e[i].value;
dfs(vv,dep+);
ver[++tot]=u;
R[tot]=dep;
}
}
}
void find_depth()
{
tot=;
memset(vis,,sizeof(vis));
memset(first,,sizeof(first));///首次出现位置
memset(ver,,sizeof(ver));///节点编号
dfs(,);
}
void ST(int n)
{
for(int i=; i<=n; i++)dp[i][]=i;
for(int j=; (<<j)<=n; j++)
{
for(int i=; i+(<<j)-<=n; i++)
{
int a=dp[i][j-];
int b=dp[i+(<<(j-))][j-];
dp[i][j]=R[a]>R[b]?b:a;
}
}
}
int RMQ(int x,int y)
{
int k=;
while((<<(k+))<=y-x+)
{
k++;
}
int a=dp[x][k];
int b=dp[y-(<<k)+][k];
return R[a]>R[b]?b:a;
}
int LCA(int x,int y)
{
x=first[x];
y=first[y];
if(x>y)swap(x,y);
return ver[RMQ(x,y)];
}
int main()
{
int n;
int a,b,c;
int T=;
while(scanf("%d",&n)!=EOF)
{
if(T)cout<<endl;
init();
for(int i=; i<n; i++)
{
scanf("%d%d%d",&a,&b,&c);
add(a+,b+,c);
add(b+,a+,c);
}
dist[]=;
find_depth();
ST(*n-);
int q=read();
for(int i=; i<=q; i++)
{
scanf("%d%d%d",&a,&b,&c);
a++;
b++;
c++;
int aa=LCA(a,b);
int bb=LCA(a,c);
int cc=LCA(b,c);
cout<<dist[a]+dist[b]+dist[c]-(dist[aa]+dist[bb]+dist[cc])<<endl;
}
T++;
}
return ;
}
代码
ZOJ Design the city LCA转RMQ的更多相关文章
- ZOJ 3195 Design the city LCA转RMQ
题意:给定n个点,下面n-1行 u , v ,dis 表示一条无向边和边权值,这里给了一颗无向树 下面m表示m个询问,问 u v n 三点最短距离 典型的LCA转RMQ #include<std ...
- ZOJ 3195 Design the city (LCA 模板题)
Cerror is the mayor of city HangZhou. As you may know, the traffic system of this city is so terribl ...
- zoj 3195 Design the city lca倍增
题目链接 给一棵树, m个询问, 每个询问给出3个点, 求这三个点之间的最短距离. 其实就是两两之间的最短距离加起来除2. 倍增的lca模板 #include <iostream> #in ...
- zoj 3195 Design the city LCA Tarjan
题目链接 : ZOJ Problem Set - 3195 题目大意: 求三点之间的最短距离 思路: 有了两点之间的最短距离求法,不难得出: 对于三个点我们两两之间求最短距离 得到 d1 d2 d3 ...
- [zoj3195]Design the city(LCA)
解题关键:求树上三点间的最短距离. 解题关键:$ans = (dis(a,b) + dis(a,c) + dis(b,c))/2$ //#pragma comment(linker, "/S ...
- zoj——3195 Design the city
Design the city Time Limit: 1 Second Memory Limit: 32768 KB Cerror is the mayor of city HangZho ...
- ZOJ3195 Design the city [2017年6月计划 树上问题04]
Design the city Time Limit: 1 Second Memory Limit: 32768 KB Cerror is the mayor of city HangZho ...
- xtu summer individual 1 C - Design the city
C - Design the city Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu D ...
- LCA和RMQ
下面写提供几个学习LCA和RMQ的博客,都很通熟易懂 http://dongxicheng.org/structure/lca-rmq/ 这个应该是讲得最好的,且博主还有很多其他文章,可以读读,感觉认 ...
随机推荐
- nginx配置文件+本地测试请求转发到远程服务器+集群
1 在本地测试1 众所周知,nginx是一个反向代理的服务器,主要功能即为实现负载均衡和动静分离.在别的我别的文章有详细的nginx(Windows)相关介绍教程. 由于自己安装的nginx在本地的计 ...
- LeetCode(70) Climbing Stairs
题目 You are climbing a stair case. It takes n steps to reach to the top. Each time you can either cli ...
- SQL SERVER占用CPU过高排查和优化
操作系统是Windows2008R2 ,数据库是SQL2014 64位. 近阶段服务器出现过几次死机,管理员反馈机器内存使用率100%导致机器卡死.于是做了个监测服务器的软件实时记录CPU数据,几日观 ...
- C51 中断 个人笔记
总架构图 IE寄存器 控制各个中断源的屏蔽与允许 TCON寄存器 各个中断源的请求标志位&有效信号的规定 中断源及其优先级 中断号写程序的时候要用 CPU处理中断三原则 1.CPU同时接收到几 ...
- pip提示Did not provide a commend
今天小编想要查看一下自己安装的pip版本,并且使用pip查看selenium版本等,结果在cmd输入pip,提示Did not provide a commend,如下所示: 在网上查询了很多方法,比 ...
- 九度oj 题目1023:EXCEL排序
题目1023:EXCEL排序 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:20699 解决:4649 题目描述: Excel可以对一组纪录按任意指定列排序.现请你编写程序实现类似 ...
- Master of Subgraph
Problem E. Master of SubgraphYou are given a tree with n nodes. The weight of the i-th node is wi. G ...
- NYOJ-58最少步数,广搜思想!
最少步数 时间限制:3000 ms | 内存限制:65535 KB 难度:4 -> Link <- 这个题深搜广搜都是可以的,迷宫已经给出了,就看怎么做了:一般起点终点确定用广搜 ...
- mvc 下 使用kindeditor 配置信息
先去下载: http://code.google.com/p/kindeditor/downloads/list引用: LitJSON.dll文件<script src="~/kind ...
- SQL Server 2008如何创建定期自动备份任务
我们知道,利用SQL Server 2008数据库可以实现数据库的定期自动备份.方法是用SQL SERVER 2008自带的维护计划创建一个计划对数据库进行备份,下面我们将SQL SERVER 200 ...