Journey

Time Limit: 1 Sec

Memory Limit: 256 MB

题目连接

http://acm.uestc.edu.cn/#/problem/show/92

Description

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

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
 

Sample Output

Case #1:
0
2
0
2
2

HINT

题意

给你一棵树,然后加了一条边,然后给Q次询问,问你这些点之间的最短距离缩短了多少

题解:

加了边之后,你走的方式就变成三种了,要么和原来一样,要么就是u-A-B-v,要么就是u-B-A-v这种

tarjan预处理一下距离跑一发就好了

代码:

//qscqesze
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <bitset>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 200006
#define mod 1000000007
#define eps 1e-9
#define e exp(1.0)
#define PI acos(-1)
const double EP = 1E- ;
int Num;
//const int inf=0x7fffffff;
const ll inf=;
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;
}
//***********************************************************
struct ndoe{
int v,w,next;
}ed[maxn*];
int dp[][maxn*],pos[maxn],dis[maxn],res[maxn],head[maxn],parent[maxn],vis[maxn];
int n,m,c,num,cnt,size;
void addedge(int u,int v,int w)
{
ed[num].v=v;
ed[num].w=w;
ed[num].next=head[u];
head[u]=num++;
}
int Find(int i)
{
if(i!=parent[i])
parent[i]=Find(parent[i]);
return parent[i];
}
void Union(int i,int j)
{
int x,y;
x=Find(i);
y=Find(j);
if(x!=y)
parent[x]=y;
}
int A,B,C,q;
void init()
{
int i,j,k;
n=read();q=read();
m=n-;
memset(head,-,sizeof(head));
memset(vis,,sizeof(vis));
for(i=;i<=n;i++)
parent[i]=i;
cnt=size=num=;
while(m--)
{
scanf("%d%d%d",&i,&j,&k);
addedge(i,j,k);
addedge(j,i,k);
Union(i,j);
}
A=read(),B=read(),C=read();
}
void dfs(int u,int dist)
{
int i,j;
vis[u]=;
dis[u]=dist;
pos[u]=cnt;
res[size]=u;
dp[][cnt++]=size++;
for(i=head[u];i!=-;i=ed[i].next)
{
j=ed[i].v;
if(!vis[j])
{
dfs(j,dist+ed[i].w);
dp[][cnt++]=dp[][pos[u]];
}
}
}
void rmq()
{
int i,j,k;
for(i=;(<<i)<=n;i++)
for(j=n-;j>=;j--)
{
k=(<<(i-));
dp[i][j]=dp[i-][j];
if(k+j<n)
dp[i][j]=min(dp[i][j],dp[i-][j+k]);
}
}
int cal(int i,int j)
{
int k;
if(i<j)
{
i^=j;
j^=i;
i^=j;
}
k=;
while((<<k)<=(i-j+))
k++;
k--;
k=min(dp[k][j],dp[k][i-(<<k)+]);
return res[k];
}
int Dis(int u,int v)
{
int k = cal(pos[u],pos[v]);
return dis[u]+dis[v]-dis[k]*;
}
int tot = ;
void solve()
{
int i,j,k;
for(i=;i<=n;i++)
if(!vis[i])
dfs(i,);
n=n*-;
rmq();
printf("Case #%d:\n",tot);
for(int i=;i<=q;i++)
{
int u=read(),v=read();
int P = Dis(u,v);
int PP1 = Dis(u,A)+C+Dis(B,v);
int PP2 = Dis(u,B)+C+Dis(A,v);
PP1 = min(PP1,PP2);
printf("%d\n",max(P-PP1,));
}
}
int main()
{
int t=read();
while(t--)
{
tot++;
init();
solve();
}
return ;
}

cdoj 92 Journey tarjan/lca 树上点对距离的更多相关文章

  1. CDOJ 92 – Journey 【LCA】

    [题意]给出一棵树,有n个点(2≤N≤105),每条边有权值,现在打算新修一条路径,给出新路径u的起点v,终点和权值,下面给出Q(1≤Q≤105)个询问(a,b)问如果都按照最短路径走,从a到b节省了 ...

  2. CDOJ 92 Journey LCA乱搞

    原题链接:http://acm.uestc.edu.cn/#/problem/show/92 题意: 给你一棵树,然后在树上连接一条边.现在有若干次询问,每次问你两个点(u,v)之间的距离在加那条边之 ...

  3. CDOJ 92 Journey(LCA&RMQ)

    题目连接:http://acm.uestc.edu.cn/#/problem/show/92 题意:给定一棵树,最后给加一条边,给定Q次查询,每次查询加上最后一条边之后是否比不加这条边要近,如果近的话 ...

  4. poj 3417 Network(tarjan lca)

    poj 3417 Network(tarjan lca) 先给出一棵无根树,然后下面再给出m条边,把这m条边连上,然后每次你能毁掉两条边,规定一条是树边,一条是新边,问有多少种方案能使树断裂. 我们设 ...

  5. 洛谷 P2783 有机化学之神偶尔会做作弊(Tarjan,LCA)

    题目背景 LS中学化学竞赛组教练是一个酷爱炉石的人. 有一天他一边搓炉石一边监考,而你作为一个信息竞赛的大神也来凑热闹. 然而你的化竞基友却向你求助了. “第1354题怎么做”<--手语 他问道 ...

  6. Tarjan+LCA【洛谷P2783】 有机化学之神偶尔会做作弊

    [洛谷P2783] 有机化学之神偶尔会做作弊 题目背景 XS中学化学竞赛组教练是一个酷爱炉石的人. 有一天他一边搓炉石一边监考,而你作为一个信息竞赛的大神也来凑热闹. 然而你的化竞基友却向你求助了. ...

  7. [BZOJ3307]:雨天的尾巴(LCA+树上差分+权值线段树)

    题目传送门 题目描述: N个点,形成一个树状结构.有M次发放,每次选择两个点x,y对于x到y的路径上(含x,y)每个点发一袋Z类型的物品.完成所有发放后,每个点存放最多的是哪种物品. 输入格式: 第一 ...

  8. LCA UESTC 92 Journey

    题目传送门 题意:先给一棵树,然后有一条额外的边,问u走到v从现在最短的路走和原来不加边走的路节省了多少距离 分析:首先跑不加边的树的LCA,这样能求出任意两点的距离,那么现在x和y多连了一条边,如果 ...

  9. [luogu P3128][USACO15DEC]Max Flow [LCA][树上差分]

    题目描述 Farmer John has installed a new system of  pipes to transport milk between the  stalls in his b ...

随机推荐

  1. js控制元素的显示与隐藏

    <body class="easyui-layout"> <div id = "centerId" data-options="re ...

  2. Glimpse

    给自己程序配好Glimpse. Glimpse.Mvc 有问题 遇到稍微复杂点的内套多个PartialView,内存就爆了彪1.7g,不开Glimpse一点问题都没.另外Glimpse.Nlog也有问 ...

  3. 使用JRockit进行性能优化一:环境搭建

    1. jrockit简介   jrockit前身是BA jrockit,后被oracle收购,并免费发布,但并不开源.   jrockit可以看做是兼容标准的JDK基础上的JVM,同原有的JVM相比, ...

  4. poj3666

    一道不错的dp题 就是最小修改代价,使序列变为一个非下降序或非上升(由于数据较弱直接求非下降即可,当然非上升非下降本质是一样的) 观察可得到,修改后得到的数列中的元素最后一定都在原序列中: 由此我们可 ...

  5. Windows Phone 获取网络类型(GSM/CDMA/WIFI/Ethernet)

    一.判断是否有网络数据连接: 最基本的网络状态判断,如果没有网络连接,一切操作都进行不下去啦. Microsoft.Phone.Net.NetworkInformation.NetworkInterf ...

  6. C#中父窗口和子窗口之间实现控件互操作

    很多人都苦恼于如何在子窗体中操作主窗体上的控件,或者在主窗体中操作子窗体上的控件.相比较而言,后面稍微简单一些,只要在主窗体中创建子窗体的时候,保留所创建子窗体对象即可. 下面重点介绍前一种,目前常见 ...

  7. SCOI2009windy数

    数位DP,还不怎么会…… 其中calc函数的计算分为三部分: 第一部分:统计最高位为0的情况,或者说不足最高位位数的数的个数 第二部分:统计最高位为1到a[len]-1的情况,直接调用数组即可 第三部 ...

  8. liunx环境下安装mysql数据库

    一:如果你的机器上之前安装有mysql数据库,先进行卸载 (1)需要先将它的文件删除 (2)同时注意删除老板本的etc/my.cnf文件和/etc/mysql目录,这两个文件控制的是mysql的一些配 ...

  9. HttpServerUtility类

    HttpServerUtility是一个工具类,为了在后台处理请求方便获取到一些常用的类型,Asp.net将很多常用的东西封装到这里. 比如可以使用其进行URL编码解码, HTML编码解码等. // ...

  10. HDU 5288 OO’s Sequence

    题意:给一个序列,函数f(l, r)表示在[l, r]区间内有多少数字不是其他数字的倍数,求所有区间的f(l, r)之和. 解法:第一次打多校……心里还有点小激动……然而一道签到题做了俩点……呜呜呜… ...