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 ...
随机推荐
- <iframe>标签的一些说明
<iframe>标签里的marginwidth/marginheight属性定义的是框架内部的margin(框架和其父元素之间的margin可以用style="margin:.. ...
- jquery相对选择器,又叫context选择器,上下文选择器;find()与children()区别
jquery相对选择器有两个参数,jQuery函数的第二个参数可以指定DOM元素的搜索范围(即以第二个参数指定的内容为容器查找指定元素). 第二个参数的不同的类型,对应的用法如下表所示. 类型 用法 ...
- Mysql ERROR 1064 (42000)
创建了一个表: +-------------+--------------+------+-----+---------+----------------+ | Field | Type | Null ...
- HTTP消息中header头部信息的讲解
HTTP Request的Header信息 1.HTTP请求方式 如下表: GET 向Web服务器请求一个文件 POST 向Web服务器发送数据让Web服务器进行处理 PUT 向Web服务器发送数据并 ...
- 清除SVN获取文件的所有.svn文件夹
Windows Registry Editor Version 5.00[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Folder\shell\清除SVN信息] @=&qu ...
- TCP的流量控制和拥塞控制
1.TCP的流量控制 一般说来,我们总是希望数据传输的更快一些.但如果发送方吧数据发送的过快,接收方就可能来不及接收,就会造成数据的丢失.所谓的流量控制就是让发送方的发送速率不要太快,要让接收方来的及 ...
- CALayer总结(一)
1.geometryFlipped 设置为yes,则子图层或者子视图本来相对于左上角放置 改为 相对于左下角放置: 2.contents 3.contentGravity: kCAGravityC ...
- 使用Jmeter进行简单的http接口测试
1.添加线程组:在“测试计划”上点击鼠标右键-->添加-->threads(Users)-->线程组,添加测试场景设置组件,接口测试中一般设置为1个“线程数”,根据测试数据的个数设定 ...
- C# 线程(五):线程池
From : http://kb.cnblogs.com/page/42531/ 在多线程的程序中,经常会出现两种情况: 一种情况: 应用程序中,线程把大部分的时间花费在等待状态,等待某个事件发生,然 ...
- 如何让div上下左右都居中
在做登陆页面的话,需要login的div 上下左右都居中. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" ...