题意:给定n个点,下面n-1行 u , v ,dis 表示一条无向边和边权值,这里给了一颗无向树

下面m表示m个询问,问 u v n 三点最短距离

典型的LCA转RMQ

#include<stdio.h>
#include<string.h>
#include<math.h>
#define N 100000
#define INF 1<<29
#define Logo 17
using namespace std; inline int Min(int a,int b){return a>b?b:a;} struct node{
int f,to,dis,nex;
}edge[N];
int edgenum,head[N],dis[N];
int E[N*2],R[N],D[N*2],en;//LCA
int ST[N*2][Logo]; void addedge(int u,int v,int dis){
edge[edgenum].f=u; edge[edgenum].to=v;
edge[edgenum].dis=dis; edge[edgenum].nex=head[u];
head[u]=edgenum++;
}
void makeRmqIndex(int n,int b[]) //返回最小值对应的下标
{
int i,j;
for(i=0;i<n;i++)
ST[i][0]=i;
for(j=1;(1<<j)<=n;j++)
for(i=0;i+(1<<j)-1<n;i++)
ST[i][j]=b[ST[i][j-1]] < b[ST[i+(1<<(j-1))][j-1]]? ST[i][j-1]:ST[i+(1<<(j-1))][j-1];
}
int LCA(int s,int v,int b[]) //这里返回的是最小值的 D中的下标(和E中下标一样)
{
s=R[s],v=R[v];
int k; if(s>v){k=s;s=v;v=k;}
k=(int)(log((v-s+1)*1.0)/log(2.0));
return b[ST[s][k]]<b[ST[v-(1<<k)+1][k]]? E[ST[s][k]]:E[ST[v-(1<<k)+1][k]];
} void DFS(int x,int deep){
E[en]=x;D[en]=deep; R[x]=en++; for(int i=head[x];i!=-1;i=edge[i].nex){
int v=edge[i].to;
if(R[v]==-1)
{
dis[v]=dis[x]+edge[i].dis;
DFS(v,deep+1);
E[en]=x; D[en++]=deep;
}
}
} void Input(int n){
memset(head,-1,sizeof(head));
edgenum=0;
while(--n)
{
int u,v,dis; scanf("%d %d %d",&u,&v,&dis);
addedge(u,v,dis);
addedge(v,u,dis);
}
memset(R,-1,sizeof(R));
en=0;
dis[0]=0;
} int main(){
int n,i,que,first=0;
while(~scanf("%d",&n)){
if(first++)printf("\n");
Input(n);
DFS(0,0);
makeRmqIndex(en,D);
scanf("%d",&que);
while(que--)
{
int a,b,c;
scanf("%d %d %d",&a,&b,&c);
int ans=dis[a]+dis[b]+dis[c]-(dis[LCA(a,c,D)]+dis[LCA(b,c,D)]+dis[LCA(a,b,D)]);
printf("%d\n",ans);
}
}
return 0;
}

ZOJ 3195 Design the city LCA转RMQ的更多相关文章

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

  2. zoj 3195 Design the city LCA Tarjan

    题目链接 : ZOJ Problem Set - 3195 题目大意: 求三点之间的最短距离 思路: 有了两点之间的最短距离求法,不难得出: 对于三个点我们两两之间求最短距离 得到 d1 d2 d3 ...

  3. zoj 3195 Design the city lca倍增

    题目链接 给一棵树, m个询问, 每个询问给出3个点, 求这三个点之间的最短距离. 其实就是两两之间的最短距离加起来除2. 倍增的lca模板 #include <iostream> #in ...

  4. ZOJ Design the city LCA转RMQ

    Design the city Time Limit: 1 Second      Memory Limit: 32768 KB Cerror is the mayor of city HangZho ...

  5. zoj——3195 Design the city

    Design the city Time Limit: 1 Second      Memory Limit: 32768 KB Cerror is the mayor of city HangZho ...

  6. ZOJ 3195 Design the city 题解

    这个题目大意是: 有N个城市,编号为0~N-1,给定N-1条无向带权边,Q个询问,每个询问求三个城市连起来的最小权值. 多组数据 每组数据  1 < N < 50000  1 < Q ...

  7. ZOJ - 3195 Design the city

    题目要对每次询问将一个树形图的三个点连接,输出最短距离. 利用tarjan离线算法,算出每次询问的任意两个点的最短公共祖先,并在dfs过程中求出离根的距离.把每次询问的三个点两两求出最短距离,这样最终 ...

  8. [zoj3195]Design the city(LCA)

    解题关键:求树上三点间的最短距离. 解题关键:$ans = (dis(a,b) + dis(a,c) + dis(b,c))/2$ //#pragma comment(linker, "/S ...

  9. zoj 3195(LCA加强版)

    传送门:Problem 3195 https://www.cnblogs.com/violet-acmer/p/9686774.html 题意: 给一个无根树,有q个询问,每个询问3个点(a,b,c) ...

随机推荐

  1. [反汇编练习] 160个CrackMe之027

    [反汇编练习] 160个CrackMe之027. 本系列文章的目的是从一个没有任何经验的新手的角度(其实就是我自己),一步步尝试将160个CrackMe全部破解,如果可以,通过任何方式写出一个类似于注 ...

  2. Java 碰撞的球 MovingBall (整理)

    package demo; /** * Java 碰撞的球 MovingBall (整理) * 声明: * 这份源代码没有注释,已经忘记了为什么要写他了,基本上应该是因为当时觉得好玩吧. * 有时候想 ...

  3. C的输入输出函数的基本用法

    printf输出函数: printf()函数是格式化输出函数, 一般用于向标准输出设备按规定格式输出信息. printf()函数的调用格式为: printf("<格式化字符串>& ...

  4. ThreadPoolExecutor使用和思考(上)-线程池大小设置与BlockingQueue的三种实现区别

    工作中多处接触到了ThreadPoolExecutor.趁着现在还算空,学习总结一下. 前记: jdk官方文档(javadoc)是学习的最好,最权威的参考. 文章分上中下.上篇中主要介绍ThreadP ...

  5. Linux学习之CentOS(十三)--CentOS6.4下Mysql数据库的安装与配置

    原文:http://www.cnblogs.com/xiaoluo501395377/archive/2013/04/07/3003278.html 如果要在Linux上做j2ee开发,首先得搭建好j ...

  6. swfupload 参数说明

    一.配置参数对象中的常用属性及说明 属性 类型 默认值 描述 upload_url String   处理上传文件的服务器端页面的url地址,可以是绝对地址,也可以是相对地址,当为相对地址时相对的是当 ...

  7. 关于jdbc收集

    一.如果我这样获得一个resultset ResultSet rs=statment.execquery("select * from tab"我如何能够从resultset中获得 ...

  8. ArcGIS 开发的一些知识学习点

    由于文章太多,不便转载,现主要列举如下: ArcGIS Runtime支持的GP工具列表 ArcGIS Runtime支持的GP工具列表 目录(?)[-] Standard版本Standard 空间分 ...

  9. 自定义View实现图片的绘制、旋转、缩放

    1.图片 把一张JPG图片改名为image.jpg,然后拷贝到项目的res-drawable中. 2.activity_main.xml <LinearLayout xmlns:android= ...

  10. static_cast .xml

    pre{ line-height:1; color:#1e1e1e; background-color:#d2d2d2; font-size:16px;}.sysFunc{color:#627cf6; ...