How far away(DFS+vector存图)
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存图)的更多相关文章
- POJ 1985.Cow Marathon-树的直径-树的直径模板(BFS、DFS(vector存图)、DFS(前向星存图))
Cow Marathon Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 7536 Accepted: 3559 Case ...
- B - Cow Marathon DFS+vector存图
After hearing about the epidemic of obesity in the USA, Farmer John wants his cows to get more exerc ...
- 【模板】Vector存图 + SPFA
最近愉快地决定要由边集数组转向Vector存图,顺便开始图论集训 老惯例,以题写模板 P1339 [USACO09OCT]热浪Heat Wave 题目描述 The good folks in Texa ...
- POJ 1655.Balancing Act-树的重心(DFS) 模板(vector存图)
Balancing Act Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 17497 Accepted: 7398 De ...
- Neither shaken nor stirred(DFS理解+vector存图)
题目链接:http://acm.timus.ru/problem.aspx?space=1&num=2013 题目理解: 给定n个点的有向图: 下面n行,第一个数字表示点权,后面一个数字m表示 ...
- 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 ...
- 存图方式---邻接表&邻接矩阵&前向星
基于vector存图 struct Edge { int u, v, w; Edge(){} Edge(int u, int v, int w):u(u), v(v), w(w){} }; vecto ...
- Safe Path(bfs+一维数组存图)
题目链接:http://codeforces.com/gym/101755/problem/H 题目分析:先bfs一遍怪兽可以到达的点,再bfs人可以走的地方看可不可以到达终点: 很显然读到 2&l ...
- Treasure Island DFS +存图
All of us love treasures, right? That's why young Vasya is heading for a Treasure Island. Treasure I ...
随机推荐
- matlab的一个疑问?
把逻辑值放入一个已知矩阵,为啥结果是:真就取矩阵的值,假就不取值? K>> aaaa=randi(10,10,2) aaaa = 6 3 10 4 6 7 5 2 6 3 8 2 1 2 ...
- HDU 2643 Rank:第二类Stirling数
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2643 题意: 有n个个选手参赛,问排名有多少种情况(可以并列). 题解: 简化问题: 将n个不同的元素 ...
- ThreadPoolExecutor线程池进阶使用
一.简介 线程池类为 java.util.concurrent.ThreadPoolExecutor,常用构造方法为: ThreadPoolExecutor(int corePoolSize, int ...
- L95
The children squealed with delight when they saw the puppy.The signal will be converted into digital ...
- hadoop源码剖析--hdfs安全模式
一.什么是安全模式 hadoop安全模式是name node的一种状态,处于该状态时有种量特性: 1.namenode不接受任何对hfds文件系统的改变操作(即此时整个文件系统处于只读状态): 2.不 ...
- Linux网络编程socket错误分析
socket错误码: EINTR: 阻塞的操作被取消阻塞的调用打断.如设置了发送接收超时,就会遇到这种错误. 只能针对阻塞模式的socket.读,写阻塞的socket时,-1返回,错误号为INTR.另 ...
- android自动连接指定wifi
public class WifiAutoConnectManager { private static final String TAG = WifiAutoConnectManager.class ...
- java---集合类(1)
java.util包中包含了一系列重要的集合类.而对于集合类,主要需要掌握的就是它的内部结构,以及遍历集合的迭代模式. 接口:Collection Collection是最基本的集合接口,一个Coll ...
- android TextView selector点击样式改变
1.selector 从单词的意思来说:选择器,就是对你的目标的控制.selector主要是用在ListView的item单击样式和TextView和Button的点击样式. 2.主要属性介绍: an ...
- POJ-3669
Meteor Shower Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 21055 Accepted: 5499 De ...