HDU2586(LCA应用:在带权树中求任意两点之间的距离)
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
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.
/*
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应用:在带权树中求任意两点之间的距离)的更多相关文章
- dfs+记忆化搜索,求任意两点之间的最长路径
C.Coolest Ski Route 题意:n个点,m条边组成的有向图,求任意两点之间的最长路径 dfs记忆化搜索 #include<iostream> #include<stri ...
- LCA - 求任意两点间的距离
There are n houses in the village and some bidirectional roads connecting them. Every day peole alwa ...
- Geotools求shapefile路网中任意两点之间最短路径的距离
前言:之前在博问求助过这个问题.经过几天的思考,算是解决了(但仍有不足),另一方面对Geotools不是很熟,有些描述可能不正确,希望大家批评指正. 问题:作为一个新手,我并没有发现Geotools中 ...
- 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 ...
- 2018中国大学生程序设计竞赛 - 网络选拔赛 hdu Tree and Permutation 找规律+求任意两点的最短路
Tree and Permutation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Oth ...
- hdu6446 网络赛 Tree and Permutation(树形dp求任意两点距离之和)题解
题意:有一棵n个点的树,点之间用无向边相连.现把这棵树对应一个序列,这个序列任意两点的距离为这两点在树上的距离,显然,这样的序列有n!个,加入这是第i个序列,那么这个序列所提供的贡献值为:第一个点到其 ...
- UESTC(LCA应用:求两点之间的距离)
Journey Time Limit: 15000/3000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Bob has ...
- Floyed-Warshall算法(求任意两点间最短距离)
思路:感觉有点像暴力啊,反正我是觉得很暴力,比如求d[i][j],用这个方法求的话,就直接考虑会不会经过点k(k是任意一点) ,最终求得最小值 看代码 #include<iostream> ...
- poj - 3268 Silver Cow Party (求给定两点之间的最短路)
http://poj.org/problem?id=3268 每头牛都要去标号为X的农场参加一个party,农场总共有N个(标号为1-n),总共有M单向路联通,每头牛参加完party之后需要返回自己的 ...
随机推荐
- POJ 1018 Communication System 题解
本题一看似乎是递归回溯剪枝的方法.我一提交,结果超时. 然后又好像是使用DP,还可能我剪枝不够. 想了非常久,无奈忍不住偷看了下提示.发现方法真多.有贪心,DP,有高级剪枝的.还有三分法的.八仙过海各 ...
- LattePanda 项目之 P2.2 起飞条件检测系统(CLI & GUI)
前言 原创文章,转载引用务必注明链接,水平有限,如有疏漏,欢迎指正. 本文使用Markdown写成,为获得更好的阅读体验和正常的链接.图片显示,请访问我的博客原文: http://www.cnblog ...
- 非常不错的ajax原理总结
在工作中用了Ajax N多次了,也看过一些相关方面的书籍,也算是认识了它,但是一直没有认真总结和整理过相关的东东,失败!近有闲情,将之总结如下:[名称]Ajax是Asynchronous JavaSc ...
- 小白学phoneGap《构建跨平台APP:phoneGap移动应用实战》连载一(PhoneGap中的API)
之前本博连载过<构建跨平台APP:jQuery Mobile移动应用实战>一书.深受移动开发入门人员的喜爱. 从如今開始,连载它的孪生姐妹书phoneGap移动应用实战一书,希望以前是小白 ...
- vue2.0 自定义 图片上传(UpLoader)组件
1.自定义组件 UpLoader.vue <!-- 上传图片 组件 --> <template> <div class="vue-uploader"& ...
- 日常沟通的 3 种模式zz
一.日常沟通的 3 种模式 根据NLP (Neuro-Linguistic Programming,神经语言程序学),日常沟通的 3 种模式分别是:上堆.下切和平行,它们是什么意思呢? 1.上堆 意思 ...
- 几篇QEMU/KVM代码分析文章
QEMU/KVM结合起来分析的几篇文章,代码跟最新的版本有些差异,但大体逻辑一样,写得通俗易懂.我把链接放这里主要是为自己需要查看时调转过去方便,感谢作者的付出! QEMU Source Code S ...
- 错误 1 error C1083: 无法打开包括文件: “numpy/arrayobject.h”: No such file
问题:错误 1 error C1083: 无法打开包括文件: “numpy/arrayobject.h”: No such file 解答:加入include路径:E:\env\Anaconda2x6 ...
- 关闭和定时显示DIV
<script type="text/javascript"> $(function(){ $('#ad1').css('display','block'); }) f ...
- 关于TCP通信程序中数据的传递格式
前言 在之前的回射程序中,实现了字符串的传递与回射.幸运的是,字符串的传递不用担心不同计算机类型的大小端匹配问题,然而,如果传递二进制数据,这就是一个要好好考虑的问题.在客户端和服务器使用不同的字节序 ...