题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

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

Problem solving report:

Description: 在一个村庄里有n个地方,这n个地方每两个之间都有一条唯一的双向道。进行m次询问,A到B的距离是多少?
Problem solving: 利用vector,把每条路的起点当数组的下标,终点作为数组里面存的内容,然后把权值用另一个数组存一下(定义一个结构体),在深搜里面就把每个数组里面存的终点作为新的起点进入新的递归。直到找到终点。

#include <vector> //动态数组的头文件
#include <stdio.h>
#include <string.h>
#define N 40010
using namespace std;
struct edge {int v, w;
};
int vis[N], temp;
vector <struct edge> a[N]; //定义一个结构体动态二维数组
void DFS(int x, int y, int sum)
{int i;if (x == y){printf("%d\n", sum);temp = 1;return ;}int len = a[x].size(); //返回数组a[x]的元素个数if (!len || vis[x] || temp)return ;for (int i = 0; i < len; i++){vis[x] = 1;DFS(a[x][i].v, y, sum + a[x][i].w);vis[x] = 0;}
}
int main()
{int t, m, n, u, v, w, x, y;scanf("%d", &t);while (t--){scanf("%d%d", &n, &m);memset(vis, 0, sizeof(vis));for (int i = 0; i < n; i++)a[i].clear(); //清空动态数组 for (int i = 1; i < n; i++){scanf("%d%d%d", &u, &v, &w);a[u].push_back((edge){v, w});a[v].push_back((edge){u, w});}for (int i = 0; i < m; i++){temp = 0;scanf("%d%d", &x, &y);DFS(x, y, 0);}}return 0;
}

HDU - How far away ?(DFS+vector)相关推荐

  1. HDU 6386 Age of Moyu DFS+BFS

    /** HDU 6386 Age of Moyu DFS+BFS 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6386题意:1-->n的最小换乘次数 ...

  2. HDU 1426 Sudoku Killer【DFS 数独】

    自从2006年3月10日至11日的首届数独世界锦标赛以后,数独这项游戏越来越受到人们的喜爱和重视.  据说,在2008北京奥运会上,会将数独列为一个单独的项目进行比赛,冠军将有可能获得的一份巨大的奖品 ...

  3. HDU - 4394 Digital Square(数位dfs)

    题目链接:点击查看 题目大意:给出N,求满足%的最小M 题目分析:通过(大佬们的)分析可以得到一个结论: N的个位只由M的个位决定,N十位由M的个位和十位决定,N的百位由M的个位十位百位决定,以此类推 ...

  4. hdu 5468 Puzzled Elena(前缀性质+dfs序+容斥)

    题目链接:hdu 5468 Puzzled Elena 解题思路 预处理出每个数的因子(注意只需要质因子幂数最大为1的数,例如6=21∗316=2^1 * 3^1)然后用一个数组维护,fac[i]表示 ...

  5. hdu 5483 Nux Walpurgis(最小生成树+dfs)

    题目链接:hdu 5483 Nux Walpurgis 解题思路 先求一下最小生成树.然后枚举起点,遍历整棵树,维护树边能被替换的最小权值. 代码 #pragma comment(linker, &q ...

  6. hdu 4358(莫队算法+dfs序列)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4358 解题思路:用dfs求出整棵树的dfs序列,这样以u为根节点的子树就转化到相对应的区间上了.由于是 ...

  7. nyoj-20--吝啬的国度-DFS+vector

    题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=20 #include<stdio.h> #include<string. ...

  8. hdu 6200 mustedge mustedge mustedge(dfs序+树状数组+并查集)

    题目链接:hdu 6200 mustedge mustedge mustedge 题意: 一开始给你一个有n个节点m条无向边的图,现在定义mustedge为u->v的路径上必须经过的边. 现在有 ...

  9. HDU - 6625 three arrays (Trie+dfs)

    题目链接 题意 两个数组A.BA.BA.B,数组的顺序随意,你需要得到一个数组CCC,满足C[i]=A[i]xorB[i]C[i] = A[i] xor B[i]C[i]=A[i]xorB[i]且字典 ...

  10. HDU(1572),最短路,DFS

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1572 很久没写深搜了,有点忘了. #include <iostream> #include ...

最新文章

  1. 数字图像处理——第六章 彩色图像处理
  2. c语言循环控制答案,C语言程序设计 实四 循环控制 答案 《西北民大 电气院》.doc...
  3. sequence.pad_sequences 的用法举例
  4. 面向对象 - 继承性
  5. FPGA加速BCNN,模型20倍剪枝率、边缘设备超5000帧/秒推理吞吐量
  6. mysql索引篇之覆盖索引、联合索引、索引下推
  7. D - Sequence Swapping DP
  8. leetcode(189) 旋转数组
  9. Gartner发布2021年数字商务技术成熟度曲线,重点关注四项技术
  10. Hive 内嵌模式安装指导
  11. apache 添加虚拟机
  12. win7科学计算机不支持,新CPU不支持Win7怎么回事?新一代主板装不了Win7的解决办法...
  13. 修正的判定条件覆盖例题_语句覆盖、判断覆盖、条件覆盖、条件判定组合覆盖、多条件覆盖、修正条件覆盖...
  14. 输入快递单号自动识别快递公司的方法
  15. 计算机组成原理rs rd,计算机组成原理五章.ppt
  16. python手绘效果图_用Python做个海量小姐姐素描图
  17. mysql创建表里主码和外码_外码必须是另一个关系的主码吗?主键主码 外键外码是同一个东西吗?...
  18. 编译器与Debug的传奇:Grace Murray Hopper小传
  19. 小程序云开发(一):新建云开发模板
  20. 在自己的数据集上训练CrowdDet过程记录

热门文章

  1. Android播放音频到耳机,android插入耳机状态使用扬声器外放音乐
  2. 【FlinkX】两个issue分析:reader和writer的通道数不一致+获取JobId
  3. 【JAVA】根据汉字拼音首字母排序 不同类型处理
  4. 今天谁在开网店?兼职卖家占整体网店近70%
  5. 数据库主键、外键、超键、最左前缀原则
  6. 计算字符串占用字节数
  7. FSSR : Frequency Separation for Real-World Super-Resolution
  8. WEB基础之:创建表格
  9. 【antdesign】表单布局和校验
  10. 服务器装系统报0x0000005d,虚拟机不能安装Win10系统,提示your PC needs to restart,错误代码0x0000005D该怎么办-电脑自学网...