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之后需要返回自己的 ...
随机推荐
- HDU 1253:胜利大逃亡(简单三维BFS)
pid=1253">胜利大逃亡 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/ ...
- mysql 存储引擎 InnoDB 与 MyISAM 的区别和选择
http://www.blogjava.net/jiangshachina/archive/2009/05/31/279288.html 酷壳 - MySQL: InnoDB 还是 MyISA ...
- C#应该掌握的一些东西
C#应该掌握的一些东西 随着培训机构的增多,越来越多的人进入IT行业.那么对于我们这些自学出来,经验不够丰富的转行者来说,我们需要掌握最起码的一些东西,这对于面试很有用,而且在工作中也很常用.本人 ...
- CentOS 6.4安装Puppet
CentOS安装Puppet 环境介绍:centos6.4x64 採用CentOS-6.4-x86_64-minimal.iso最小化安装 puppet版本号3.6.2.ruby1.8.7,f ...
- upstart man
man upstart nit(8) init(8) NAME init - Upstart process management daemon SYNOPSIS init [OPTION]... D ...
- Git bare repo with multiple branches
http://stackoverflow.com/questions/9324762/git-bare-repo-with-multiple-branches Q: I want to make a ...
- 转_【大话IT】你离大数据架构师有多远?
话题背景: 首先,先科普下“数据架构师”的相关职责:数据架构师要负责建立和维持公司数据储存的技术基准,策划硬体和软体的结构,确保数据储存系统可以支持未来的数据量和分析需求. 据了解,美国地区数据架构师 ...
- new和delete的基本用法
前言 new和delete是C++中用来动态管理内存分配的运算符,其用法较为灵活.如果你对它们的几种不同用法感到困惑,混淆,那么接着看下去吧. 功能一:动态管理单变量/对象空间 下面例子使用new为单 ...
- DotNetBar MessageBoxEx 显示中文 显示office2007风格
MessageBoxEx显示消息的时候按钮是中文的解决这个问题设置 MessageBoxEx的UseSystemLocalizedString属性为 true. MessageBoxEx.UseSys ...
- 设置netbeans文件编码格式
在项目ecmall上右键 选择属性,然后在项目属性里设置