UESTC(LCA应用:求两点之间的距离)
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 |
Case #1: |
Source
模板题。
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应用:求两点之间的距离)的更多相关文章
- 武汉科技大学ACM :1006: 零起点学算法25——求两点之间的距离
Problem Description 输入平面坐标系中2点的坐标,输出它们之间的距离 Input 输入4个浮点数x1 y1 x2 y2,分别是点(x1,y1) (x2,y2)的坐标(多组数据) Ou ...
- HDU2874(LCA应用:求两点之间距离,图不连通)
Connections between cities Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (J ...
- 求两点之间距离 C++
求两点之间距离(20 分) 定义一个Point类,有两个数据成员:x和y, 分别代表x坐标和y坐标,并有若干成员函数. 定义一个函数Distance(), 用于求两点之间的距离.输入格式: 输入有两行 ...
- sql server2008根据经纬度计算两点之间的距离
--通过经纬度计算两点之间的距离 create FUNCTION [dbo].[fnGetDistanceNew] --LatBegin 开始经度 --LngBegin 开始维度 --29.49029 ...
- 2D和3D空间中计算两点之间的距离
自己在做游戏的忘记了Unity帮我们提供计算两点之间的距离,在百度搜索了下. 原来有一个公式自己就写了一个方法O(∩_∩)O~,到僵尸到达某一个点之后就向另一个奔跑过去 /// <summary ...
- java实例三维空间求点之间的距离。。。。
package com.b; public class Ponit { private double x; private double y; private double z; public Pon ...
- js通过经纬度计算两点之间的距离
最近这几天在做地图的时候,获取到目的地经纬度和当前所在位置的经纬度,通过这几个参数,用js代码就能获取到这两点之间的直线距离: function (lat1, lng1, lat2, lng2) { ...
- 利用JS实现的根据经纬度计算地球上两点之间的距离
最近用到了根据经纬度计算地球表面两点间距离的公式,然后就用JS实现了一下. 计算地球表面两点间的距离大概有两种办法. 第一种是默认地球是一个光滑的球面,然后计算任意两点间的距离,这个距离叫做大圆距 ...
- HDU2586(LCA应用:在带权树中求任意两点之间的距离)
How far away ? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
随机推荐
- Golang Map Addressability
http://wangzhezhe.github.io/blog/2016/01/22/golangmapaddressability-dot-md/ 在golang中关于map可达性的问题(addr ...
- Hadoop 源码阅读技巧
http://www.cnblogs.com/xuxm2007/category/388607.html 个人谈谈阅读hadoop源代码的经验.首先,不得不说,hadoop发展到现在这个阶段, ...
- Android通过JNI实现与C语言的串口通讯操作蓝牙硬件模块
一直想写一份技术文档,但因为自感能力有限而无从下笔,近期做了个关于Android平台下实现与C语言的通讯来操作蓝牙模块的项目,中间碰到了很多问题,也在网上查了很多资料,在完毕主要功能后.也有一些人在网 ...
- Android gdb so
gdb debug an android application 1.gdb 要有gdbserver 一般模拟器默认装有gdbserver,如2.3.3的模拟器,看一下有没有: D:\Develope ...
- 详谈kubernetes滚动更新-1
系列目录 这个系列分为两个小节,第一个小节介绍deployment滚动更新时,deployment.replicaset.pod的细节以及创建过程以及deployment版本管理的方式 第二个小节将介 ...
- 压力测试工具ab,wrk,locust简介
ab 无疑是目前最常见的压力测试工具.其典型用法如下: shell> ab -k -c 100 -t 10 http://domain/path 其中,参数「c」表示的是并发, 参数「t」表示的 ...
- linux shell 的前世今生和流行BASH SHELL的特点
前言 shell作为用户和操作系统内核交互的接口,也不断的在发展迭代.shell的发展也离不开unix/linux 系统的发展.并且在开源社区对shell的发展也起到了推动作用. 内容思维导图简介 发 ...
- 图像滤镜艺术---PS图层混合模式之明度模式
本文将介绍PS图层混合模式中比較复杂 的"明度"模式的算法原理及代码实现内容. 说到PS的图层混合模式,计算公式都有,详细代码实现也能找到,可是,都没有完整介绍全部图层混合模式的代 ...
- linux socket send和recv、write和read
1 recv和read ssize_t recv(int sockfd, void *buf, size_t len, int flags); ssize_t read(int fd, void *b ...
- java会不会出现内存泄露
1 什么是java内存泄露 当java中的对象生命周期已经结束,本应该释放,但是却长时间不能被释放时,也就是说,内存被浪费了,就是内存泄露. 2 java内存泄露的根本原因 长生命周期的对象中持有短生 ...