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. java设计模式——多例模式

    ★ 缓存在单例中的使用    缓存在编程中使用很频繁,有着非常重要的作用,它能够帮助程序实现以空间换取时间,通 常被设计成整个应用程序所共享的一个空间,现要求实现一个用缓存存放单例对象的类. 说明:该 ...

  2. Android 利用cursor来进行排序(转至http://blog.csdn.net/yangzongquan/article/details/6547860)

    主要思路是:override move系列的方法,让cursor以自己想要的顺序来移动,从而达到对cursor排序的目的.比如数组A0里有 4(0),3(1),1(2),2(3),括号内为位置,排序后 ...

  3. UIView的几个枚举定义

    UIView是iOS开发最主要的视图,非常多控件都是继承它,掌握当中的几个基本枚举定义,有利益理解视图的载入和參数差别. 一.UIViewAnimationCurve UIView的基本动画变化规律 ...

  4. opencv实例三:播放AVI格式视频

    一.不带滚动条的视频读取播放. 1.原理介绍:视频的本质是一些静态的图像的集合,opencv可以不断读取视屏中的图片,显示,就可以实时的视频流进行处理了. 2.代码如下: /************* ...

  5. c++ list reverse_iterator

    #pragma warning(disable:4786) #include <set> #include <iostream> using namespace std ; t ...

  6. Digest Authentication 摘要认证

    “摘要”式认证( Digest authentication)是一个简单的认证机制,最初是为HTTP协议开发的,因而也常叫做HTTP摘要,在RFC2671中描述.其身份验证机制很简单,它采用杂凑式(h ...

  7. 【算法】CRF

    http://www.open-open.com/doc/view/7e983c0bf1594849bcd088dc212098c4 http://wenku.baidu.com/link?url=c ...

  8. mybatis由浅入深day01_ 7输入映射(7.1传递pojo的包装对象_7.2#{}与${}_7.3传递简单类型_7.4传递pojo对象_7.5传递hashmap)

    7 输入映射 通过parameterType指定输入参数的类型,类型可以是简单类型.hashmap.pojo的包装类型. 7.1 传递pojo的包装对象 7.1.1 需求 完成用户信息的综合查询,需要 ...

  9. Effective C++ Item 13 Use object to manage resources

    1. Always use object to manage resource! If you delete a pointer or release a handler manually by yo ...

  10. chrome JS 总结

    1. chrome 的 console 中不能添加本地文件 2. 下面的代码是在亚马逊的商品页面上添加一个 image, 点击之后触发 alert 函数. 其中 cBoxInner 是人工寻找到的标签 ...