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

题解:建立一个双向图,然后去深搜即可,注意存图要用vector并且每次用完注意清空vector存的内容

代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<vector> using namespace std; struct node {
int pos;
int val;
} temp,q; vector<struct node>a[40005]; int n,m,flag,e,vis[40005]; void DFS(int s,int ans) {
int size,i;
if(vis[s])
return ;
if(flag)
return ;
if(s==e) {
printf("%d\n",ans);
flag=1;
return;
}
if(a[s].empty()) return ;
else {
vis[s]=1;
size=a[s].size();
for(i=0; i<size; i++)
DFS(a[s][i].pos,ans+a[s][i].val);
vis[s]=0;
}
}
int main() {
int cas;
scanf("%d",&cas);
while(cas--) {
int i,j,x,y,z;
scanf("%d %d",&n,&m);
for(i=0; i<n-1; i++) {
scanf("%d %d %d",&x,&y,&z);
//建双向图
q.pos=y;
q.val=z;
a[x].push_back(q);
q.pos=x;
q.val=z;
a[y].push_back(q); }
for(j=0; j<m; j++) {
memset(vis,0,sizeof(vis));
flag=0;
int s;
scanf("%d %d",&s,&e);
DFS(s,0);
}
//用完记得清空
for(i=0; i<n; i++) {
a[i].clear();
}
}
return 0;
}

How far away(DFS+vector存图)的更多相关文章

  1. POJ 1985.Cow Marathon-树的直径-树的直径模板(BFS、DFS(vector存图)、DFS(前向星存图))

    Cow Marathon Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 7536   Accepted: 3559 Case ...

  2. B - Cow Marathon DFS+vector存图

    After hearing about the epidemic of obesity in the USA, Farmer John wants his cows to get more exerc ...

  3. 【模板】Vector存图 + SPFA

    最近愉快地决定要由边集数组转向Vector存图,顺便开始图论集训 老惯例,以题写模板 P1339 [USACO09OCT]热浪Heat Wave 题目描述 The good folks in Texa ...

  4. POJ 1655.Balancing Act-树的重心(DFS) 模板(vector存图)

    Balancing Act Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 17497   Accepted: 7398 De ...

  5. Neither shaken nor stirred(DFS理解+vector存图)

    题目链接:http://acm.timus.ru/problem.aspx?space=1&num=2013 题目理解: 给定n个点的有向图: 下面n行,第一个数字表示点权,后面一个数字m表示 ...

  6. Codeforce-1106-D. Lunar New Year and a Wander(DFS遍历+vector存图+set)

    Lunar New Year is approaching, and Bob decides to take a wander in a nearby park. The park can be re ...

  7. 存图方式---邻接表&邻接矩阵&前向星

    基于vector存图 struct Edge { int u, v, w; Edge(){} Edge(int u, int v, int w):u(u), v(v), w(w){} }; vecto ...

  8. Safe Path(bfs+一维数组存图)

    题目链接:http://codeforces.com/gym/101755/problem/H 题目分析:先bfs一遍怪兽可以到达的点,再bfs人可以走的地方看可不可以到达终点: 很显然读到  2&l ...

  9. Treasure Island DFS +存图

    All of us love treasures, right? That's why young Vasya is heading for a Treasure Island. Treasure I ...

随机推荐

  1. BZOJ 1614 [Usaco2007 Jan]Telephone Lines架设电话线:spfa + 二分【路径中最大边长最小】

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1614 题意: 给你一个无向图,n个点,m条边. 你需要找出一条从1到n的路径,使得这条路径 ...

  2. BZOJ 1208 [HNOI2004]宠物收养所:Splay(伸展树)

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1208 题意: 有一个宠物收养所,在接下来一段时间内会陆续有一些宠物进到店里,或是一些人来领 ...

  3. LoadRunner监控图表与配置(一) 监控与图表

    1.“Monitoer”菜单-“Online Graphs”-“Open a new graph”打开监视图表列表. 实际上这些监视图表已经在Available Graphs中显示了出来. 2.常用监 ...

  4. ES 相似度算法设置(续)

    Tuning BM25 One of the nice features of BM25 is that, unlike TF/IDF, it has two parameters that allo ...

  5. BZOJ_3159_决战

    题目链接 分析: 我使用树剖+splay维护这个东西. 对每条重链维护一棵splay,链加和查询正常做,剩下的链反转如下. 由于一定是深度递增的一条链,我们树剖将它分成从左到右log个区间,提取出对应 ...

  6. poj1637 Sightseeing tour[最大流+欧拉回路]

    混合图的欧拉回路定向问题. 顺便瞎说几句,有向图定欧拉回路的充要条件是每个点入度等于出度,并且图联通.无向图的话只要联通无奇点即可. 欧拉路径的确定应该是无向图联通且奇点数0个或2个,有向图忘了,好像 ...

  7. bzoj 2216: Lightning Conductor 单调队列优化dp

    题目大意 已知一个长度为\(n\)的序列\(a_1,a_2,...,a_n\)对于每个\(1\leq i\leq n\),找到最小的非负整数\(p\)满足: 对于任意的\(j\), \(a_j \le ...

  8. 【Lintcode】 035.Reverse Linked List

    题目: Reverse a linked list. Example For linked list 1->2->3, the reversed linked list is 3-> ...

  9. poj2411铺砖——状压DP

    题目:http://poj.org/problem?id=2411 状态压缩,一行的状态记为一个二进制数,从上往下逐行DP,答案输出最后一行填0的方案数. 代码如下: #include<iost ...

  10. Nuget-QRCode:QRCoder

    ylbtech-Nuget-QRCode:QRCoder 1.返回顶部   2.返回顶部   3.返回顶部   4.返回顶部   5.返回顶部 0. https://github.com/codebu ...