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

Problem Description
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
 
Source
 
Recommend
 
用邻接表+dfs比较容易过...
代码:
 #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)的更多相关文章

  1. 迭代器 Iterator 是什么?(未完成)Iterator 怎么使用?(未完成)有什么特点?(未完成)

    迭代器 Iterator 是什么?(未完成)Iterator 怎么使用?(未完成)有什么特点?(未完成)

  2. Crontab中的除号(slash)到底怎么用?(转载)

    转载于:https://www.cnblogs.com/cocowool/p/5865397.html crontab 是Linux中配置定时任务的工具,在各种配置中,我们经常会看到除号(Slash) ...

  3. HDU 1160(两个值的LIS,需dfs输出路径)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1160 FatMouse's Speed Time Limit: 2000/1000 MS (Java/ ...

  4. 域名绑定和域名解析(DNS)有什么不同?(转载)

    域名解析在DNS处设置,DNS服务器将你的域名指向你的存储网页的服务器. 域名绑定在服务器中设置,存储你网页文件的服务器绑定了你的域名才能把浏览者引导到这个域名指定的物理位置来访问. 比如,你进一个高 ...

  5. 如何分析和提高大型项目(C/C++)的编译速度?(VS2015特有的:/LTCG:incremental选项)

    常见的有几个:1. Precompile header2. 多线程编译3. 分布式编译4. 改code,减少依赖性 另外还有一个VS2015特有的:/LTCG:incremental选项.以前为了执行 ...

  6. 牛客小白月赛13 小A的最短路(lca+RMQ)

    链接:https://ac.nowcoder.com/acm/contest/549/F来源:牛客网 题目描述 小A这次来到一个景区去旅游,景区里面有N个景点,景点之间有N-1条路径.小A从当前的一个 ...

  7. 大视野 1012: [JSOI2008]最大数maxnumber(线段树/ 树状数组/ 单调队列/ 单调栈/ rmq)

    1012: [JSOI2008]最大数maxnumber Time Limit: 3 Sec  Memory Limit: 162 MBSubmit: 9851  Solved: 4318[Submi ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. mysql中整数类型后面的数字,是不是指定这个字段的长度?比如int(11),11代表11个字节吗?

    原文地址:    http://www.cnblogs.com/stringzero/p/5707467.html 原先对mysql不太理解,但也没有报错.但理解的不够深入.这次补上. 原来以为int ...

  2. ios 学习线路(图片)(摘录)

    iOS学习路线

  3. hdu 5023 A Corrupt Mayor's Performance Art 线段树

    A Corrupt Mayor's Performance Art Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 100000/100 ...

  4. Codeforces Round #279 (Div. 2) E. Restoring Increasing Sequence 二分

    E. Restoring Increasing Sequence time limit per test 1 second memory limit per test 256 megabytes in ...

  5. git学习笔记06-创建分支合并分支-比svn快多了,因为只有指针在改变

    一开始git只有一条时间线,这个分支叫主分支,即master分支. HEAD严格来说不是指向提交,而是指向master,master才是指向提交的,所以,HEAD指向的就是当前分支. 每次提交,mas ...

  6. 使用BAPI_ACC_DOCUMENT_POST,创建会计凭证,用BADI扩展字段(转)

    业务需求:和银行做一个接口,要通过银行流水产生会计凭证,会计凭证的事务码是F-02,查到了BAPI方法BAPI_ACC_DOCUMENT_POST.昨天测试发现,有一些参数在BAPI_ACC_DOCU ...

  7. [转]ubuntu环境变量配置文件

    参考网址:http://www.bkjia.com/Linuxjc/1008127.html Ubuntu通常使用的几个配置文件主要有下面这几个: /etc/environment./etc/prof ...

  8. [转]瓦的VPS后台kiwivm面板使用+安装AMH+装VPN

    参考网址:http://u-lis.com/archives/4159 ZC:网页图片保存于“百度云 OsSkill --> 全部文件 > 知识__来自网页 > 瓦 > 瓦_面 ...

  9. JavaSE复习_2 对象与类

    △java中的制表符.'\t'制表符."\t"也可以. △方法内不能再定义一个方法,互相平级. △数组中boolean类型的变量默认为false;char默认为'\u0000'(\ ...

  10. valueForKeyPath的妙用(转)

    可能大家对 - (id)valueForKeyPath:(NSString *)keyPath 方法不是很了解. 其实这个方法非常的强大,举个例子: NSArray *array = @[@" ...