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. 类的数据成员加前缀 m_(表示 member)

    类的数据成员加前缀 m_(表示 member),这样可以避免数据成员与 成员函数的参数同名. 例如: void Object::SetValue(int width, int height) { m_ ...

  2. bootstrap -- css -- 按钮

    本文中提到的按钮样式,适用于:<a>, <button>, 或 <input> 元素上 但最好在 <button> 元素上使用按钮 class,避免跨浏 ...

  3. CentOS运行级别和开机过程

    linux运行级别: 1)0:关机 2)1:单用户 3)2:多用户状态没有网络服务 4)3:多用户状态有网络服务 5)4:系统未使用保留给用户 6)5:图形界面 7)6:系统重启 注:常用运行级别是3 ...

  4. Session超时问题(AOP 过滤器)

    public class TimeoutAttribute : ActionFilterAttribute { public override void OnActionExecuting(Actio ...

  5. 【Java面试题】14 super.getClass()方法调用

    下面程序的输出结果是多少? import java.util.Date; public class Test extends Date{ public static void main(String[ ...

  6. 【MongoDB】数组长度查询

    db.groupedInfo.count({'surveyInfo.surveyAndUserID.0':{$exists:1}})

  7. CSS使用学习总结

    尽量少使用类,因为可以层叠识别,如: .News h3而不必在h3上加类 <div class=”News”> <h3></h3> <h2></h ...

  8. 如何Request客户端的传值的Data

    我们在做B/S的项目,客户端向服务端传值的时候,一般都是request接受. Request常用三个接受方式为:Request.QueryString,Request.Form,Request.Par ...

  9. javascript与 ios通讯解决办法

    阔别1年半之久,一个JavaScript和ios通讯的想法终于被实现了(我不知道别人有没有早就实现过~). 记得早期ios内嵌html做通讯时,貌似做好的办法只能是 ios通过url来截取页面发送消息 ...

  10. simple fix 主从不一致滴error

    Last_SQL_Error: Error 'Unknown table 'bb'' on query. Default database: 'test'. Query: 'DROP TABLE `b ...