题目链接:http://poj.org/problem?id=1986

Description

Farmer John's cows refused to run in his marathon since he chose a path much too long for their leisurely lifestyle. He therefore wants to find a path of a more reasonable length. The input to this problem consists of the same input as in "Navigation Nightmare",followed by a line containing a single integer K, followed by K "distance queries". Each distance query is a line of input containing two integers, giving the numbers of two farms between which FJ is interested in computing distance (measured in the length of the roads along the path between the two farms). Please answer FJ's distance queries as quickly as possible! 

Input

* Lines 1..1+M: Same format as "Navigation Nightmare"

* Line 2+M: A single integer, K. 1 <= K <= 10,000

* Lines 3+M..2+M+K: Each line corresponds to a distance query and contains the indices of two farms.

Output

* Lines 1..K: For each distance query, output on a single line an integer giving the appropriate distance. 

Sample Input

7 6
1 6 13 E
6 3 9 E
3 5 7 S
4 1 3 N
2 4 20 W
4 7 2 S
3
1 6
1 4
2 6

Sample Output

13
3
36

Hint

Farms 2 and 6 are 20+3+13=36 apart. 

题意:

输入第1~M+1行,与POJ 1984相同,代表了农场地图。

然后再一行有一个整数K代表询问数,

再然后有K个询问u和v之间最短距离。

题解:

本题的输入确定了农场地图是一棵树,并且本题不需要知道农场之间的位置关系,所以不需要记录东西南北。

树上两点间的最短距离,有两种情况:

①u是v的祖先,则dist(u,v) = dist(root,v) - dist(root,u)

②u不是v的祖先,那么从u到v必然要经过LCA(u,v),显然就是最短路径,则dist(u,v) = dist(root,u) - dist(root,LCA(u,v)) + dist(root,v) - dist(root,LCA(u,v))

不难发现,第①种情况下,dist(root,LCA(u,v)) = dist(root,u),那么①和②就可以统一为:dist(u,v) = dist(root,u) + dist(root,v) - 2 * dist(root,LCA(u,v))

所以我们只要计算出每个节点和树根的距离,求出所有查询(u,v)的LCA(u,v),就能得到dist(u,v)。

AC代码:

#include<cstdio>
#include<vector>
using namespace std; const int maxn=+; //节点数
const int maxm=+; //边数
const int maxq=+; //查询数 int par[maxn];
int find(int x){return (par[x]==x)?x:(par[x]=find(par[x]));} struct Edge{
int u,v,w;
Edge(int u=,int v=,int w=){this->u=u,this->v=v,this->w=w;}
};
vector<Edge> E;
vector<int> Ge[maxn];
void addedge(int u,int v,int w)
{
E.push_back(Edge(u,v,w));
Ge[u].push_back(E.size()-);
} struct Query{
int u,v;
int lca;
Query(int u=,int v=,int lca=){this->u=u,this->v=v,this->lca=lca;}
};
vector<Query> Q;
vector<int> Gq[maxn];
void addquery(int u,int v)
{
Q.push_back(Query(u,v));
Gq[u].push_back(Q.size()-);
} bool vis[maxn];
int dist[maxn];
void LCA(int u,int d)
{
par[u]=u; //建立以u为代表元素的集合
vis[u]=;
dist[u]=d;
for(int i=;i<Ge[u].size();i++)
{
Edge &e=E[Ge[u][i]]; int v=e.v;
if(!vis[v])
{
LCA(v,d+e.w);
par[v]=u; //将v的集合并入u的集合
}
}
for(int i=;i<Gq[u].size();i++)
{
Query &q=Q[Gq[u][i]]; int v=q.v;
if(vis[v])
{
q.lca=find(v);
Q[Gq[u][i]^].lca=q.lca;
}
}
} int m,n,k;
int main()
{
scanf("%d%d",&n,&m);
for(int i=;i<=m;i++)
{
int u,v,w; char d[];
scanf("%d%d%d%s",&u,&v,&w,d);
addedge(u,v,w);
addedge(v,u,w);
} scanf("%d",&k);
for(int i=;i<=k;i++)
{
int u,v;
scanf("%d%d",&u,&v);
addquery(u,v);
addquery(v,u);
} LCA(,); for(int i=;i<=k;i++)
{
printf("%d\n",dist[Q[(i-)*].u]+dist[Q[(i-)*].v]-*dist[Q[(i-)*].lca]);
}
}

最新文章

  1. 帆软报表FineReport中数据连接的JDBC连接池属性问题
  2. 程序员面试大揭秘——应聘微软、亚马逊、谷歌、苹果等IT公司你都要做什么准备?
  3. Python torndoa mysql 模块安装
  4. css3常用标签
  5. .net学习笔记---lambda表达式(自执行方法)
  6. paip.自动import的实现跟java.lang.SecurityException Prohibited package name java
  7. grootJS ui控件定义
  8. Ruiy自我识人做事领悟录ing
  9. 待整理 - BAT文件编写
  10. oracle根据pid查询出正在执行的执行语句
  11. MYSQL参数学习---------------- 张碧池
  12. [转] 详细整理:UITableView优化技巧
  13. MVCC的一种实现方案
  14. redhat6.3 64位更新源(使用网易源)全过程记录
  15. POJ3050 Hopscotch 【DFS】
  16. laravel利用subquery使左连接查询右表数据唯一查询
  17. Java之split()方法
  18. Docker学习笔记 - Docker的简介
  19. 让你的Spring Boot应用快速运行在Docker上面
  20. Git访问远程出现错误

热门文章

  1. IOS UILineBreakMode的各种情况分析
  2. Java Jdk1.8 HashMap源代码阅读笔记二
  3. JAVA WEB ------ 文件下载及导出数据到office Execl表格
  4. 新版本的body-parser中间件和morgan中间件引用问题:body-parser deprecated bodyParser和morgan deprecated morgan(options)
  5. win10进入到安全模式的三种方法
  6. 【RF库Collections测试】Log Dictionary 【同log list】
  7. [Command] lrzsz - 文件传输工具包
  8. 深入浅出MFC——MFC骨干程序(四)
  9. linux C 调用shell程序执行
  10. 决策树归纳算法之C4.5