How far away ?

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

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
模板题.离线算法:dfs+并查集.
/*
2586 31MS 10288K 1637 B G++
*/
#include"cstdio"
#include"cstring"
#include"vector"
using namespace std;
const int MAXN=;
typedef pair<int,int> P;
vector<P> G[MAXN];
vector<P> que[MAXN];
int par[MAXN];
int fnd(int x)
{
if(par[x]==x)
return x;
par[x]=fnd(par[x]);
}
int vis[MAXN];
int d[MAXN];
int ans[MAXN];
void dfs(int u,int fa)
{
par[u]=u;
for(int i=;i<que[u].size();i++)
{
P no=que[u][i];
if(vis[no.first]) ans[no.second]=d[u]+d[no.first]-*d[fnd(no.first)];
} vis[u]=;
for(int i=;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);
par[now.first]=u;
} }
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
memset(vis,,sizeof(vis));
memset(d,,sizeof(d));
memset(ans,,sizeof(ans));
int V,Q;
scanf("%d%d",&V,&Q);
for(int i=;i<=V;i++)
{
G[i].clear();
que[i].clear();
}
for(int i=;i<=V-;i++)
{
int u,v,cost;
scanf("%d%d%d",&u,&v,&cost);
G[u].push_back(P(v,cost));
G[v].push_back(P(u,cost));
}
for(int i=;i<=Q;i++)
{
int u,v;
scanf("%d%d",&u,&v);
que[u].push_back(P(v,i));
que[v].push_back(P(u,i));
}
dfs(,-);
for(int i=;i<=Q;i++)
{
printf("%d\n",ans[i]);
}
// printf("\n");
}
return ;
}
 

因为查询较少,朴素的求LCA算法就能过。

/*
Accepted 2586 62MS 7148K 1444B G++
*/
#include"cstdio"
#include"cstring"
#include"vector"
using namespace std;
const int MAXN=;
typedef pair<int,int> P;
vector<P> G[MAXN];
int depth[MAXN];
int parent[MAXN];
void dfs(int u,int fa,int d)
{
depth[u]=d;
parent[u]=fa;
for(int i=;i<G[u].size();i++)
{
P now=G[u][i];
if(now.first!=fa) dfs(now.first,u,d+);
}
}
void Swap(int &a,int &b)
{
int t=a;
a=b;
b=t;
}
int LCA(int u,int v)
{
int d=;
if(depth[u]<depth[v]) Swap(u,v);
while(depth[u]>depth[v])
{
int fa=parent[u];
for(int i=;i<G[fa].size();i++)
{
P now=G[fa][i];
if(now.first==u)
{
d+=now.second;
break;
}
}
u=fa;
}
while(u!=v)
{
int fa=parent[u];
for(int i=;i<G[fa].size();i++)
{
P now=G[fa][i];
if(now.first==u)
{
d+=now.second;
break;
}
}
u=fa; fa=parent[v];
for(int i=;i<G[fa].size();i++)
{
P now=G[fa][i];
if(now.first==v)
{
d+=now.second;
break;
}
}
v=fa;
}
return d;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int V,Q;
scanf("%d%d",&V,&Q);
for(int i=;i<=V;i++) G[i].clear(); for(int i=;i<=V-;i++)
{
int u,v,cost;
scanf("%d%d%d",&u,&v,&cost);
G[u].push_back(P(v,cost));
G[v].push_back(P(u,cost));
}
dfs(,-,);
while(Q--)
{
int u,v;
scanf("%d%d",&u,&v);
printf("%d\n",LCA(u,v));
}
}
return ;
}
 

HDU2586(LCA应用:在带权树中求任意两点之间的距离)的更多相关文章

  1. dfs+记忆化搜索,求任意两点之间的最长路径

    C.Coolest Ski Route 题意:n个点,m条边组成的有向图,求任意两点之间的最长路径 dfs记忆化搜索 #include<iostream> #include<stri ...

  2. LCA - 求任意两点间的距离

    There are n houses in the village and some bidirectional roads connecting them. Every day peole alwa ...

  3. Geotools求shapefile路网中任意两点之间最短路径的距离

    前言:之前在博问求助过这个问题.经过几天的思考,算是解决了(但仍有不足),另一方面对Geotools不是很熟,有些描述可能不正确,希望大家批评指正. 问题:作为一个新手,我并没有发现Geotools中 ...

  4. AOJ GRL_1_C: All Pairs Shortest Path (Floyd-Warshall算法求任意两点间的最短路径)(Bellman-Ford算法判断负圈)

    题目链接:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_1_C All Pairs Shortest Path Input ...

  5. 2018中国大学生程序设计竞赛 - 网络选拔赛 hdu Tree and Permutation 找规律+求任意两点的最短路

    Tree and Permutation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Oth ...

  6. hdu6446 网络赛 Tree and Permutation(树形dp求任意两点距离之和)题解

    题意:有一棵n个点的树,点之间用无向边相连.现把这棵树对应一个序列,这个序列任意两点的距离为这两点在树上的距离,显然,这样的序列有n!个,加入这是第i个序列,那么这个序列所提供的贡献值为:第一个点到其 ...

  7. UESTC(LCA应用:求两点之间的距离)

    Journey Time Limit: 15000/3000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Bob has ...

  8. Floyed-Warshall算法(求任意两点间最短距离)

    思路:感觉有点像暴力啊,反正我是觉得很暴力,比如求d[i][j],用这个方法求的话,就直接考虑会不会经过点k(k是任意一点) ,最终求得最小值 看代码 #include<iostream> ...

  9. poj - 3268 Silver Cow Party (求给定两点之间的最短路)

    http://poj.org/problem?id=3268 每头牛都要去标号为X的农场参加一个party,农场总共有N个(标号为1-n),总共有M单向路联通,每头牛参加完party之后需要返回自己的 ...

随机推荐

  1. 兔子-svnserver,client搭建

    http://www.cnblogs.com/xiaobaihome/archive/2012/03/20/2407610.html

  2. FastDFS的配置、部署与API使用解读(6)FastDFS配置详解之Storage配置(转)

    1 基本配置 disabled #func:该配置文件是否生效 #valu: ## true:无效 ## false:生效 disabled=false group_name #func:本stora ...

  3. 去除input框的值

    onfocus="this.value=' ';" <input type="text" name="buynum" id=" ...

  4. [转]JavaScript

    javascript 1. oncontextmenu="window.event.returnValue=false" 将彻底屏蔽鼠标右键 <table border on ...

  5. 基于UDP的一对回射客户/服务器程序

    前言 之前曾经学习过一对回射客户/服务器程序的例子,不过那个是基于TCP协议的.本文将讲解另一对回射客户/服务器程序,该程序基于UDP协议.由于使用的协议不同,因此编写出的程序也有本质上的区别,应将它 ...

  6. 基于docker/虚拟机的esp32远程工作流

    原文:基于docker/虚拟机的esp32远程工作流 工作流框图 背景说明 为什么需要这套工作流--为了满足高效和灵活的开发方式 因为我经常需要在公司和家里切换不同的电脑工作,所以编译环境需要在远程主 ...

  7. POJ 2263 Heavy Cargo(ZOJ 1952)

    最短路变形或最大生成树变形. 问 目标两地之间能通过的小重量. 用最短路把初始赋为INF.其它为0.然后找 dis[v]=min(dis[u], d); 生成树就是把最大生成树找出来.直到出发和终点能 ...

  8. AAC包增加ADTS头Without MediaCodec

    AAC原始码流无法直接播放,一般需要封装为ADTS格式才能再次使用,本博主在android中用MediaCodec编码得到的AAC就是raw格式,为了保存为.aac格式,需要增加adts头,这样就可以 ...

  9. JQuery ajax 把后台返回的List数据 遍历出来 赋值给div

    1.效果 2.前端代码 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <he ...

  10. aop学习总结三----aop的相关概念

    aop学习总结三----aop的相关概念 public Object invoke(Object proxy, Method method, Object[] args) throws Throwab ...