poj1986带权lca
lca求距离,带权值 的树上求lca,我是用倍增法求的,求两点之间的距离转化为到根节点之间的距离
(de了一个小时 的bug,重打居然就过了。。。。)
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<cstdio>
#include<cassert>
#include<iomanip>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define C 0.5772156649
#define pi acos(-1.0)
#define ll long long
#define mod 1000000007
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
#pragma comment(linker, "/STACK:1024000000,1024000000") using namespace std; const double g=10.0,eps=1e-;
const int N=+,maxn=+,inf=0x3f3f3f; struct edge{
int to,Next,w;
}e[N];
int head[N],dis[N];
int cnt,depth[N],father[][N];
void add(int u,int v,int w)
{
e[cnt].to=v;
e[cnt].w=w;
e[cnt].Next=head[u];
head[u]=cnt++;
}
void dfs(int u,int f)
{
father[][u]=f;
for(int i=head[u];~i;i=e[i].Next)
{
int To=e[i].to;
if(To!=f)
{
depth[To]=depth[u]+;
dis[To]=dis[u]+e[i].w;
dfs(To,u);
}
}
}
void init(int n)
{
depth[]=;
dfs(,-);
for(int i=;i<;i++)
for(int j=;j<=n;j++)
father[i][j]=father[i-][father[i-][j]];
}
int lca(int x,int y)
{
if(depth[x]>depth[y])swap(x,y);
for(int i=;i<;i++)
if((depth[y]-depth[x])>>i&)
y=father[i][y];
if(x==y)return x;
for(int i=;i>=;i--)
{
if(father[i][x]!=father[i][y])
{
x=father[i][x];
y=father[i][y];
}
}
return father[][x];
}
int main()
{
ios::sync_with_stdio(false);
cin.tie();
int n,m;
cin>>n>>m;
cnt=;
memset(head,-,sizeof head);
for(int i=;i<m;i++)
{
int a,b,c;
string s;
cin>>a>>b>>c>>s;
add(a,b,c);
add(b,a,c);
}
init(n);
int k;
cin>>k;
while(k--){
int a,b;
cin>>a>>b;
int p=lca(a,b);
cout<<dis[a]+dis[b]-*dis[p]<<endl;
}
return ;
}
/********************
7 6
1 6 13 E
6 3 9 E
3 5 7 S
4 1 3 N
2 4 20 W
4 7 2 S
5
1 6
6 1
1 4
4 1
2 6
********************/
poj1986带权lca的更多相关文章
- Codevs 3287 货车运输 2013年NOIP全国联赛提高组(带权LCA+并查集+最大生成树)
3287 货车运输 2013年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 传送门 题目描述 Description A 国有 n 座 ...
- hdu 2874 Connections between cities 带权lca判是否联通
Connections between cities Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (J ...
- poj 1986 Distance Queries 带权lca 模版题
Distance Queries Description Farmer John's cows refused to run in his marathon since he chose a pa ...
- hdu 2586 How far away ? 带权lca
How far away ? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) P ...
- 带权值的LCA
例题:http://poj.org/problem?id=1986 POJ1986 Distance Queries Language: Default Distance Queries Time L ...
- CF :K 一个含n条边的带权无向连通图,q次查询,每次查询两点间的最短距离。
题意:给你一个含n条边的带权无向连通图,q次查询,每次查询两点间的最短距离. 思路:LCA+思维. 设a,b两点间的距离为f(a,b) 则f(a,b)=dis[a]+dis[b]-2*dis[lca( ...
- hdu 2583 How far away ? 离线算法 带权求最近距离
How far away ? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- 51nod1459(带权值的dijkstra)
题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1459 题意:中文题诶- 思路:带权值的最短路,这道题数据也没 ...
- POJ 1703 Find them, Catch them(带权并查集)
传送门 Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 42463 Accep ...
随机推荐
- Web 编程中编码问题
1. 常见字符编码 iso-8859-1(不支持中文) gbk(国标码) utf-8 (万国码, 支持全世界的编码) 2. 响应编码 当使用 response.getWriter() 来向客户端发送字 ...
- Python基础教程-List和Tuple
List Python内置的一种数据类型是列表:list.list是一种有序的集合,可以随时添加和删除其中的元素. 比如: >>> classmates = ['Michael',' ...
- jquery关于select框的取值和赋值
jQuery("#select_id").change(function(){}); // 1.为Select添加事件,当选择其中一项时触发 var checkValue ...
- pycharm断点调试
step over 执行下一步 蓝色高亮的那一行表示准备执行的代码
- sqlserver导入excel的电话号码(身份证)变为科学计数解决方式
如果excel中有一列存的是手机号码或者身份证号码,那么导入到sql中时,会把手机或者身份证当作数字格式对待,因而会以科学记数法的形式存在sqlserver表中,解决方式,先将excel文件另存为文本 ...
- LeetCode:二叉搜索树中第K小的数【230】
LeetCode:二叉搜索树中第K小的数[230] 题目描述 给定一个二叉搜索树,编写一个函数 kthSmallest 来查找其中第 k 个最小的元素. 说明:你可以假设 k 总是有效的,1 ≤ k ...
- AFNetworking 3.0 解决加密后请求参数是字符串问题
把整个请求参数的json加密生成一个字符串传给服务器,错误提示:[NSJSONSerialization dataWithJSONObject:options:error:]: Invalid top ...
- 一年java程序员的感悟
前沿 在小公司干了差不多一年,刚进来与一个中级程序员做交接,过了大概一个月,那个中级程序员走了,从此,走上了"泥泞"的道路(独立开发),熟悉了公司的项目和业务用了一个月左右,公司当 ...
- 20145201 实验三 敏捷开发与XP实践
实验内容 XP基础 XP核心实践 相关工具 实验步骤 (一)敏捷开发与XP 软件工程是把系统的.有序的.可量化的方法应用到软件的开发.运营和维护上的过程.软件工程包括下列领域:软件需求分析.软件设计. ...
- 解决Vim插入模式下backspace按键无法删除字符的问题【转】
本文转载自:https://blog.csdn.net/zxy987872674/article/details/64124959 最近使用某个服务器编辑文件时,快捷键i进入插入模式后,下方不出现in ...