How far away ?(DFS)
How far away ?
Total Submission(s): 5766 Accepted Submission(s): 2166
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.
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.
2
3 2
1 2 10
3 1 15
1 2
2 3 2 2
1 2 100
1 2
2 1
10
25
100
100
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)的更多相关文章
- bzoj 1064 假面舞会 图论??+dfs
有两种情况需要考虑 1.链:可以发现对最终的k没有影响 2.环:如果是真环(即1->2->3->4->1),可以看出所有可行解一定是该环的因数 假环呢??(1->2-&g ...
- HZOJ 20190727 T2 单(树上dp+乱搞?+乱推式子?+dfs?)
考试T2,考试时想到了40pts解法,即对于求b数组,随便瞎搞一下就oxxk,求a的话,很明显的高斯消元,但考试时不会打+没开double挂成10pts(我真sb),感觉考试策略还是不够成熟,而且感觉 ...
- [ZOJ 1008]Gnome Tetravex (dfs搜索 + 小优化)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1008 题目大意:给你n*n的矩阵,每个格子里有4个三角形,分别是 ...
- DFS例题
特殊的质数肋骨(递归)] -题目描述-农民约翰的母牛总是生产出最好的肋骨.你能通过农民约翰和美国农业部标记在每根肋骨上的数字认出它们. 农民约翰确定他卖给买方的是真正的质数肋骨,是因为从右边开始切下肋 ...
- BZOJ5338[TJOI2018]xor——主席树+dfs序
题目描述 现在有一颗以1为根节点的由n个节点组成的树,树上每个节点上都有一个权值vi. 现在有Q 次操作,操作如下: 1 x y 查询节点x的子树中与y异或结果的最大值 2 x y z ...
- 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 ...
- bzoj 4765 普通计算姬 dfs序 + 分块
题目链接 Description "奋战三星期,造台计算机".小G响应号召,花了三小时造了台普通计算姬.普通计算姬比普通计算机要厉害一些.普通计算机能计算数列区间和,而普通计算姬能 ...
- luogu 1113 杂务--啥?最长路?抱歉,我不会
P1113 杂务 题目描述 John的农场在给奶牛挤奶前有很多杂务要完成,每一项杂务都需要一定的时间来完成它.比如:他们要将奶牛集合起来,将他们赶进牛棚,为奶牛清洗乳房以及一些其它工作.尽早将所有杂务 ...
- [codevs 1961]躲避大龙(dfs)
题目:http://dev.codevs.cn/problem/1961/ 分析: 被“SPFA”的标签骗了…… 看了hzwer的博客才知道可以用f[i][0..60]表示每个点每个秒是否可以到.至于 ...
随机推荐
- Javascript之对象的创建
面向对象语言有一个非常显著的标志,那就是它们都有类的概念,通过类之间的继承就可以达到任意创建具有相同属性方法的对象.而在ECMAScript中并没有类的概念,它把对象定义为:无序属性的集合,其属性包含 ...
- 通过field:global给子元素添加css样式
{dede:arclist row=5 typeid=200} <li [field:global runphp=’yes’ name=autoindex](@me==1)?@me=”class ...
- 编译环境搭建:Makefile
前言 长久以来,笔者一直想用一种管理工具,将所编写的测试程序.算法代码以及工程代码统一管理起来.因为有些是用Java写的有些是用C++写的.虽有想法,但却无行动.这又让我想起了昨天晚上看到一部电影里所 ...
- Centos7.3安装vsftp服务
我们需要向centos操作系统的服务器上上传文件或者下载文件,这时候,ftp有必要安装下, 我们选择主流的vsftp: 第一步:安装vsftp yum install -y vsftpd 第二步:设置 ...
- » Working Around JNI UTF-8 Strings Deprogramming
private static native void printString(String text); ... void examplePrintString() { String str = &q ...
- Spring整合JDBC以及AOP管理事务
本节内容: Spring整合JDBC Spring中的AOP管理事务 一.Spring整合JDBC Spring框架永远是一个容器,Spring整合JDBC其实就是Spring提供了一个对象,这个对象 ...
- 《精通Python设计模式》学习结构型之外观模式
这个我在工作中也有所应用的. 就是在真正的实现层上面,再封装一个函数的调用的. 这样就可以在内层函数作真正实现, 而外层调用函数对外开放, 隔离内外的变化性. from enum import Enu ...
- Mongodb C#客户端数据关联数据,使用Linq语法进行关联
在Mongodb C# drivers 文档 官方地址:https://docs.mongodb.com/ecosystem/drivers/csharp/ 基础的使用请参考<c# Mongod ...
- ionic启动App时不使用淡入淡出效果
找到我们项目下面 res下面的config.xml,然后加入下面两句就可以解决. <preference name="FadeSplashScreen" value=&quo ...
- CSUOJ 1341 String and Arrays
Description 有一个N*N的字符矩阵,从上到下依次记为第1行,第2行,--,第N行,从左至右依次记为第1列,第2列,--,第N列. 对于这个矩阵会进行一系列操作,但这些操作只有两类: ...