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 ...
随机推荐
- 【Nginx】事件驱动框架和异步处理
Nginx对请求的处理是通过事件触发的,模块作为事件消费者,仅仅能被事件收集.分发器调用.这与传统的Webserver是不同的. 传统的Webserver下,一个请求由一个进程消费.请求在建立连接后将 ...
- Java太阳系小游戏分析和源代码
Java太阳系小游戏分析和源代码 -20150809 近期看了面向对象的一些知识.然后跟着老师的解说做了一个太阳系各行星绕太阳转的小游戏,来练习巩固一下近期学的知识: 用到知识点:类的继承.方法的重载 ...
- Android 音频的播放之二MediaPlayer
MediaPlayer类可用于控制音频/视频文件或流的播放.关于怎样使用这个类的方法还能够阅读VideoView类的文档. 1.状态图 对播放音频/视频文件和流的控制是通过一个状态机来管理的. 下图显 ...
- 基于RFS(robot framework selenium)框架模拟POST/GET请求执行自动化接口测试
打开RIDE添加测试用例 如: Settings Library Collections Library RequestsLibrary Test Cases ...
- Java面向对象基础三
1.函数的重载 2.构造函数的作用 (构造函数能够重载) 1.函数名必须和类名同样 2.没有返回值 3.使用 New 来调用构造函数 4.假设类中没有构造函数,编译器会自己主动帮忙载入一个參数为空.方 ...
- AndroidManifest具体解释之Application(有图更好懂)
可以包括的标签: <activity> <activity-alias> <service> <receiver> <provider> & ...
- 目标检测之显著区域检测---国外的一个图像显著区域检测代码及其效果图 saliency region detection
先看几张效果图吧 效果图: 可以直接测试的代码: 头文件: // Saliency.h: interface for the Saliency class.////////////////////// ...
- poj 1730Perfect Pth Powers(分解质因数)
id=1730">Perfect Pth Powers Time Li ...
- EasyDarwin开源流媒体音视频云平台遇到的奇葩问题:内网运行正常,公网流媒体不通
最近在帮助EasyDarwin的用户部署EasyNVR+EasyDarwin云平台+EasyClient方案的过程中,遇到一个问题,EasyNVR分布在用户各地区现场的内网中,EasyDarwin云平 ...
- Axure实现Tab选项卡切换功能
这几天用Axure画原型图的过程中,须要实现Tab选项卡切换的效果,但Axure中并没有类似于Tab控件的部件,所以能够用Axure中的动态面板(Dynamic Panel)来实现. 本文以已经汉化的 ...