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 ...
随机推荐
- linux 命令:crontab
一.crond简介 crond 是linux下用来周期性的执行某种任务或等待处理某些事件的一个守护进程,与windows下的计划任务类似,当安装完成操作系统后,默认会安装此服务 工具,并且会自动启动c ...
- linux命令学习笔记:cut详解
cut命令从文件的每一行剪切字节.字符和字段并将它们写至标准输出.它是以文件的每一行作为处理对象的. 命令格式:cut [选项] [范围] 文件.选项用来指定单位(字节.字符还是字段),范围指定选项的 ...
- android如何查看网卡名和ip
我们知道,在windows下查看ip地址用ipconfig,在Linux下查看ip地址用ifconfig.今天在使用android查看的时候ifconfig却不管用: 查找网上资料发现,原来默认ifc ...
- SoundHound Inc. Programming Contest 2018
A - F Time Limit: 2 sec / Memory Limit: 1024 MB Score : 100100 points Problem Statement You are give ...
- ACM学习历程—Hihocoder 1164 随机斐波那契(数学递推)
时间限制:5000ms 单点时限:1000ms 内存限制:256MB 描述 大家对斐波那契数列想必都很熟悉: a0 = 1, a1 = 1, ai = ai-1 + ai-2,(i > 1). ...
- AndyQsmart ACM学习历程——ZOJ3872 Beauty of Array(递推)
Description Edward has an array A with N integers. He defines the beauty of an array as the summatio ...
- 【Lintcode】074.First Bad Version
题目: The code base version is an integer start from 1 to n. One day, someone committed a bad version ...
- mysql数据库---编码格式基本操作
1.查看数据库编码格式 mysql> show variables like 'character_set_database'; 2.查看数据表的编码格式 mysql> show crea ...
- 面向对象(this关键字)
package com_package2; public class Person3 { private int age; private String name; public int getAge ...
- fastreport整理
Q1:如何直接打印,不显示打印对话框? frxReport1.PrintOptions.ShowDialog := false; frxReport1.PrepareReport(true); frx ...