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 ...
随机推荐
- Spring笔记07(Spring AOP的通知advice和顾问advisor)
1.Spring AOP的通知advice 01.接口代码: package cn.pb.dao; public interface UserDao { //主业务 String add(); //主 ...
- 【LeetCode】047. Permutations II
题目: Given a collection of numbers that might contain duplicates, return all possible unique permutat ...
- BZOJ3772:精神污染
浅谈主席树:https://www.cnblogs.com/AKMer/p/9956734.html 题目传送门:https://www.lydsy.com/JudgeOnline/problem.p ...
- WaitHandle.WaitAll 方法在WPF工程中的应用
因为WaiAll需要多线程支持, 而WPF是STA模式, 可以通过以下方式实现WaitAll ManualResetEvent[] events: foreach (ManualResetEvent ...
- TTS API 使用
#define SPCAT_VOICES L"HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Speech\\Voices" ...
- μC/OS-II与RT-Thread对比—任务调度
在任务调度器的实现上,μC/OS-II和RT-Thread都采用了位图调度(bitmap scheduling),任务优先级的值越小则代表具有越高的优先级,主要区别在于实现形式,是采用多级队列的形式, ...
- MVN 命令行
Maven依赖查询: http://mvnrepository.com/ Maven常用命令: 1. 创建Maven的普通java项目: mvn archetype:create - ...
- 27.集成EFCore配置Client和API
copy链接字符串,这是一个官方的字符串,直接复制过来,放在上面. 添加包的引用 引入IdentityServer4.EntityFramework的命名空间 主要是这个地方初始化我们的数据库 Ope ...
- 过滤asp.net页面每次发出请求之前访问
public class PageFiltert : System.Web.UI.Page { public PageFiltert() { // //TODO: 在此处添加构造函数逻辑 // } p ...
- MySQL学习中,遇到的问题记录
一.安装mysql时,报错:1045 原因:因为之前安装过mysql,之后再安装会出现这个问题. 解决: 删除之前的残留就好了 方法:清除mysql之前的历史残留