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
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=100005;
typedef pair<int,int> P;
vector<P> G[MAXN];
int depth[2*MAXN];
int vs[MAXN*2];
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=0;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*2][20];
void init_rmq(int n)
{
    for(int i=1;i<=n;i++)    dp[i][0]=depth[i];

    int m=floor(log(n*1.0)/log(2.0));
    for(int j=1;j<=m;j++)
        for(int i=1;i<=n-(1<<j)+1;i++)
            dp[i][j]=min(dp[i][j-1],dp[i+(1<<(j-1))][j-1]);
}
int rmq(int a,int b)
{
    int k=floor(log((b-a+1)*1.0)/log(2.0));
    return min(dp[a][k],dp[b-(1<<k)+1][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]-2*d[LCA(u,v)];
}
int main()
{
    int T;
    scanf("%d",&T);
    for(int cas=1;cas<=T;cas++)
    {
        memset(0,sizeof(d),0);
        cnt=0;
        dep=0;
        int V,Q;
        scanf("%d%d",&V,&Q);
        for(int i=1;i<=V;i++)    G[i].clear();
        for(int i=1;i<=V-1;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(1,0);
        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 0;
}
时间: 2024-08-07 00:15:28

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

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

Problem Description 输入平面坐标系中2点的坐标,输出它们之间的距离 Input 输入4个浮点数x1 y1 x2 y2,分别是点(x1,y1) (x2,y2)的坐标(多组数据) Output 输出它们之间的距离,保留2位小数(每组数据一行) Sample Input 1 0 2 0 Sample Output 1.00 1 #include<stdio.h> 2 3 #include<math.h> 4 5 6 7 int main() 8 9 { 10 11 f

求两点之间的距离

import java.util.Scanner; public class TestObject {     public static void main(String[] args) {         // TODO Auto-generated method stub         Scanner in = new Scanner(System.in);         System.out.println("请输入第一个坐标点:");         int x1 = i

poj 2251(广搜求两点之间的距离)

题目链接:http://poj.org/problem?id=2251 Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16682   Accepted: 6491 Description You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit

利用结构类型的相关知识计算两点之间的距离

#include<stdio.h>#include<stdlib.h>#include<math.h> struct point{ /*点的结构类型名*/ float x; /*横坐标*/ float y; /*纵坐标*/ }; struct point readPoint(); /*函数原型声明*/float distance(struct point p1,struct point p2);/*主函数*/ int main(void){ struct point a

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

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

openlayer3计算两点之间的距离

openlayer3计算两点之间的距离 对应的openlayers的版本为3.7. 主要用的接口是ol.Sphere.haversineDistance([x1,y1],[x2,y2]): 4326坐标系中计算两点距离的方式为: var wgs84Sphere = new ol.Sphere(6378137); wgs84Sphere.haversineDistance(C1,C2); 示例为: var wgs84Sphere = new ol.Sphere(6378137); wgs84Sph

两点之间的距离

import javax.swing.JOptionPane; public class Distance { public static void main(String[] args){ String input = JOptionPane.showInputDialog(null,"请输入x1的坐标:","对话框",JOptionPane.QUESTION_MESSAGE); String input1 = JOptionPane.showInputDialo

使用友元函数计算两点之间的距离

#include <iostream> #include <cmath> using namespace std; class CPoint//点类 { private: double x;//横坐标 double y;//纵坐标 public: //使用初始化表初始化数据成员 CPoint(double xx=0,double yy=0):x(xx),y(yy){} //定义友元函数用于计算两点之间的距离 friend double Distance(CPoint &p1

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

--通过经纬度计算两点之间的距离 create FUNCTION [dbo].[fnGetDistanceNew] --LatBegin 开始经度 --LngBegin 开始维度 --29.490295,106.486654,29.615467, 106.581515 (@LatBegin1 varchar(128), @LngBegin1 varchar(128),@location varchar(128)) Returns real AS BEGIN --转换location字段,防止字段