HDU - 2586 - How far away ? (最短路)

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

题目大意:

给你一个带权无向图,有n个顶点,n-1条边。然后有m次询问,每次询问 有 a b两个数,需要输出a b 之间的最短路。

抽象一下,就是求任意两点之间的最短路。

解题思路:

任意两点最短路???首先想到的肯定是Floyd算法,但是。。。数据量n=1e4…复杂度0(n3)肯定是不行的
然后我就考虑把询问里的数按出现次序排序,然后先用Dijkstra跑出现次数最多的顶点w为起点的最短路,然后把结果用个 标记数组 book[i][j]记录下来,然后遇到已经跑过的最短路,也就是book数组里有的就直接输出,然后每次都跑Dijkstra。。。虽然i想的很美好,但是却给我显示超内存了???

无奈之下,上网搜了一波题解
题解上用dfs每次询问,找一下最短路。。。简单粗暴又实用。。。数据量是1e4 边数 也是1e4的数据量 询问数<100…所以也不会超时。。

搜索的时候我们用个标记数组vis[]表示这个点是否经过过,然后遇到中终点就输出结果,不是终点就继续dfs。

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#define inf 0x3f3f3f3f
using namespace std;
const int maxn=40009;
int vis[maxn];
int head[maxn];
int cnt=0;
int n,m;
struct node{int to;int val;int next;
}side[maxn*2];
void init()
{memset(head,-1,sizeof(head));memset(vis,0,sizeof(vis));cnt=0;
}
void add(int x,int y,int w)
{side[cnt].to=y;side[cnt].val=w;side[cnt].next=head[x];head[x]=cnt++;
}
void dfs(int a,int b,int step)
{vis[a]=1;for(int i=head[a];i!=-1;i=side[i].next){int k=side[i].to;if(vis[k])continue;if(k==b){cout<<step+side[i].val<<endl;return ;} else{dfs(k,b,step+side[i].val);}}
}
int main()
{int T;cin>>T;while(T--){cin>>n>>m;init();for(int i=0;i<n-1;i++){int a,b,c;scanf("%d %d %d",&a,&b,&c);add(a,b,c);add(b,a,c);}while(m--){int a,b;cin>>a>>b;memset(vis,0,sizeof(vis));dfs(a,b,0);}}return 0;
}

HDU - 2586 - How far away ? (最短路)相关推荐

  1. hdu - 2586 How far away ?(最短路共同祖先问题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 最近公共祖先问题~~LAC离散算法 题目大意:一个村子里有n个房子,这n个房子用n-1条路连接起 ...

  2. hdu 5636 Shortest Path(Floyd最短路)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5636 解题思路: 这道题可以用Floyd解决,不过需要特殊处理一下: 实际上我们只需要利用添加的那三条 ...

  3. HDU 2586 How far away ?【LCA】

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2586 题意: 无向图,给定边及边权重,任意两点之间都有一条唯一的道路,道路上每个点只能出现一次.给定 ...

  4. 【HDU】1535 Invitation Cards 最短路

    传送门:[HDU]1535 Invitation Cards 题目分析:题目真难读......其实题目的意思就是让求从编号为1的点到其他所有点的最短路距离之和加上其他所有点到编号为1的点的最短路距离之 ...

  5. 【HDU】-2112-HDU Today(最短路)

    HDU Today Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  6. D - Age of Moyu HDU - 6386 -BFS+DFS分层最短路

    D - Age of Moyu HDU - 6386 题意:双向图,m条边,每条边有一个编号id,而花费就是根据这个id的变化来确定的求1-n的最短路 思路:全值为0,1的图直接bfs按层遍历图找到n ...

  7. hdu 4460 friend chains spfa 最短路里面的最长路

    题意不再赘述... 连接http://acm.hdu.edu.cn/showproblem.php?pid=4460 此题直接郁闷致死.... 比赛的时候用的是floyd然后直接超时...当时闪过sp ...

  8. 【HDU - 5889】Barricade(最短路+网络流,最小割)

    题干: The empire is under attack again. The general of empire is planning to defend his castle. The la ...

  9. HDU 5294 Tricks Device(最短路+最大流)

    题意:给一个无向图(连通的),张在第n个点,吴在第1个点,'吴'只能通过最短路才能到达'张',两个问题:(1)张最少毁掉多少条边后,吴不可到达张(2)吴在张毁掉最多多少条边后仍能到达张. 思路:将所有 ...

  10. HDU 2586 How far away ? LCA ---tanjar+并查集 离线算法

    tanjar算法离线求LCA的思想主要是利用并查集的思想. 求距离的话就是d[start[i]]+end[en[i]]-2*d[lca[i]]; 首先从根节点dfs,在深度遍历的回溯的过程中不断的更新 ...

最新文章

  1. ⑥python模块初识、pyc和PyCodeObject
  2. 技术QA:在 Outlook 2000 里为何不能取消“对已读回执的请求的使用”?
  3. 如何在一个背景图像上,做半透明的图。
  4. WGAN的提出背景以及解决方案
  5. js中const,var,let区别与用法
  6. 魅族 虚拟位置服务状态:未运行_魅族17评测:「重量平衡设计」 手感出色 魅友们的5G梦想旗舰...
  7. Python 三级菜单
  8. python画图代码-Python为啥这么牛?一行Python代码除了画图竟然还有这些功能!
  9. python redis模块常用_python redis 模块
  10. duilib开发基础:创建自定义控件的过程
  11. 根据身份证号 计算具体年龄
  12. 身高预测_大部分都很准哦
  13. python 安装失败 errorcode 2203_win10系统安装软件出现the error code is 2203错误怎么办...
  14. 前端搭建小人逃脱游戏(内附源码)
  15. Mifare UltraLight 卡存储结构
  16. MaxCompute(ODPS)一对多连表时实现多行过滤(同样适用于MySQL)
  17. BandZIP无广告版(v6.25)安装及禁止联网设置
  18. 可汗学院计算机课程都有哪些,要录制可汗学院教学视频你需要哪些硬件和软件?...
  19. linux环境下常用的打包、压缩、解压命令(tar、gzip、bzip2、zip)
  20. 软件测试的底层逻辑思维是什么?

热门文章

  1. 最全iOS12捷径库收集整理,iOS12捷径推荐
  2. 算法设计与分析: 2-13 标准二维表问题
  3. php毕业论文总结,毕业设计总结
  4. mail163邮箱官网如何注册?
  5. Jsonp跨域原理及实现
  6. 个人定制ESXi安装程序(集成三方网卡驱动程序)
  7. linux 声音设置,Linux aumix设置音效装置命令详解
  8. 数据科学----知识树(机器学习、数据挖掘学习思维导图)
  9. gitlab无法推送
  10. word文档页眉清除和页码设置