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. group by 并且 count(1)的linq写法

    SELECT [MobleNo],count(1) FROM [CustMobleNo] group by [MobleNo] GO ===作用等于=== var rst = from c in da ...

  2. HDU 3861 The King’s Problem(强连通分量+最小路径覆盖)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3861 题目大意: 在csdn王国里面, 国王有一个新的问题. 这里有N个城市M条单行路,为了让他的王国 ...

  3. Windows开机自动启动pageant,方便使用ssh链接到GitHub

    按win +r,输入 shell:startup "C:\Program Files\TortoiseGit\bin\pageant.exe" "d:\GitHubPri ...

  4. 在Kubernetes上使用Traefik

    本节内容: Traefik介绍 部署测试用的两个服务 Role Based Access Control configuration (Kubernetes 1.6+ only) 部署Traefik ...

  5. Ansible playbook基础组件介绍

    本节内容: ansible playbook介绍 ansible playbook基础组件 playbook中使用变量 一.ansible playbook介绍 playbook是由一个或多个“pla ...

  6. JavaScript 三个常用对话框

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  7. 【BZOJ】1061: [Noi2008]志愿者招募

    题解 可能是世界上最裸的一个单纯形 (话说全幺模矩阵是啥我到现在都不知道) 假装我们已经看过了算导,或者xxx的论文,知道了单纯形是怎么实现的 扔一个blog走掉..https://www.cnblo ...

  8. Mysql安装(绿色版安装)

    一:下载 1.官网 https://dev.mysql.com/ 2.下载 3.下载 二:安装 1.将官网上下载的包进行解压 2.以管理员的身份运行DOS安装 进入mysql的bin目录 运行mysq ...

  9. nginx配置web服务器

    一:设置虚拟服务器 1.设置 http { server { listen 127.0.0.1:8080; server_name example.org www.example.org; } } 2 ...

  10. 006 ajax验证用户名

    1.大纲 2.index.jsp <%@ page language="java" contentType="text/html; charset=UTF-8&qu ...