hdu----(2586)How far away ?(DFS/LCA/RMQ)
How far away ?
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5492 Accepted Submission(s): 2090
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.
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.
3 2
1 2 10
3 1 15
1 2
2 3
2 2
1 2 100
1 2
2 1
25
100
100
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<vector>
#include<algorithm>
#include<iostream>
using namespace std;
const int maxn=;
struct node
{
int id,val;
};
bool vis[maxn];
vector< node >map[maxn];
node tem;
int n,m,ans,cnt;
void dfs(int a,int b)
{ if(a==b){
if(ans>cnt)ans=cnt;
return ;
}
int Size=map[a].size();
vis[a]=;
for(int i=;i<Size;i++){
if(!vis[map[a][i].id]){
cnt+=map[a][i].val;
dfs(map[a][i].id,b);
cnt-=map[a][i].val;
}
}
vis[a]=;
}
int main()
{
int cas,a,b,val;
cin>>cas;
while(cas--){
cin>>n>>m;
cnt=;
for(int i=;i<=n;i++)
map[i].clear();
for(int i=;i<n;i++){
scanf("%d%d%d",&a,&b,&val); tem=(node){b,val};
map[a].push_back(tem); //ÎÞÏòͼ
tem=(node){a,val};
map[b].push_back(tem);
}
for(int i=;i<m;i++)
{
ans=0x3f3f3f3f;
scanf("%d%d",&a,&b);
dfs(a,b);
printf("%d\n",ans);
}
}
return ;
}
hdu----(2586)How far away ?(DFS/LCA/RMQ)的更多相关文章
- 迭代器 Iterator 是什么?(未完成)Iterator 怎么使用?(未完成)有什么特点?(未完成)
迭代器 Iterator 是什么?(未完成)Iterator 怎么使用?(未完成)有什么特点?(未完成)
- Crontab中的除号(slash)到底怎么用?(转载)
转载于:https://www.cnblogs.com/cocowool/p/5865397.html crontab 是Linux中配置定时任务的工具,在各种配置中,我们经常会看到除号(Slash) ...
- HDU 1160(两个值的LIS,需dfs输出路径)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1160 FatMouse's Speed Time Limit: 2000/1000 MS (Java/ ...
- 域名绑定和域名解析(DNS)有什么不同?(转载)
域名解析在DNS处设置,DNS服务器将你的域名指向你的存储网页的服务器. 域名绑定在服务器中设置,存储你网页文件的服务器绑定了你的域名才能把浏览者引导到这个域名指定的物理位置来访问. 比如,你进一个高 ...
- 如何分析和提高大型项目(C/C++)的编译速度?(VS2015特有的:/LTCG:incremental选项)
常见的有几个:1. Precompile header2. 多线程编译3. 分布式编译4. 改code,减少依赖性 另外还有一个VS2015特有的:/LTCG:incremental选项.以前为了执行 ...
- 牛客小白月赛13 小A的最短路(lca+RMQ)
链接:https://ac.nowcoder.com/acm/contest/549/F来源:牛客网 题目描述 小A这次来到一个景区去旅游,景区里面有N个景点,景点之间有N-1条路径.小A从当前的一个 ...
- 大视野 1012: [JSOI2008]最大数maxnumber(线段树/ 树状数组/ 单调队列/ 单调栈/ rmq)
1012: [JSOI2008]最大数maxnumber Time Limit: 3 Sec Memory Limit: 162 MBSubmit: 9851 Solved: 4318[Submi ...
- HDU 1847:Good Luck in CET-4 Everybody!(规律?博弈?)
Good Luck in CET-4 Everybody! Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...
- HDU 4063 Aircraft(计算几何)(The 36th ACM/ICPC Asia Regional Fuzhou Site —— Online Contest)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4063 Description You are playing a flying game. In th ...
随机推荐
- [Effective Java]第五章 泛型
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- C# 操作的时候接收用户输入密码进行确认
首先新建一个原始窗体,如下:
- CUBRID学习笔记 34 net参数化查询 cubrid教程示例
using CUBRID.Data.CUBRIDClient; namespace ParameterExample { class Program { static void Main(string ...
- Linux Shell脚本攻略 读书笔记
Linux Shell脚本攻略 读书笔记 这是一本小书,总共253页,但内容却很丰富,书中的示例小巧而实用,对我这样总是在shell门前徘徊的人来说真是如获至宝:最有价值的当属文本处理,对这块我单独整 ...
- win 8 pip install 或者 pycharm 安装 paramiko 报错
这是安装时报错的最后几行 creating build\temp.win-amd64-3.5\Release\build creating build\temp.win-amd64-3.5\Relea ...
- ubuntu下配置hosts
由于Chrome浏览器访问问题,需要配置hosts. 在Ubuntu系统下,需要修改/etc/hosts文件,修改完之后要重启网络.具体过程如下:1.修改hostssudo vi /etc/hosts ...
- Android学习参考2
一名自学成才的Android开发者怒答! 1. Google做开发前完全是小白,真心不知道有Google这东西,只晓得百 度,遇到问题直接百度,不是黑百度,百度在娱乐八卦方面确实靠谱,但是技术方面查出 ...
- T-SQL排名函数
提到排名函数我们首先可能想到的是order by,这个是排序,不是排名,排名需要在前面加个名次序号的,order by是没有这个功能的.还可能会想到identity(1,1),它也给了一个序号,但是不 ...
- Spring的DI(Ioc) - 注入bean 和 基本数据类型
注入bean有两种方式: 注入其他bean: 方式一 <bean id="orderDao" class="cn.itcast.service.OrderDaoBe ...
- Redis基础知识之————空间换时间的查询案例
空间与时间 空间换时间是在数据库中经常出现的术语,简单说就是把查询需要的条件进行索引的存储,然后查询时为O(1)的时间复杂度来快速获取数据,从而达到了使用空间存储来换快速的时间响应!对于redis这个 ...