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. Ejb in action(六)——拦截器

    Ejb拦截器可以监听程序中的一个或全部方法.与Struts2中拦截器同名,并且他们都可以实现切面式服务.同一时候也与Spring中的AOP技术类似. 不同的是struts2的拦截器的实现原理是一层一层 ...

  2. ORCAD元件属性白色区域和黄色区域的理解

    白色部分为instance属性,黄色部分为occurence 属性 在平坦式电路中,黄色部分是默认不显示的. 在层次式电路中,黄色部分会显示.      如果这两个区域的Reference不同,以黄色 ...

  3. Logical Volume Manager (Linux)

    http://en.wikipedia.org/wiki/Logical_Volume_Manager_(Linux) Logical Volume Manager (Linux) From Wiki ...

  4. 关于0基础磁盘管理(gpt UEFI...)最好的一篇文章(来自gentoo linux)

    放链接:https://wiki.gentoo.org/wiki/Handbook:AMD64/Installation/Disks 顺便几张图 watermark/2/text/aHR0cDovL2 ...

  5. B树的生成

    B树的生成 flyfish 2015-7-19 从空树開始构建一棵B树 逐个插入keyword 规则: 除根结点之外的全部非终端结点至少有⌈m/2⌉棵子树,所以keyword的个数必须 n为keywo ...

  6. 网页编程-Djiango(二)

    一.初始Ajax ajax的写法: $.ajax({ url:'/host', type:'POST' data:{'k1':123,'k2':'root'} success:function(dat ...

  7. Error (167005): Can't assign I/O pad "GX_TX" to PIN_AG27 because this causes failure in the placement of the other atoms in its associated channel

    1.同时在两个GX的bank,建立两GX ip core 会出现 两个IP的cal_blk_clk信号,要保持是同一个时钟

  8. hdu1878欧拉回路(DFS+欧拉回路)

    欧拉回路 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

  9. POSIX标准中的 “ 限制 ”

    前言 在POSIX标准中,定义了许多限制.这些限制大约分为五类,不同类型的限制获取的方式不一样. 限制值分类 1. 不变的最小值 这类型的限制值是静态的,固定的. 2. 不变值 同上 3. 运行时可以 ...

  10. 图像处理之opencv---mat、cvmat、IplImage之间的转换

    一.Mat类型:矩阵类型,Matrix.  在openCV中,Mat是一个多维的密集数据数组.可以用来处理向量和矩阵.图像.直方图等等常见的多维数据. Mat有3个重要的方法: 1.Mat mat = ...