How far away ?

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 18191    Accepted Submission(s): 7072

Problem Description
There are n houses in the village and some bidirectional roads connecting them. Every day peole always like to ask like this "How far is it if I want to go from house A to house B"? Usually it hard to answer. But luckily int this village the answer is always unique, since the roads are built in the way that there is a unique simple path("simple" means you can't visit a place twice) between every two houses. Yout task is to answer all these curious people.
 
Input
First line is a single integer T(T<=10), indicating the number of test cases.
  For each test case,in the first line there are two numbers n(2<=n<=40000) and m (1<=m<=200),the number of houses and the number of queries. The following n-1 lines each consisting three numbers i,j,k, separated bu a single space, meaning that there is a road connecting house i and house j,with length k(0<k<=40000).The houses are labeled from 1 to n.
  Next m lines each has distinct integers i and j, you areato answer the distance between house i and house j.
 
Output
For each test case,output m lines. Each line represents the answer of the query. Output a bland line after each test case.
 
Sample Input
2
3 2
1 2 10
3 1 15
1 2
2 3

2 2
1 2 100
1 2
2 1

 
Sample Output
10
25
100
100
 
Source
 
  • LCA模板题
  • 这里两种代码,一种离线tarjan一种在线RMQ+ST
 #include <iostream>
#include <string>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <climits>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
using namespace std;
typedef long long LL ;
typedef unsigned long long ULL ;
const int maxn = 1e5 + ;
const int inf = 0x3f3f3f3f ;
const int npos = - ;
const int mod = 1e9 + ;
const int mxx = + ;
const double eps = 1e- ;
const double PI = acos(-1.0) ; /* LCA + ST + RMQ Online */
struct node{
int v, next;
LL w;
};
node edge[maxn<<];
int fac[];
int vis[maxn];
int head[maxn<<], cnt;
int ver[maxn<<], dep[maxn<<], first[maxn], tot;
int dp[maxn<<][];
LL dis[maxn];
void addedge(int x, int y, LL z){
edge[cnt].v=y;
edge[cnt].w=z;
edge[cnt].next=head[x];
head[x]=cnt++;
}
void dfs(int u, int d){
tot++;
vis[u]=;
ver[tot]=u;
dep[tot]=d;
first[u]=tot;
for(int i=head[u];i!=-;i=edge[i].next){
int v=edge[i].v;
LL w=edge[i].w;
if(!vis[v]){
dis[v]=dis[u]+w;
dfs(v,d+);
tot++;
ver[tot]=u;
dep[tot]=d;
}
}
}
void ST(int n){
int k=(int)(log((double)n)/log(2.0));
for(int i=;i<=n;i++)
dp[i][]=i;
for(int j=;j<=k;j++)
for(int i=;i+fac[j]-<=n;i++){
int pl=dp[i][j-];
int pr=dp[i+fac[j-]][j-];
if(dep[pl]<dep[pr])
dp[i][j]=pl;
else
dp[i][j]=pr;
}
}
int RMQ(int l, int r){
int k=(int)(log((double)(r-l+))/log(2.0));
int pl=dp[l][k];
int pr=dp[r-fac[k]+][k];
if(dep[pl]<dep[pr])
return pl;
else
return pr;
}
int LCA(int u, int v){
int x=first[u];
int y=first[v];
if(x>y)swap(x,y);
return ver[RMQ(x,y)];
}
int T, n, m, u, v;
LL w;
int main(){
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
for(int i=;i<;i++)
fac[i]=(<<i);
while(~scanf("%d",&T)){
while(T--){
scanf("%d %d",&n,&m);
cnt=;
memset(head,-,sizeof(head));
memset(vis,,sizeof(vis));
for(int i=;i<n;i++){
scanf("%d %d %lld",&u,&v,&w);
addedge(u,v,w);
addedge(v,u,w);
}
dis[]=;
tot=;
dfs(,);
ST(tot);
while(m--){
scanf("%d %d",&u,&v);
printf("%lld\n",dis[u]+dis[v]-*dis[LCA(u,v)]);
}
}
}
return ;
}
 #include <iostream>
#include <string>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <climits>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
using namespace std;
typedef long long LL ;
typedef unsigned long long ULL ;
const int maxn = 1e5 + ;
const int inf = 0x3f3f3f3f ;
const int npos = - ;
const int mod = 1e9 + ;
const int mxx = + ;
const double eps = 1e- ;
const double PI = acos(-1.0) ; /* Tarjan Offline */
struct node{
int v, next;
LL w;
};
node edge[maxn<<], ask[maxn<<];
int head0[maxn<<], cnt0;
int head1[maxn<<], cnt1;
int vis[maxn], fa[maxn];
LL ans[], dis[maxn];
void addedge(int x, int y, LL z, int flag){
if(flag){
ask[cnt1].v=y;
ask[cnt1].w=z;
ask[cnt1].next=head1[x];
head1[x]=cnt1++;
}else{
edge[cnt0].v=y;
edge[cnt0].w=z;
edge[cnt0].next=head0[x];
head0[x]=cnt0++;
}
}
int find(int u){
if(u==fa[u])
return u;
else
return fa[u]=find(fa[u]);
// return u==fa[u]?u:find(fa[u]);
}
void Union(int x, int y){
int fx=find(x);
int fy=find(y);
if(fx!=fy)
fa[fy]=fx;
}
void Tarjan(int u){
vis[u]=;
fa[u]=u;
for(int i=head1[u];i!=-;i=ask[i].next){
int v=ask[i].v;
if(vis[v]){
int lca=find(v);
int id=ask[i].w;
ans[id]=dis[u]+dis[v]-*dis[lca];
}
}
for(int i=head0[u];i!=-;i=edge[i].next){
int v=edge[i].v;
LL w=edge[i].w;
if(!vis[v]){
dis[v]=dis[u]+w;
Tarjan(v);
Union(u,v);
}
}
}
int T, n, m, u, v;
LL w;
int main(){
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
while(~scanf("%d",&T)){
while(T--){
scanf("%d %d",&n,&m);
cnt0=;
cnt1=;
memset(head0,-,sizeof(head0));
memset(head1,-,sizeof(head1));
memset(vis,,sizeof(vis));
for(int i=;i<n;i++){
scanf("%d %d %lld",&u,&v,&w);
addedge(u,v,w,);
addedge(v,u,w,);
}
for(int i=;i<=m;i++){
scanf("%d %d",&u,&v);
addedge(u,v,(LL)i,);
addedge(v,u,(LL)i,);
}
dis[]=;
Tarjan();
for(int i=;i<=m;i++)
printf("%lld\n",ans[i]);
}
}
return ;
}

HDU_2586_How far away ?的更多相关文章

  1. 先说IEnumerable,我们每天用的foreach你真的懂它吗?

    我们先思考几个问题: 为什么在foreach中不能修改item的值? 要实现foreach需要满足什么条件? 为什么Linq to Object中要返回IEnumerable? 接下来,先开始我们的正 ...

  2. 平台之大势何人能挡? 带着你的Net飞奔吧!

    镇楼图: 跨平台系列: Linux基础 1.Linux基础学习 By dnt http://www.cnblogs.com/dunitian/p/4822807.html 环境配置 1.Hyper-v ...

  3. 谈谈一些有趣的CSS题目(十一)-- reset.css 知多少?

    开本系列,谈谈一些有趣的 CSS 题目,题目类型天马行空,想到什么说什么,不仅为了拓宽一下解决问题的思路,更涉及一些容易忽视的 CSS 细节. 解题不考虑兼容性,题目天马行空,想到什么说什么,如果解题 ...

  4. 百度推出新技术 MIP,网页加载更快,广告呢?

    我们在2016年年初推出了MIP,帮助移动页面加速(原理).内测数据表明,MIP页面在1s内加载完成.现在已经有十多家网站加入MIP项目,有更多的网站正在加入中.在我们收到的反馈中,大部分都提到了广告 ...

  5. 哪种缓存效果高?开源一个简单的缓存组件j2cache

    背景 现在的web系统已经越来越多的应用缓存技术,而且缓存技术确实是能实足的增强系统性能的.我在项目中也开始接触一些缓存的需求. 开始简单的就用jvm(java托管内存)来做缓存,这样对于单个应用服务 ...

  6. 复杂的 Hash 函数组合有意义吗?

    很久以前看到一篇文章,讲某个大网站储存用户口令时,会经过十分复杂的处理.怎么个复杂记不得了,大概就是先 Hash,结果加上一些特殊字符再 Hash,结果再加上些字符.再倒序.再怎么怎么的.再 Hash ...

  7. 你知道C#中的Lambda表达式的演化过程吗?

    那得从很久很久以前说起了,记得那个时候... 懵懂的记得从前有个叫委托的东西是那么的高深难懂. 委托的使用 例一: 什么是委托? 个人理解:用来传递方法的类型.(用来传递数字的类型有int.float ...

  8. Linq表达式、Lambda表达式你更喜欢哪个?

    什么是Linq表达式?什么是Lambda表达式? 如图: 由此可见Linq表达式和Lambda表达式并没有什么可比性. 那与Lambda表达式相关的整条语句称作什么呢?在微软并没有给出官方的命名,在& ...

  9. 搞个这样的APP要多久?

    这是一个“如有雷同,纯属巧合”的故事,外加一些废话,大家请勿对号入座.开始了…… 我有些尴尬地拿着水杯,正对面坐着来访的王总,他是在别处打拼的人,这几年据说收获颇丰,见移动互联网如火如荼,自然也想着要 ...

随机推荐

  1. 测试x264编码器的低延时编码和非延时编码

    最近在学x264的编码,经过大量的测试,编码1080P的视频,编码10000帧数据. 在设置为低延时编码的时候: 编码线程0,一帧耗时:7.000000 ms.编码线程0,一帧耗时:8.000000 ...

  2. 同一页面中引入多个JS库产生的冲突解决方案(转)

    发生JS库冲突的主要原因:与jQuery库一样,许多JS库都使用‘$’符号作为其代号.因此在一个页面中引入多个JS库,并且使用‘$’作为代号时,程序不能识别其代表哪个库(这个是我自己的解释,但更深的原 ...

  3. linux下简单好用的端口映射转发工具rinetd 转

    linux下简单好用的工具rinetd,实现端口映射/转发/重定向 官网地址http://www.boutell.com/rinetd 软件下载 wget http://www.boutell.com ...

  4. 【Java面试题】18 java中数组有没有length()方法?string没有lenght()方法?下面这条语句一共创建了多少个对象:String s="a"+"b"+"c"+"d";

    数组没有length()这个方法,有length的属性.String有有length()这个方法. int a[]; a.length;//返回a的长度 String s; s.length();// ...

  5. eclipse (ADT) svn插件 过滤上传的 文件 文件夹 一劳永逸

    其实很简单哈,过滤的有三种类型,1.文件.2.文件夹.3.android的target 在ADT中 window->preferences-> 会打开如下界面 ignore就是忽视的意思 ...

  6. 无法在Word中打开MathType怎么办

    MathType是一种数学公式编辑器,通常我们都是与Office文档配合使用,但是如果大家在Word中使用MathType编辑公式时,遇到MathType无法打开的情况,我们应该怎么办?下面我们就针对 ...

  7. JsonConvert.DeserializeAnonymousType

    string JsApiTicketString = string.Empty; using (StreamReader reader = new StreamReader(response.GetR ...

  8. PL/SQL如何调试Oracle存储过程

    from:http://jingyan.baidu.com/article/3a2f7c2e144d2826aed61167.html 调试过程对找到一个存过的bug或错误是非常重要的,Oracle作 ...

  9. Java 类设计----Java类的继承

    Java类的继承 为描述和处理个人信息,定义类Person: public class Person { public String name; public inat age; public Dat ...

  10. 有人问thinkphp的标签解析的时候为什么出现标签内内容空格丢失

    举例如下 该代码被解析后 变为 并不是  active li bg  这里面的空格没有了 我试了多次,确实是这样,后来想了想 应该是框架解析的时候自动处理了,然后找了找框架代码 Template.cl ...