How far away ?

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 5766    Accepted Submission(s): 2166

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
 
Source
ECJTU 2009 Spring Contest



AC代码:

#include <iostream>
#include <cstring>
#include <vector>
#include <cstdio> using namespace std; const int M = 4 * 1e4 + 100;
typedef long long ll;
typedef pair<int,int> P;
vector<P>G[M];
int vis[M],isGO,e; void dfs(int x, int cost)
{
if(isGO || vis[x]) return ;
if(x == e)//数据太大,要改成全局变量
{
isGO = 1;
printf("%d\n",cost);
return ;
}
vis[x] = 1;
for(int i = 0; i < G[x].size(); i++)
{
P a = G[x][i];
dfs(a.first,cost + a.second);
}
vis[x] = 0;
} void solve()
{
int x,y,c,n,m;
scanf("%d %d",&n,&m);
for(int i = 1; i < n; i++)
{
scanf("%d %d %d",&x,&y,&c);
G[x].push_back(make_pair(y,c));
G[y].push_back(make_pair(x,c));
}
while(m--)
{
isGO = 0;
scanf("%d %d",&x,&e);
dfs(x,0);
}
for(int i = 1; i <= n; i++) G[i].clear();
} int main()
{
int T,cnt = 0;
scanf("%d",&T);
while(T--)
{
solve();
}
return 0;
}

How far away ?(DFS)的更多相关文章

  1. bzoj 1064 假面舞会 图论??+dfs

    有两种情况需要考虑 1.链:可以发现对最终的k没有影响 2.环:如果是真环(即1->2->3->4->1),可以看出所有可行解一定是该环的因数 假环呢??(1->2-&g ...

  2. HZOJ 20190727 T2 单(树上dp+乱搞?+乱推式子?+dfs?)

    考试T2,考试时想到了40pts解法,即对于求b数组,随便瞎搞一下就oxxk,求a的话,很明显的高斯消元,但考试时不会打+没开double挂成10pts(我真sb),感觉考试策略还是不够成熟,而且感觉 ...

  3. [ZOJ 1008]Gnome Tetravex (dfs搜索 + 小优化)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1008 题目大意:给你n*n的矩阵,每个格子里有4个三角形,分别是 ...

  4. DFS例题

    特殊的质数肋骨(递归)] -题目描述-农民约翰的母牛总是生产出最好的肋骨.你能通过农民约翰和美国农业部标记在每根肋骨上的数字认出它们. 农民约翰确定他卖给买方的是真正的质数肋骨,是因为从右边开始切下肋 ...

  5. BZOJ5338[TJOI2018]xor——主席树+dfs序

    题目描述 现在有一颗以1为根节点的由n个节点组成的树,树上每个节点上都有一个权值vi. 现在有Q 次操作,操作如下: 1  x y    查询节点x的子树中与y异或结果的最大值 2 x y z     ...

  6. HDU 6203 ping ping ping(贪心+LCA+DFS序+BIT)

    ping ping ping Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  7. bzoj 4765 普通计算姬 dfs序 + 分块

    题目链接 Description "奋战三星期,造台计算机".小G响应号召,花了三小时造了台普通计算姬.普通计算姬比普通计算机要厉害一些.普通计算机能计算数列区间和,而普通计算姬能 ...

  8. luogu 1113 杂务--啥?最长路?抱歉,我不会

    P1113 杂务 题目描述 John的农场在给奶牛挤奶前有很多杂务要完成,每一项杂务都需要一定的时间来完成它.比如:他们要将奶牛集合起来,将他们赶进牛棚,为奶牛清洗乳房以及一些其它工作.尽早将所有杂务 ...

  9. [codevs 1961]躲避大龙(dfs)

    题目:http://dev.codevs.cn/problem/1961/ 分析: 被“SPFA”的标签骗了…… 看了hzwer的博客才知道可以用f[i][0..60]表示每个点每个秒是否可以到.至于 ...

随机推荐

  1. python网络编程-多进程multiprocessing

    一:mutilprocess简介 多线程类似于同时执行多个不同程序,多线程运行有如下优点: 使用线程可以把占据长时间的程序中的任务放到后台去处理. 用户界面可以更加吸引人,这样比如用户点击了一个按钮去 ...

  2. 浅谈js设计模式之单例模式

    单例模式的定义是:保证一个类仅有一个实例,并提供一个访问它的全局访问点. 单例模式是一种常用的模式,有一些对象我们往往只需要一个,比如线程池.全局缓存.浏览器中的 window 对象等.在 JavaS ...

  3. MySQL缓存命中率概述及如何提高缓存命中率

    MySQL缓存命中率概述 工作原理: 查询缓存的工作原理,基本上可以概括为: 缓存SELECT操作或预处理查询(注释:5.1.17开始支持)的结果集和SQL语句: 新的SELECT语句或预处理查询语句 ...

  4. Codeforces 552C Vanya and Scales(进制转换+思维)

    题目链接:http://codeforces.com/problemset/problem/552/C 题目大意:有101个砝码重量为w^0,w^1,....,w^100和一个重量为m的物体,问能否在 ...

  5. Effective STL 学习笔记 Item 16:vector, string & C API

    有时需要支持 C 的接口,但这并不复杂. 对于 vector 来讲, \(v[0]\) 的地址 \(\&v[0]\) 即可作为数组指针传递给 C API: 1: // Legacy C API ...

  6. Loadrunner11在win7下录制脚本,ie打不开

    Loadrunner11在win7下录制脚本,ie打不开 使用loadrunner11录制脚本时试了很多办法都无法打开ie浏览器,最后终于解决了 1.ie浏览器去掉启用第三方浏览器扩展 2.loadr ...

  7. 8-11 Add All uva 10954

    有n n小于等于五千 个数的集合s  每次可以从s中删除两个数 然后把他们的和放回集合 直到剩下一个数 每次操作的开销等于删除两个数二之和  求最小总开销 思路:就是每次取最小的两个数即可 用优先级队 ...

  8. JAVAEE——宜立方商城06:Redis安装、数据类型和持久化方案、Redis集群分析与搭建、实现缓存和同步

    1. 学习计划 1.首页轮播图展示 2.Redis服务器搭建 3.向业务逻辑中添加缓存. 4.使用redis做缓存 5.缓存同步. 2. 首页轮播图动态展示 2.1. 功能分析 根据分类id查询内容列 ...

  9. 【SQL】177. Nth Highest Salary

    Write a SQL query to get the nth highest salary from the Employee table. +----+--------+ | Id | Sala ...

  10. 常见的Javascript报错及解决方案

    一.堆栈溢出不顾堆栈中分配的局部数据块大小,向该数据块写入了过多的数据,导致数据越界,以至于覆盖了别的数据.1.哪些操作会引起堆栈溢出?比如递归2.如何解决堆栈溢出?闭包,setTimeout,优化调 ...