Journey

Time Limit: 15000/3000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others)

Bob has traveled to byteland, he find the N cities in byteland formed a tree structure, a tree structure is very special structure, there is exactly one path connecting each pair of nodes, and a tree with N nodes has N−1 edges.

As a traveler, Bob wants to journey between those N cities, and he know the time each road will cost. he advises the king of byteland building a new road to save time, and then, a new road was built. Now Bob has Q journey plan, give you the start city and destination city, please tell Bob how many time is saved by add a road if he always choose the shortest path. Note that if it's better not journey from the new roads, the answer is 0.

Input

First line of the input is a single integer T(1≤T≤20), indicating there are T test cases.

For each test case, the first will line contain two integers N(2≤N≤105) and Q(1≤Q≤105), indicating the number of cities in byteland and the journey plans. Then N line followed, each line will contain three integer x, y(1≤x,y≤N) and z(1≤z≤1000) indicating there is a road cost z time connect the xth city and the yth city, the first N−1 roads will form a tree structure, indicating the original roads, and the Nth line is the road built after Bob advised the king. Then Q line followed, each line will contain two integer x and y(1≤x,y≤N), indicating there is a journey plan from the xth city to yth city.

Output

For each case, you should first output Case #t: in a single line, where t indicating the case number between 1 and T, then Q lines followed, the ith line contains one integer indicating the time could saved in ith journey plan.

Sample input and output

Sample Input Sample Output
1
5 5
1 2 3
2 3 4
4 1 5
3 5 1
3 1 5
1 2
1 3
2 5
3 4
4 5
Case #1:
0
2
0
2
2

Source

Sichuan State Programming Contest 2012

模板题。

RMQ+LCA在线算法。

#include <cstdio>
#include <cstring>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
const int MAXN=;
typedef pair<int,int> P;
vector<P> G[MAXN];
int depth[*MAXN];
int vs[MAXN*];
int pos[MAXN];
int dep,cnt;
int d[MAXN];
void dfs(int u,int fa)
{
int temp=++dep;
depth[++cnt]=temp;
vs[temp]=u;
pos[u]=cnt;
for(int i=;i<G[u].size();i++)
{
P now=G[u][i];
if(now.first==fa) continue;
d[now.first]=d[u]+now.second;
dfs(now.first,u);
depth[++cnt]=temp;
}
}
int dp[MAXN*][];
void init_rmq(int n)
{
for(int i=;i<=n;i++) dp[i][]=depth[i]; int m=floor(log(n*1.0)/log(2.0));
for(int j=;j<=m;j++)
for(int i=;i<=n-(<<j)+;i++)
dp[i][j]=min(dp[i][j-],dp[i+(<<(j-))][j-]);
}
int rmq(int a,int b)
{
int k=floor(log((b-a+)*1.0)/log(2.0));
return min(dp[a][k],dp[b-(<<k)+][k]);
}
int LCA(int u,int v)
{
if(pos[u]>pos[v]) swap(u,v);
int k=rmq(pos[u],pos[v]);
return vs[k];
}
int dist(int u,int v)
{
return d[u]+d[v]-*d[LCA(u,v)];
}
int main()
{
int T;
scanf("%d",&T);
for(int cas=;cas<=T;cas++)
{
memset(,sizeof(d),);
cnt=;
dep=;
int V,Q;
scanf("%d%d",&V,&Q);
for(int i=;i<=V;i++) G[i].clear();
for(int i=;i<=V-;i++)
{
int u,v,t;
scanf("%d%d%d",&u,&v,&t);
G[u].push_back(P(v,t));
G[v].push_back(P(u,t));
}
int nu,nv,nt;
scanf("%d%d%d",&nu,&nv,&nt);
dfs(,);
init_rmq(cnt);
printf("Case #%d:\n",cas);
while(Q--)
{
int u,v;
scanf("%d%d",&u,&v);
int d1=dist(u,v);
int d2=min(dist(u,nu)+dist(v,nv),dist(u,nv)+dist(v,nu))+nt;
if(d2>=d1) printf("0\n");
else printf("%d\n",d1-d2);
}
}
return ;
}

UESTC(LCA应用:求两点之间的距离)的更多相关文章

  1. 武汉科技大学ACM :1006: 零起点学算法25——求两点之间的距离

    Problem Description 输入平面坐标系中2点的坐标,输出它们之间的距离 Input 输入4个浮点数x1 y1 x2 y2,分别是点(x1,y1) (x2,y2)的坐标(多组数据) Ou ...

  2. HDU2874(LCA应用:求两点之间距离,图不连通)

    Connections between cities Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (J ...

  3. 求两点之间距离 C++

    求两点之间距离(20 分) 定义一个Point类,有两个数据成员:x和y, 分别代表x坐标和y坐标,并有若干成员函数. 定义一个函数Distance(), 用于求两点之间的距离.输入格式: 输入有两行 ...

  4. sql server2008根据经纬度计算两点之间的距离

    --通过经纬度计算两点之间的距离 create FUNCTION [dbo].[fnGetDistanceNew] --LatBegin 开始经度 --LngBegin 开始维度 --29.49029 ...

  5. 2D和3D空间中计算两点之间的距离

    自己在做游戏的忘记了Unity帮我们提供计算两点之间的距离,在百度搜索了下. 原来有一个公式自己就写了一个方法O(∩_∩)O~,到僵尸到达某一个点之后就向另一个奔跑过去 /// <summary ...

  6. java实例三维空间求点之间的距离。。。。

    package com.b; public class Ponit { private double x; private double y; private double z; public Pon ...

  7. js通过经纬度计算两点之间的距离

    最近这几天在做地图的时候,获取到目的地经纬度和当前所在位置的经纬度,通过这几个参数,用js代码就能获取到这两点之间的直线距离: function (lat1, lng1, lat2, lng2) { ...

  8. 利用JS实现的根据经纬度计算地球上两点之间的距离

      最近用到了根据经纬度计算地球表面两点间距离的公式,然后就用JS实现了一下. 计算地球表面两点间的距离大概有两种办法. 第一种是默认地球是一个光滑的球面,然后计算任意两点间的距离,这个距离叫做大圆距 ...

  9. HDU2586(LCA应用:在带权树中求任意两点之间的距离)

    How far away ? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

随机推荐

  1. 从头写一个Cucumber测试(一) Selenium Test

    转载:https://yaowenjie.github.io/%E7%BC%96%E7%A8%8B%E7%9B%B8%E5%85%B3/cucumber-test, 背景(废话不读系列)   前段时间 ...

  2. overflow滚动条样式设置,ie和webkit内核

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. Web框架Django(一)

    Python的WEB框架有Django.Tornado.Flask 等多种,Django相较与其他WEB框架其优势为:大而全,框架本身集成了ORM.模型绑定.模板引擎.缓存.Session等诸多功能. ...

  4. hibernate5(10)注解映射[2]一对多单向关联

    在上一篇文章里.我们从端方向一端建立关联关系,完毕了从文章到作者的关联关系建立.但在实际的博客站点中,用户肯定还须要获取自己所写的文章,这时能够建立用户(一)对文章(多)的单向关联映射. 先来看我们的 ...

  5. some cmds,tools and utils man

    NAME stdbuf - Run COMMAND, with modified buffering operations for its standard streams. SYNOPSIS std ...

  6. python缺省参数

    def test(a,b=22): result=a+b print("resuLt=%d"%result) test(33,33) #缺省参数的意思就是,函数在有参数的情况下,调 ...

  7. sed系列:行或者模式匹配删除特定行

    “p” command prints the buffer (remember to use -n option with “p”) “d” command is just opposite, its ...

  8. shiro框架的使用

    1.配置二级缓存 <ehcache updateCheck="false" name="shiroCache"> <defaultCache ...

  9. vue 表单输入与绑定 v-model

    vue使用 v-model 指令在表单 <input>.<textarea> 及 <select> 元素上创建双向数据绑定.下面我们通过示例先了解下基本用法: &l ...

  10. python发送post请求上传文件,无法解析上传的文件

    前言 近日,在做接口测试时遇到一个奇葩的问题. 使用post请求直接通过接口上传文件,无法识别文件. 遇到的问题 以下是抓包得到的信息: 以上请求是通过Postman直接发送请求的. 在这里可以看到消 ...