HDU:2586-How far away
How far away
Time limit1000 ms
Memory limit32768 kB
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
解题心得:
- 题意就是给你一个双向图,每两点之间只有一条路径(其实就是一颗树),任意问你两点,要求输出两点间的最小距离。两种做法:
- 第一种就是直接跑暴力的dfs,复杂度也就是n*m最大8e6,很简单就跑过了。
- 第二种就是用LCA,两点之间最短的距离就是两点经过最近公共祖先形成的一条路。具体做法可以先预处理一下每个点到根节点的距离,存在数组dis里面,假如询问a,b之间的最小距离,就可以先得出最近公共祖先c然后答案就是dis[a] + dis[b] - 2*dis[c]。
- 然后就是双向边怎么建成一棵树的问题,可以用邻接表存图,存双向边,在使用的时候任意选一个点为根节点,使用过的点就标记上,这样就可以形成一棵树,因为每两点之间只选择一个方向的边。
dfs跑暴力:
#include<stdio.h>
#include<cstring>
#include<vector>
using namespace std;
const int maxn = 4e4+100;
bool vis[maxn],flag;
int n,m,ans,a,b;
vector <pair<int,int> > ve[maxn];
void init()
{
memset(vis,0,sizeof(vis));
scanf("%d%d",&n,&m);
for(int i=0;i<=n+1;i++)
ve[i].clear();
for(int i=1;i<n;i++)
{
int a,b,len;
scanf("%d%d%d",&a,&b,&len);
//邻接表存图
ve[a].push_back(make_pair(b,len));
ve[b].push_back(make_pair(a,len));
}
}
void dfs(int pos,int sum_len)
{
if(vis[pos] || flag)
return ;
vis[pos] = true;
if(pos == b)
{
printf("%d\n",sum_len);
flag = true;
return;
}
for(int i=0;i<ve[pos].size();i++)
{
pair<int,int> p;
p = ve[pos][i];
dfs(p.first,sum_len + p.second);
}
}
void solve()
{
while(m--)
{
flag = false;
memset(vis,0,sizeof(vis));
scanf("%d%d",&a,&b);
dfs(a,0);
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
init();
solve();
}
}
LCA(tarjan写法)
#include<bits/stdc++.h>
using namespace std;
const int maxn = 4e4+100;
vector <pair<int,int> > ve[maxn];
int vis[maxn],n,father[maxn],sum[maxn],m,a,b;
bool flag;
void init()
{
memset(father,0,sizeof(father));
memset(vis,0,sizeof(vis));
memset(sum,0,sizeof(sum));
for(int i=0; i<=n; i++)
ve[i].clear();
for(int i=1; i<n; i++)
{
int a,b,len;
scanf("%d%d%d",&a,&b,&len);
ve[a].push_back(make_pair(b,len));
ve[b].push_back(make_pair(a,len));
}
}
void dfs(int pos,int len)
{
vis[pos] = true;
sum[pos] = len;
for(int i=0; i<ve[pos].size(); i++)
{
int v = ve[pos][i].first;
if(!vis[v])
dfs(v,len+ve[pos][i].second);
}
return ;
}
int find(int x)
{
if(x == father[x])
return x;
return father[x] = find(father[x]);
}
void tarjan(int pos)//跑tarjan
{
vis[pos] = true;
father[pos] = pos;
if(flag)
return;
for(int i=0; i<ve[pos].size(); i++)
{
int v = ve[pos][i].first;
if(!vis[v])
{
tarjan(v);
father[v] = pos;
}
if(flag)
return ;
}
if(pos == a || pos == b)
{
if(pos != a)
swap(a,b);
if(father[b])
{
int ans = find(father[b]);
printf("%d\n",abs(sum[a]+sum[b]-sum[ans]*2));
flag = true;
}
}
}
void query()
{
while(m--)
{
flag = false;
memset(father,0,sizeof(father));
memset(vis,0,sizeof(vis));
scanf("%d%d",&a,&b);
tarjan(1);
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
init();
dfs(1,0);//先dfs预处理
query();
}
}
HDU:2586-How far away的更多相关文章
- HDU:过山车(二分图最大匹配)
http://acm.hdu.edu.cn/showproblem.php?pid=2063 题意:有m个男,n个女,和 k 条边,求有多少对男女可以搭配. 思路:裸的二分图最大匹配,匈牙利算法. 枚 ...
- HDU:Gauss Fibonacci(矩阵快速幂+二分)
http://acm.hdu.edu.cn/showproblem.php?pid=1588 Problem Description Without expecting, Angel replied ...
- HDU:2846-Repository
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2846 Repository Time Limit: 2000/1000 MS (Java/Others) ...
- HDU:1251-统计难题(字典树模板,动态建树,静态建树)
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1251 统计难题 Time Limit: 4000/2000 MS (Java/Others) Memor ...
- HDU:2767-Proving Equivalences(添边形成连通图)
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2767 Proving Equivalences Time Limit: 4000/2000 MS (Ja ...
- HDU:2255-奔小康赚大钱(KM算法模板)
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2255 奔小康赚大钱 Time Limit: 1000/1000 MS (Java/Others) Mem ...
- AC自动机(Aho-Corasick automation)模板 HDU:2222
#include <iostream> #include <cstdio> #include <cstring> #include <queue> us ...
- HDU ACM 2586 How far away ?LCA->并查集+Tarjan(离线)算法
题意:一个村子有n个房子,他们用n-1条路连接起来,每两个房子之间的距离为w.有m次询问,每次询问房子a,b之间的距离是多少. 分析:近期公共祖先问题,建一棵树,求出每一点i到树根的距离d[i],每次 ...
- hdu:2036.改革春风吹满地
Problem Description “ 改革春风吹满地,不会AC没关系;实在不行回老家,还有一亩三分地.谢谢!(乐队奏乐)” 话说部分学生心态极好,每天就知道游戏,这次考试如此简单的题目,也是云里 ...
- hdu:2030.汉字统计
Problem Description 统计给定文本文件中汉字的个数. Input 输入文件首先包含一个整数n,表示测试实例的个数,然后是n段文本. Output 对于每一段文本,输出其中的汉 ...
随机推荐
- 批量插入,update
#####setting 1create table t as select * from all_objects where 1 =2; ###.模拟逐行提交的情况,注意观察执行时间DECLAREB ...
- 1137 - Sin your life sin公式 + 枚举
http://www.ifrog.cc/acm/problem/1137 和差化积公式, 变成2 * sin((x + y) / 2) * cos((x - y) / 2) + sin(n - (x ...
- Spark Mllib里的Mllib基本数据类型(图文详解)
不多说,直接上干货! Spark Mllib基本数据类型,根据不同的作用和应用场景,分为四种不同的类型 1.Local vector : 本地向量集,主要向spark提供一组可进行操作的数据集合 2 ...
- Tensorflow版Faster RCNN源码解析(TFFRCNN) (2)推断(测试)过程不使用RPN时代码运行流程
本blog为github上CharlesShang/TFFRCNN版源码解析系列代码笔记第二篇 推断(测试)过程不使用RPN时代码运行流程 作者:Jiang Wu 原文见:https://hom ...
- asp.net MVC 4.0 View回顾——布局页与分部页
asp.net MVC 4.0中总结 视图里加载部分视图几种方法 @RenderPage() 但它不能使用 原来视图的 Model 和 ViewData ,只能通过参数来传递. @RenderPage ...
- 动态代理案例1:运用Proxy动态代理来增强方法
动态代理案例1: /*要求:运用Proxy动态代理来增强方法 题目: 1.定义接口Fruit,其中有addFruit方法 2.定义实现类FruitImpl,实现Fruit接口 3.定 ...
- gitlab api批量操作 批量添加用户
import os,time import requests,json # def downloadFile(name, url): # headers = {'Proxy-Connection': ...
- C# 使用解析json 嵌套方法
C#从网页不传参数 接收json数据 public String GetHtmlFromUrl(String url) { //Response.Write(url); //Response.End( ...
- Android商城开发系列(一)——开篇
最近在看尚硅谷的硅谷商城视频,想系统学习一下Android的商城开发流程,打算跟着视频的一步步做出一个商城,然后写博客总结记录一下整个商城的开发过程以及使用到的技术知识点,这个商城的最终效果如下图所示 ...
- 用Jersey为Android客户端开发Restful Web Service
平时在做Android客户端的时候经常要与服务器之间通信,客户端通过服务端提供的接口获取数据,然后再展示在客户端的界面上,作为Android开发者,我们平时更多的是关注客户端的开发,而对服务端开发的关 ...