luogu P2912 [USACO08OCT]牧场散步Pasture Walking
题目描述
The N cows (2 <= N <= 1,000) conveniently numbered 1..N are grazing among the N pastures also conveniently numbered 1..N. Most conveniently of all, cow i is grazing in pasture i.
Some pairs of pastures are connected by one of N-1 bidirectional walkways that the cows can traverse. Walkway i connects pastures A_i and B_i (1 <= A_i <= N; 1 <= B_i <= N) and has a length of L_i (1 <= L_i <= 10,000).
The walkways are set up in such a way that between any two distinct pastures, there is exactly one path of walkways that travels between them. Thus, the walkways form a tree.
The cows are very social and wish to visit each other often. Ever in a hurry, they want you to help them schedule their visits by computing the lengths of the paths between 1 <= L_i <= 10,000 pairs of pastures (each pair given as a query p1,p2 (1 <= p1 <= N; 1 <= p2 <= N).
POINTS: 200
有N(2<=N<=1000)头奶牛,编号为1到W,它们正在同样编号为1到N的牧场上行走.为了方 便,我们假设编号为i的牛恰好在第i号牧场上.
有一些牧场间每两个牧场用一条双向道路相连,道路总共有N - 1条,奶牛可以在这些道路 上行走.第i条道路把第Ai个牧场和第Bi个牧场连了起来(1 <= A_i <= N; 1 <= B_i <= N),而它的长度 是 1 <= L_i <= 10,000.在任意两个牧场间,有且仅有一条由若干道路组成的路径相连.也就是说,所有的道路构成了一棵树.
奶牛们十分希望经常互相见面.它们十分着急,所以希望你帮助它们计划它们的行程,你只 需要计算出Q(1 < Q < 1000)对点之间的路径长度•每对点以一个询问p1,p2 (1 <= p1 <= N; 1 <= p2 <= N). 的形式给出.
输入输出格式
输入格式:
Line 1: Two space-separated integers: N and Q
Lines 2..N: Line i+1 contains three space-separated integers: A_i, B_i, and L_i
- Lines N+1..N+Q: Each line contains two space-separated integers representing two distinct pastures between which the cows wish to travel: p1 and p2
输出格式:
- Lines 1..Q: Line i contains the length of the path between the two pastures in query i.
输入输出样例
4 2
2 1 2
4 3 2
1 4 3
1 2
3 2
2
7
说明
Query 1: The walkway between pastures 1 and 2 has length 2.
Query 2: Travel through the walkway between pastures 3 and 4, then the one between 4 and 1, and finally the one between 1 and 2, for a total length of 7.
路径是一条树两点间的距离用lca求解
考虑边权问题,由于倍增法从节点1开始dfs处理深度与边权,可以处理出前缀和,那么两点间路径边权和就是dis[a]+dis[b]-2*dis[lca(a,b)]
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn = ;
int n,m,num,head[maxn];
struct node{
int v,w,next;
}edge[maxn];int dad[maxn][],deep[maxn],dis[maxn];
inline void add_edge(int u,int v,int w) {
edge[++num].v=v,edge[num].w=w,edge[num].next=head[u],head[u]=num;
}
void dfs(int x) {
deep[x]=deep[dad[x][]]+;
for(int i=;dad[x][i];i++)
dad[x][i+]=dad[dad[x][i]][i];
for(int i=head[x];i;i=edge[i].next)
if(!deep[edge[i].v]) {
dis[edge[i].v]=dis[x]+edge[i].w;
//printf("%d - > %d : %d\n",x,edge[i].v,edge[i].v);
dad[edge[i].v][]=x;
dfs(edge[i].v);
}
}
int lca(int x,int y) {
if(deep[x]>deep[y])swap(x,y);
for(int i=;i>=;i--)
if(deep[dad[y][i]]>=deep[x])y=dad[y][i];
if(x==y)return x;
for(int i=;i>=;i--)
if(dad[x][i]!=dad[y][i]) {
x=dad[x][i];
y=dad[y][i];
}
return dad[x][];
}
int main () {
scanf("%d%d",&n,&m);
for(int a,b,c,i=;i<n;++i) {
scanf("%d%d%d",&a,&b,&c);
add_edge(a,b,c);add_edge(b,a,c);
}
dfs();
int a,b;
while(m--) {
scanf("%d%d",&a,&b);
printf("%d\n",dis[a]+dis[b]-*dis[lca(a,b)]);
//printf("%d\n",dis[4]);
}
return ;
}
luogu P2912 [USACO08OCT]牧场散步Pasture Walking的更多相关文章
- LCA || BZOJ 1602: [Usaco2008 Oct]牧场行走 || Luogu P2912 [USACO08OCT]牧场散步Pasture Walking
题面:[USACO08OCT]牧场散步Pasture Walking 题解:LCA模版题 代码: #include<cstdio> #include<cstring> #inc ...
- bzoj1602 / P2912 [USACO08OCT]牧场散步Pasture Walking(倍增lca)
P2912 [USACO08OCT]牧场散步Pasture Walking 求树上两点间路径--->lca 使用倍增处理lca(树剖多长鸭) #include<iostream> # ...
- 洛谷P2912 [USACO08OCT]牧场散步Pasture Walking [2017年7月计划 树上问题 01]
P2912 [USACO08OCT]牧场散步Pasture Walking 题目描述 The N cows (2 <= N <= 1,000) conveniently numbered ...
- 洛谷——P2912 [USACO08OCT]牧场散步Pasture Walking(lca)
题目描述 The N cows (2 <= N <= 1,000) conveniently numbered 1..N are grazing among the N pastures ...
- 洛谷 P2912 [USACO08OCT]牧场散步Pasture Walking
题目描述 The N cows (2 <= N <= 1,000) conveniently numbered 1..N are grazing among the N pastures ...
- BZOJ——1602: [Usaco2008 Oct]牧场行走 || 洛谷—— P2912 [USACO08OCT]牧场散步Pasture Walking
http://www.lydsy.com/JudgeOnline/problem.php?id=1602 || https://www.luogu.org/problem/show?pid=2912 ...
- Luogu 2912 [USACO08OCT]牧场散步Pasture Walking
快乐树剖 #include<cstdio> #include<cstring> #include<algorithm> #define rd read() #def ...
- [USACO08OCT]牧场散步Pasture Walking BZOJ1602 LCA
题目描述 The N cows (2 <= N <= 1,000) conveniently numbered 1..N are grazing among the N pastures ...
- [luoguP2912] [USACO08OCT]牧场散步Pasture Walking(lca)
传送门 水题. 直接倍增求lca. x到y的距离为dis[x] + dis[y] - 2 * dis[lca(x, y)] ——代码 #include <cstdio> #include ...
随机推荐
- Noip2016 提高组 蚯蚓
刚看到这道题:这题直接用堆+模拟不就可以了(并没有认真算时间复杂度) 于是用priority_queue水到了85分-- (STL大法好) 天真的我还以为是常数问题,于是疯狂卡常--(我是ZZ) 直到 ...
- 关于Java IO流学习总结
一.IO流的三种分类方式 1.按流的方向分为:输入流和输出流 2.按流的数据单位不同分为:字节流和字符流 3.按流的功能不同分为:节点流和处理流 二.IO流的四大抽象类: 字符流:Reader ...
- 设置section的距离
在ios7中使用group类型的tableview时,第一个section距离navigationbar的距离很大,不符合这边的设计图.使用 myTableView . sectionHeaderHe ...
- PyQt5(2)、垃圾分类小程序(2)——初代窗口程序可执行文件
又是一天时间(又没做大作业).今天的心路历程:(1)前端后端怎么连接?(2)后端数据库插数据(3)完全没用上之前的字典反查法(4)突然发现面向对象编程其实很好用,甚至越用越上瘾(5)QLineEdit ...
- Linux实现删除撤回的方法。
RM命令改造 vim /etc/bashrc 在文件的最前端添加如下代码 #修改rm命令 alias rm=delete #命令别名,通过delete来实现rm改为mv alias r=de ...
- strcpy与strcat函数原型
1.strcpy函数原型 char *my_strcpy(char *dest,const char *src) //const使在函数中不能修改*src其原先的值{ char *strDest ...
- JAVA面向过程VS面向对象
面向过程 面向过程是一种自顶向下的编程,强调行为过程,可扩展性可维护性差. 优点: 性能比面向对象高,因为类调用时需要实例化,开销比较大,比较消耗资源. 单片机.嵌入式开发.Linux/Unix等一般 ...
- css 标题
纯CSS制作的复古风格的大标题 .vintage{ background: #EEE url(data:image/gif;base64,iVBORw0KGgoAAAANSUhEUgAAAAQAAAA ...
- C++枚举类型enum
为啥需要枚举类型 编程语言中的所有特性都是为了满足某种需求,达到某个目的还出现.不会莫名其妙的出现在那. 枚举可以用来保存一组属性的值.enum的全称是enumeration意思是列举 看着这句话可能 ...
- 对于一棵二叉树,请设计一个算法,创建含有某一深度上所有结点的链表。 给定二叉树的根结点指针TreeNode* root,以及链表上结点的深度,请返回一个链表ListNode,代表该深度上所有结点的值,请按树上从左往右的顺序链接,保证深度不超过树的高度,树上结点的值为非负整数且不超过100000。
/* struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; TreeNode(int x) : val(x ...